Spaces:
Running
Running
Update app.py
#2
by AymanFahim - opened
app.py
CHANGED
|
@@ -2,50 +2,67 @@ import gradio as gr
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import PIL.Image
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
model = YOLO('best.pt')
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Extract text names
|
| 26 |
-
detected_items = set()
|
| 27 |
-
for box in results[0].boxes:
|
| 28 |
-
class_id = int(box.cls)
|
| 29 |
-
class_name = model.names[class_id]
|
| 30 |
-
detected_items.add(class_name)
|
| 31 |
-
|
| 32 |
-
ingredient_list_text = ", ".join(detected_items)
|
| 33 |
-
if not ingredient_list_text:
|
| 34 |
-
ingredient_list_text = "No ingredients detected."
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# Build the
|
| 39 |
demo = gr.Interface(
|
| 40 |
-
fn=
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
outputs=[
|
| 44 |
-
gr.
|
| 45 |
-
gr.Textbox(label="
|
| 46 |
],
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
demo.launch()
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import PIL.Image
|
| 4 |
|
| 5 |
+
# Load your model
|
| 6 |
model = YOLO('best.pt')
|
| 7 |
|
| 8 |
+
def process_multiple_images(file_paths):
|
| 9 |
+
"""
|
| 10 |
+
This function now accepts a LIST of file paths,
|
| 11 |
+
loops through them, and combines the results.
|
| 12 |
+
"""
|
| 13 |
+
if not file_paths:
|
| 14 |
+
return [], "No files uploaded."
|
| 15 |
+
|
| 16 |
+
gallery_images = []
|
| 17 |
+
all_detected_ingredients = set()
|
| 18 |
+
|
| 19 |
+
# Loop through every image uploaded
|
| 20 |
+
for image_path in file_paths:
|
| 21 |
+
# Run the model on this specific image
|
| 22 |
+
# Using conf=0.5 as a balanced starting point
|
| 23 |
+
results = model.predict(source=image_path, conf=0.5, iou=0.3, imgsz=640)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# 1. Process the Image (Draw boxes)
|
| 26 |
+
res_plotted = results[0].plot()
|
| 27 |
+
# Convert BGR (OpenCV) to RGB (Gradio/PIL)
|
| 28 |
+
res_rgb = res_plotted[..., ::-1]
|
| 29 |
+
pil_img = PIL.Image.fromarray(res_rgb)
|
| 30 |
+
|
| 31 |
+
# Add to our list of images to display
|
| 32 |
+
gallery_images.append(pil_img)
|
| 33 |
+
|
| 34 |
+
# 2. Extract Ingredients
|
| 35 |
+
for box in results[0].boxes:
|
| 36 |
+
cls_id = int(box.cls)
|
| 37 |
+
name = model.names[cls_id]
|
| 38 |
+
all_detected_ingredients.add(name)
|
| 39 |
+
|
| 40 |
+
# Create a master list of unique ingredients found across ALL photos
|
| 41 |
+
master_list_text = ", ".join(all_detected_ingredients)
|
| 42 |
+
if not master_list_text:
|
| 43 |
+
master_list_text = "No ingredients detected."
|
| 44 |
+
|
| 45 |
+
return gallery_images, master_list_text
|
| 46 |
|
| 47 |
+
# Build the Interface
|
| 48 |
demo = gr.Interface(
|
| 49 |
+
fn=process_multiple_images,
|
| 50 |
+
|
| 51 |
+
# INPUT CHANGE: Use gr.File allows selecting multiple files at once
|
| 52 |
+
inputs=gr.File(
|
| 53 |
+
file_count="multiple",
|
| 54 |
+
file_types=["image"],
|
| 55 |
+
label="Upload Multiple Photos (Fridge & Pantry)"
|
| 56 |
+
),
|
| 57 |
+
|
| 58 |
+
# OUTPUT CHANGE: Use gr.Gallery to show a carousel of images
|
| 59 |
outputs=[
|
| 60 |
+
gr.Gallery(label="Processed Images", columns=2),
|
| 61 |
+
gr.Textbox(label="Master Ingredient List")
|
| 62 |
],
|
| 63 |
+
|
| 64 |
+
title="Bulk Ingredient Scanner",
|
| 65 |
+
description="Upload multiple photos at once. The AI will combine all ingredients into one list."
|
| 66 |
)
|
| 67 |
|
| 68 |
demo.launch()
|