|
|
|
|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
vit_classifier = pipeline("image-classification", model="chrisis2/vit-food-classification-chrisis2") |
|
|
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification") |
|
|
|
|
|
|
|
|
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" |
|
|
] |
|
|
|
|
|
|
|
|
def classify_food(image): |
|
|
|
|
|
vit_results = vit_classifier(image) |
|
|
vit_output = {r['label']: r['score'] for r in vit_results} |
|
|
vit_top_label = vit_results[0]['label'] |
|
|
vit_top_score = vit_results[0]['score'] * 100 |
|
|
|
|
|
|
|
|
clip_results = clip_detector(image, candidate_labels=food_labels) |
|
|
clip_output = {r['label']: r['score'] for r in clip_results} |
|
|
clip_top_label = clip_results[0]['label'] |
|
|
clip_top_score = clip_results[0]['score'] * 100 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
example_images = [ |
|
|
["example_images/burger.jpg"], |
|
|
["example_images/donut.jpg"], |
|
|
["example_images/fries.jpg"], |
|
|
["example_images/pizza.jpg"], |
|
|
["example_images/sushi.jpg"], |
|
|
|
|
|
] |
|
|
|
|
|
|
|
|
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 globalen und 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) |