Files changed (1) hide show
  1. app.py +54 -37
app.py CHANGED
@@ -2,50 +2,67 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  import PIL.Image
4
 
5
- # Load the model
6
  model = YOLO('best.pt')
7
 
8
- def detect_ingredients(image_path):
9
- # KEY FIX 1: We use the image_path directly.
10
- # This forces YOLO to load the raw file, bypassing Gradio's compression.
11
-
12
- # KEY FIX 2: Lowered conf to 0.4.
13
- # If it detects too much junk, raise this to 0.5 or 0.6.
14
- # imgsz=640 ensures it uses the standard YOLO resolution.
15
- results = model.predict(source=image_path, conf=0.4, iou=0.3, imgsz=640)
16
-
17
- # Plot the results (draw boxes)
18
- # result_image is a numpy array in BGR format
19
- result_image_bgr = results[0].plot()
20
-
21
- # KEY FIX 3: Convert BGR (OpenCV) to RGB (Gradio/PIL)
22
- # This ensures colors aren't weirdly inverted
23
- result_image_rgb = result_image_bgr[..., ::-1]
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
- return PIL.Image.fromarray(result_image_rgb), ingredient_list_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- # Build the App
39
  demo = gr.Interface(
40
- fn=detect_ingredients,
41
- # KEY FIX 4: type="filepath" prevents the browser from shrinking the image
42
- inputs=gr.Image(type="filepath", label="Upload Photo"),
 
 
 
 
 
 
 
43
  outputs=[
44
- gr.Image(type="pil", label="Detections"),
45
- gr.Textbox(label="Detected Ingredients")
46
  ],
47
- title="Fridge Scanner (High Quality)",
48
- description="Upload a photo. Processing at native resolution."
 
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()