Update app.py
Browse files
app.py
CHANGED
|
@@ -18,13 +18,16 @@ def run_inference(image):
|
|
| 18 |
detections = results.pandas().xyxy[0]
|
| 19 |
|
| 20 |
# Count objects by category
|
| 21 |
-
object_counts = detections['name'].value_counts()
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Convert the annotated image from BGR to RGB for display
|
| 24 |
annotated_image = results.render()[0]
|
| 25 |
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 26 |
|
| 27 |
-
return annotated_image,
|
| 28 |
|
| 29 |
# Create the Gradio interface
|
| 30 |
interface = gr.Interface(
|
|
@@ -32,7 +35,7 @@ interface = gr.Interface(
|
|
| 32 |
inputs=gr.Image(type="pil"),
|
| 33 |
outputs=[
|
| 34 |
gr.Image(type="pil"),
|
| 35 |
-
gr.
|
| 36 |
],
|
| 37 |
title="YOLOv5 Object Detection with Counts",
|
| 38 |
description="Upload an image to run YOLOv5 object detection, see the annotated results, and view the count of detected objects by category."
|
|
|
|
| 18 |
detections = results.pandas().xyxy[0]
|
| 19 |
|
| 20 |
# Count objects by category
|
| 21 |
+
object_counts = detections['name'].value_counts()
|
| 22 |
+
|
| 23 |
+
# Create a formatted string to show object counts
|
| 24 |
+
count_text = "\n".join([f"{obj}: {count}" for obj, count in object_counts.items()])
|
| 25 |
|
| 26 |
# Convert the annotated image from BGR to RGB for display
|
| 27 |
annotated_image = results.render()[0]
|
| 28 |
annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 29 |
|
| 30 |
+
return annotated_image, count_text
|
| 31 |
|
| 32 |
# Create the Gradio interface
|
| 33 |
interface = gr.Interface(
|
|
|
|
| 35 |
inputs=gr.Image(type="pil"),
|
| 36 |
outputs=[
|
| 37 |
gr.Image(type="pil"),
|
| 38 |
+
gr.Textbox(label="Object Counts", lines=5, interactive=False) # Display counts as text
|
| 39 |
],
|
| 40 |
title="YOLOv5 Object Detection with Counts",
|
| 41 |
description="Upload an image to run YOLOv5 object detection, see the annotated results, and view the count of detected objects by category."
|