Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +37 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from collections import Counter
|
| 5 |
+
|
| 6 |
+
model = YOLO("yolov8s.pt")
|
| 7 |
+
|
| 8 |
+
def detect_classify(image):
|
| 9 |
+
results = model(image)[0]
|
| 10 |
+
boxes = results.boxes
|
| 11 |
+
|
| 12 |
+
if boxes is not None and len(boxes.cls) > 0:
|
| 13 |
+
class_ids = boxes.cls.tolist()
|
| 14 |
+
names = results.names
|
| 15 |
+
labels = [names[int(cls_id)] for cls_id in class_ids]
|
| 16 |
+
label_counts = Counter(labels)
|
| 17 |
+
count_str = ", ".join([f"{v} {k}" for k, v in label_counts.items()])
|
| 18 |
+
total = sum(label_counts.values())
|
| 19 |
+
final_count = f"Total Detected: {total}\nBreakdown: {count_str}"
|
| 20 |
+
else:
|
| 21 |
+
final_count = "No objects detected."
|
| 22 |
+
|
| 23 |
+
annotated_img = Image.fromarray(results.plot())
|
| 24 |
+
return annotated_img, final_count
|
| 25 |
+
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=detect_classify,
|
| 28 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
| 29 |
+
outputs=[
|
| 30 |
+
gr.Image(label="Detected Image"),
|
| 31 |
+
gr.Label(label="Detection Summary")
|
| 32 |
+
],
|
| 33 |
+
title="Object Detector",
|
| 34 |
+
description="Upload an image to detect objects using YOLOv8."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
gradio
|
| 3 |
+
pillow
|
| 4 |
+
torch
|