CTP-Project / app.py
AymanFahim's picture
Update app.py
d16596b verified
raw
history blame
2.1 kB
import gradio as gr
from ultralytics import YOLO
import PIL.Image
# Load your model
model = YOLO('best.pt')
def process_multiple_images(file_paths):
"""
This function now accepts a LIST of file paths,
loops through them, and combines the results.
"""
if not file_paths:
return [], "No files uploaded."
gallery_images = []
all_detected_ingredients = set()
# Loop through every image uploaded
for image_path in file_paths:
# Run the model on this specific image
# Using conf=0.5 as a balanced starting point
results = model.predict(source=image_path, conf=0.5, iou=0.3, imgsz=640)
# 1. Process the Image (Draw boxes)
res_plotted = results[0].plot()
# Convert BGR (OpenCV) to RGB (Gradio/PIL)
res_rgb = res_plotted[..., ::-1]
pil_img = PIL.Image.fromarray(res_rgb)
# Add to our list of images to display
gallery_images.append(pil_img)
# 2. Extract Ingredients
for box in results[0].boxes:
cls_id = int(box.cls)
name = model.names[cls_id]
all_detected_ingredients.add(name)
# Create a master list of unique ingredients found across ALL photos
master_list_text = ", ".join(all_detected_ingredients)
if not master_list_text:
master_list_text = "No ingredients detected."
return gallery_images, master_list_text
# Build the Interface
demo = gr.Interface(
fn=process_multiple_images,
# INPUT CHANGE: Use gr.File allows selecting multiple files at once
inputs=gr.File(
file_count="multiple",
file_types=["image"],
label="Upload Multiple Photos (Fridge & Pantry)"
),
# OUTPUT CHANGE: Use gr.Gallery to show a carousel of images
outputs=[
gr.Gallery(label="Processed Images", columns=2),
gr.Textbox(label="Master Ingredient List")
],
title="Bulk Ingredient Scanner",
description="Upload multiple photos at once. The AI will combine all ingredients into one list."
)
demo.launch()