Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
|
|
|
| 3 |
|
| 4 |
-
# 1. Load the model
|
| 5 |
-
# Ensure 'best.pt' is in the SAME folder as this file
|
| 6 |
model = YOLO('best.pt')
|
| 7 |
|
| 8 |
-
def detect_ingredients(
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
# 3. Get the image with boxes
|
| 13 |
result_image = results[0].plot()
|
| 14 |
|
| 15 |
-
# 4. Get the text list of ingredients
|
| 16 |
detected_items = set()
|
| 17 |
for box in results[0].boxes:
|
| 18 |
class_id = int(box.cls)
|
|
@@ -22,16 +20,20 @@ def detect_ingredients(image):
|
|
| 22 |
ingredient_list_text = ", ".join(detected_items)
|
| 23 |
if not ingredient_list_text:
|
| 24 |
ingredient_list_text = "No ingredients detected."
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
return result_image, ingredient_list_text
|
| 27 |
-
|
| 28 |
-
# 5. Create the interface
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=detect_ingredients,
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# 6. THE MOST IMPORTANT LINE:
|
| 37 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
+
import PIL.Image
|
| 4 |
|
|
|
|
|
|
|
| 5 |
model = YOLO('best.pt')
|
| 6 |
|
| 7 |
+
def detect_ingredients(image_path):
|
| 8 |
+
# Load the image from the file path to ensure full quality
|
| 9 |
+
# MATCHING YOUR LOCAL SETTINGS: conf=0.7
|
| 10 |
+
results = model.predict(source=image_path, conf=0.7, iou=0.3)
|
| 11 |
|
|
|
|
| 12 |
result_image = results[0].plot()
|
| 13 |
|
|
|
|
| 14 |
detected_items = set()
|
| 15 |
for box in results[0].boxes:
|
| 16 |
class_id = int(box.cls)
|
|
|
|
| 20 |
ingredient_list_text = ", ".join(detected_items)
|
| 21 |
if not ingredient_list_text:
|
| 22 |
ingredient_list_text = "No ingredients detected."
|
| 23 |
+
|
| 24 |
+
# Convert numpy result back to PIL for Gradio to display
|
| 25 |
+
return PIL.Image.fromarray(result_image[..., ::-1]), ingredient_list_text
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=detect_ingredients,
|
| 29 |
+
# CHANGE: type="filepath" prevents Gradio from compressing/resizing before the model sees it
|
| 30 |
+
inputs=gr.Image(type="filepath", label="Upload Photo"),
|
| 31 |
+
outputs=[
|
| 32 |
+
gr.Image(type="pil", label="Detections"),
|
| 33 |
+
gr.Textbox(label="Detected Ingredients")
|
| 34 |
+
],
|
| 35 |
+
title="Fridge Ingredient Scanner",
|
| 36 |
+
description="Upload a photo of your groceries. Using conf=0.7 to match local training."
|
| 37 |
)
|
| 38 |
|
|
|
|
| 39 |
demo.launch()
|