Alaaeldin commited on
Commit
2bb2622
·
verified ·
1 Parent(s): 0f5109e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -15,6 +15,8 @@ def predict(image):
15
  # Run YOLOv8 inference
16
  results = model(img)
17
 
 
 
18
  # Draw bounding boxes on the image
19
  for r in results:
20
  for box in r.boxes:
@@ -23,19 +25,20 @@ def predict(image):
23
  cls = int(box.cls[0]) # Class index
24
 
25
  label = f"{model.names[cls]} {conf:.2f}"
 
26
  cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
27
  cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
28
 
29
  # Convert back to PIL format
30
- return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
31
 
32
  # Create the Gradio interface
33
  iface = gr.Interface(
34
  fn=predict,
35
  inputs=gr.Image(type="pil"),
36
- outputs=gr.Image(type="pil"),
37
  title="YOLOv8 Fruit Detection",
38
- description="Upload an image and the model will detect objects."
39
  )
40
 
41
  # Launch the Gradio app
 
15
  # Run YOLOv8 inference
16
  results = model(img)
17
 
18
+ detected_objects = [] # Store detected object names and confidence
19
+
20
  # Draw bounding boxes on the image
21
  for r in results:
22
  for box in r.boxes:
 
25
  cls = int(box.cls[0]) # Class index
26
 
27
  label = f"{model.names[cls]} {conf:.2f}"
28
+ detected_objects.append(label) # Save object name and confidence
29
  cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
30
  cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
31
 
32
  # Convert back to PIL format
33
+ return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), "\n".join(detected_objects) if detected_objects else "No objects detected."
34
 
35
  # Create the Gradio interface
36
  iface = gr.Interface(
37
  fn=predict,
38
  inputs=gr.Image(type="pil"),
39
+ outputs=[gr.Image(type="pil"), gr.Textbox(label="Detected Objects")],
40
  title="YOLOv8 Fruit Detection",
41
+ description="Upload an image and the model will detect objects and list them."
42
  )
43
 
44
  # Launch the Gradio app