File size: 3,289 Bytes
d7c784c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! pip install gradio
import gradio as gr
from transformers import pipeline

# Lade beide Modelle
vit_classifier = pipeline("image-classification", model="chrisis2/vit-food-classification-chrisis2")  # ViT: speziell traineirtes Modell für Lebensmittelklassifikation
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification") #CLIP: Allgmeines Zero-Shot-Modell das Bilde rmit Textbeschreibungen vergleicht

# Liste aller Food-Labes für Zero-Shot Modell
food_labels = [
    "Baked Potato", "Crispy Chicken", "Donut", "Fries", "Hot Dog", "Sandwich", "Taco", "Taquito",
    "apple_pie", "burger", "butter_naan", "chai", "chapati", "cheesecake", "chicken_curry",
    "chole_bhature", "dal_makhani", "dhokla", "fried_rice", "ice_cream", "idli", "jalebi",
    "kaathi_rolls", "kadai_paneer", "kulfi", "masala_dosa", "momos", "omelette", "paani_puri",
    "pakode", "pav_bhaji", "pizza", "samosa", "sushi"
]

# Klassifikationsfunktion
def classify_food(image):
    # Klassifikation mit ViT
    vit_results = vit_classifier(image)
    vit_output = {r['label']: r['score'] for r in vit_results} # Ergebnisse als Dictionary
    vit_top_label = vit_results[0]['label'] #beste Vrohersage
    vit_top_score = vit_results[0]['score'] * 100 # zu % umwandeln

    # Klassifikation mit CLIP
    clip_results = clip_detector(image, candidate_labels=food_labels) 
    clip_output = {r['label']: r['score'] for r in clip_results} # Ergebnisse als Dictionary
    clip_top_label = clip_results[0]['label'] #beste Vrohersage
    clip_top_score = clip_results[0]['score'] * 100 # zu % umwandeln

    # Formatierte Zusammenfassung für Anzeige
    summary = (
        "### Höchste Vorhersagewahrscheinlichkeit pro Modell:\n\n"
        f"**Trainiertes ViT-Modell**: erkennt **{vit_top_label}** mit **{vit_top_score:.3f}%** Wahrscheinlichkeit.\n\n"
        f"**CLIP Zero-Shot-Modell**: erkennt **{clip_top_label}** mit **{clip_top_score:.3f}%** Wahrscheinlichkeit."
    )

    return {"Trainiertes ViT-Modell": vit_output, "CLIP Zero-Shot": clip_output}, summary

# Beispielbilder für Anzeige
example_images = [
    ["example_images/burger.jpg"],
    ["example_images/donut.jpg"],
    ["example_images/fries.jpg"],
    ["example_images/pizza.jpg"],
    ["example_images/sushi.jpg"],
    
]

# Erstellung der Gradio-Oberfläche
iface = gr.Interface(
    fn=classify_food,
    inputs=gr.Image(type="filepath"),
    outputs=[gr.JSON(label="Detaillierte Modell-Ausgaben"), gr.Markdown(label="Fazit der besten Vorhersagen")],
    title="🍔 KI-Modellvergleich zur Klassifikation von Gerichten 🍟",
    description="""Diese Anwendung vergleicht zwei KI-Modelle zur Klassifikation von Gerichten einschliesslich der westlichen und vor allem indischen Küche:

Modell 1: Ein trainiertes ViT-Modell: Wurde speziell mit einem Datensatz aus Food-Bildern trainiert und erkennt häufige Speisen besonders zuverlässig.

Modell 2: Ein CLIP Zero-Shot-Modell: Wurde nicht speziell für Food trainiert. Es nutzt Sprachverständnis, um das Bild mit Text-Labels zu vergleichen.

📸 Lade ein Bild eines Gerichts hoch (z. B. Pizza, Sushi oder Donut) und sieh dir an, wie beide Modelle es erkennen und ob sie sich einig sind!""",
    examples=example_images
)

iface.launch(share=True)