File size: 1,803 Bytes
34733a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

import streamlit as st
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForImageClassification
import torch
import requests
from utils.matcher import oversaet_fuzzy

@st.cache_resource
def load_model():
    processor = AutoImageProcessor.from_pretrained("timm/food101-vit-base-patch16-224")
    model = AutoModelForImageClassification.from_pretrained("timm/food101-vit-base-patch16-224")
    return processor, model

processor, model = load_model()

st.title("Kalorieestimat og Fødevareklassificering")
uploaded_file = st.file_uploader("Upload et billede", type=["jpg", "jpeg", "png"])
confidence_threshold = 0.7

if uploaded_file is not None:
    image = Image.open(uploaded_file).convert("RGB")
    st.image(image, caption="Uploadet billede", use_column_width=True)

    inputs = processor(images=image, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        predicted_class_idx = logits.argmax(-1).item()
        confidence = torch.softmax(logits, dim=-1)[0][predicted_class_idx].item()

    label = model.config.id2label[predicted_class_idx]
    label_dk = oversaet_fuzzy(label)

    st.markdown(f"**Modelgæt:** {label} ({confidence*100:.1f}%)")
    st.markdown(f"**Oversat (fuzzy):** {label_dk}")

    if confidence < confidence_threshold:
        manual = st.selectbox("Modellen er usikker – vælg manuelt fødevaretype:", options=["æg", "kartofler", "smør", "broccoli"])
        st.markdown(f"**Manuelt valg:** {manual}")

    feedback = st.text_input("Har du feedback eller en mere præcis betegnelse?")
    if feedback:
        st.success("Tak for din feedback!")

    st.subheader("Eksempel på fødevareanalyse:")
    st.markdown("- 100 g æg
- 200 g kartofler
- 50 g smør
- 25 g broccoli")