Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# !pip install ultralytics gradio supervision torch pillow opencv-python
|
3 |
+
import spaces
|
4 |
+
import supervision as sv
|
5 |
+
import PIL.Image as Image
|
6 |
+
import cv2
|
7 |
+
import numpy as np
|
8 |
+
from ultralytics import YOLO
|
9 |
+
import gradio as gr
|
10 |
+
import torch
|
11 |
+
|
12 |
+
# YOLO model filenames
|
13 |
+
model_filenames = [
|
14 |
+
"yolo11n.pt",
|
15 |
+
"yolo11s.pt",
|
16 |
+
"yolo11m.pt",
|
17 |
+
"yolo11l.pt",
|
18 |
+
"yolo11x.pt"
|
19 |
+
]
|
20 |
+
|
21 |
+
# Box annotator for drawing bounding boxes
|
22 |
+
box_annotator = sv.BoxAnnotator()
|
23 |
+
|
24 |
+
# COCO category dictionary for labeling classes
|
25 |
+
category_dict = {
|
26 |
+
0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
|
27 |
+
6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
|
28 |
+
11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',
|
29 |
+
16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',
|
30 |
+
22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
|
31 |
+
27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
|
32 |
+
32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',
|
33 |
+
36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',
|
34 |
+
40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
|
35 |
+
46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',
|
36 |
+
51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',
|
37 |
+
56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
|
38 |
+
61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
|
39 |
+
67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
|
40 |
+
72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
|
41 |
+
77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'
|
42 |
+
}
|
43 |
+
|
44 |
+
# YOLO inference function
|
45 |
+
@spaces.GPU
|
46 |
+
def yolo_inference(image, model_id, conf_threshold, iou_threshold, max_detection):
|
47 |
+
model = YOLO(model_id)
|
48 |
+
# model.to("cuda")
|
49 |
+
results = model(source=image, imgsz=640, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=max_detection)[0]
|
50 |
+
detections = sv.Detections.from_ultralytics(results)
|
51 |
+
|
52 |
+
# Count objects
|
53 |
+
counts = {}
|
54 |
+
for class_id in detections.class_id:
|
55 |
+
label = category_dict[class_id]
|
56 |
+
if label not in counts:
|
57 |
+
counts[label] = 0
|
58 |
+
counts[label] += 1
|
59 |
+
|
60 |
+
# Prepare labels for drawing boxes and counting
|
61 |
+
labels = [
|
62 |
+
f"{category_dict[class_id]} {confidence:.2f}"
|
63 |
+
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
64 |
+
]
|
65 |
+
|
66 |
+
# Annotate the image with bounding boxes
|
67 |
+
annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
|
68 |
+
|
69 |
+
# Convert annotated_image to OpenCV format (from PIL)
|
70 |
+
annotated_image_cv = cv2.cvtColor(np.array(annotated_image), cv2.COLOR_RGB2BGR)
|
71 |
+
|
72 |
+
# Draw counts on the annotated image using cv2.putText
|
73 |
+
y_offset = 30 # Starting y offset for text
|
74 |
+
for label, count in counts.items():
|
75 |
+
text = f"{label}: {count}"
|
76 |
+
cv2.putText(annotated_image_cv, text, (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
|
77 |
+
y_offset += 25 # Increase y offset for the next label
|
78 |
+
|
79 |
+
# Convert back to PIL for Gradio output
|
80 |
+
return Image.fromarray(cv2.cvtColor(annotated_image_cv, cv2.COLOR_BGR2RGB))
|
81 |
+
|
82 |
+
# Gradio app function
|
83 |
+
def app():
|
84 |
+
with gr.Blocks():
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column():
|
87 |
+
image = gr.Image(type="pil", label="Image", interactive=True)
|
88 |
+
model_id = gr.Dropdown(label="Model", choices=model_filenames, value=model_filenames[0] if model_filenames else "")
|
89 |
+
conf_threshold = gr.Slider(label="Confidence Threshold", minimum=0.1, maximum=1.0, step=0.1, value=0.25)
|
90 |
+
iou_threshold = gr.Slider(label="IoU Threshold", minimum=0.1, maximum=1.0, step=0.1, value=0.45)
|
91 |
+
max_detection = gr.Slider(label="Max Detection", minimum=1, maximum=300, step=1, value=300)
|
92 |
+
yolov_infer = gr.Button(value="Detect Objects")
|
93 |
+
with gr.Column():
|
94 |
+
output_image = gr.Image(type="pil", label="Annotated Image", interactive=False)
|
95 |
+
yolov_infer.click(fn=yolo_inference, inputs=[image, model_id, conf_threshold, iou_threshold, max_detection], outputs=[output_image])
|
96 |
+
|
97 |
+
# Main Gradio app
|
98 |
+
gradio_app = gr.Blocks()
|
99 |
+
with gradio_app:
|
100 |
+
gr.HTML("<h1 style='text-align: center'>Object Counting using YoloV11</h1>")
|
101 |
+
gr.HTML("<p style='text-align: center'>Upload an image to run inference. By Kelvin</p>")
|
102 |
+
with gr.Row():
|
103 |
+
with gr.Column():
|
104 |
+
app()
|
105 |
+
|
106 |
+
# gradio_app.launch(debug=True)
|
107 |
+
gradio_app.launch()
|
108 |
+
|