atalaydenknalbant commited on
Commit
45439d0
·
verified ·
1 Parent(s): e61e14d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -160
app.py CHANGED
@@ -1,165 +1,51 @@
1
- import spaces
2
- import supervision as sv
3
  import PIL.Image as Image
4
  from ultralytics import YOLO
5
- import gradio as gr
6
- import torch
7
-
8
-
9
-
10
- model_filenames = [
11
- "yolo11n.pt",
12
- "yolo11s.pt",
13
- "yolo11m.pt",
14
- "yolo11l.pt",
15
- "yolo11x.pt"
16
- ]
17
-
18
-
19
-
20
- box_annotator = sv.BoxAnnotator()
21
-
22
- category_dict = {
23
- 0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
24
- 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
25
- 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',
26
- 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',
27
- 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
28
- 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
29
- 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',
30
- 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',
31
- 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
32
- 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',
33
- 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',
34
- 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
35
- 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
36
- 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
37
- 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
38
- 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'
39
- }
40
-
41
-
42
 
43
- @spaces.GPU
44
- def yolo_inference(image, model_id, conf_threshold, iou_threshold, max_detection):
45
-
46
-
47
  model = YOLO(model_id)
48
- results = model(source=image, imgsz=640, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=max_detection)[0]
49
- detections = sv.Detections.from_ultralytics(results)
50
-
51
- labels = [
52
- f"{category_dict[class_id]} {confidence:.2f}"
53
- for class_id, confidence in zip(detections.class_id, detections.confidence)
54
- ]
55
- annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
56
-
57
- return annotated_image
58
-
59
- def app():
60
- with gr.Blocks():
61
- with gr.Row():
62
- with gr.Column():
63
- image = gr.Image(type="pil", label="Image", interactive=True)
64
 
65
- model_id = gr.Dropdown(
66
- label="Model",
67
- choices=model_filenames,
68
- value=model_filenames[0] if model_filenames else "",
69
- )
70
- conf_threshold = gr.Slider(
71
- label="Confidence Threshold",
72
- minimum=0.1,
73
- maximum=1.0,
74
- step=0.1,
75
- value=0.25,
76
- )
77
- iou_threshold = gr.Slider(
78
- label="IoU Threshold",
79
- minimum=0.1,
80
- maximum=1.0,
81
- step=0.1,
82
- value=0.45,
83
- )
84
-
85
- max_detection = gr.Slider(
86
- label="Max Detection",
87
- minimum=1,
88
- maximum=300,
89
- step=1,
90
- value=300,
91
- )
92
- yolov_infer = gr.Button(value="Detect Objects")
93
-
94
- with gr.Column():
95
- output_image = gr.Image(type="pil", label="Annotated Image", interactive=False)
96
-
97
- yolov_infer.click(
98
- fn=yolo_inference,
99
- inputs=[
100
- image,
101
- model_id,
102
- conf_threshold,
103
- iou_threshold,
104
- max_detection,
105
- ],
106
- outputs=[output_image],
107
- )
108
-
109
- gr.Examples(
110
- examples=[
111
- [
112
- "zidane.jpg",
113
- "yolo11s.pt",
114
- 0.25,
115
- 0.45,
116
- 300,
117
- ],
118
-
119
- [
120
- "bus.jpg",
121
- "yolo11m.pt",
122
- 0.25,
123
- 0.45,
124
- 300,
125
- ],
126
- [
127
- "yolo_vision.jpg",
128
- "yolo11x.pt",
129
- 0.25,
130
- 0.45,
131
- 300,
132
- ],
133
- ],
134
- fn=yolo_inference,
135
- inputs=[
136
- image,
137
- model_id,
138
- conf_threshold,
139
- iou_threshold,
140
- max_detection,
141
- ],
142
- outputs=[output_image],
143
- cache_examples=True,
144
- )
145
-
146
- gradio_app = gr.Blocks()
147
- with gradio_app:
148
- gr.HTML(
149
- """
150
- <h1 style='text-align: center'>
151
- Yolo11: Object Detection
152
- </h1>
153
- <a>
154
- """)
155
- gr.HTML(
156
- """
157
- <p style='text-align: center'>
158
- Latest ultralytics yolo11 object detection models. Upload an image to run inference.
159
- </p>
160
- """)
161
- with gr.Row():
162
- with gr.Column():
163
- app()
164
-
165
- gradio_app.launch()
 
1
+ import gradio as gr
 
2
  import PIL.Image as Image
3
  from ultralytics import YOLO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ def yolo_inference(images, model_id, conf_threshold, iou_threshold, max_detection):
 
 
 
6
  model = YOLO(model_id)
7
+ results = model.predict(
8
+ source=images,
9
+ conf=conf_threshold,
10
+ iou=iou_threshold,
11
+ imgsz=640,
12
+ max_det=max_detection,
13
+ show_labels=True,
14
+ show_conf=True,
15
+ )
16
+
17
+ annotated_images = []
18
+ for result in results:
19
+ im_array = result.plot()
20
+ im = Image.fromarray(im_array)
21
+ annotated_images.append(im)
 
22
 
23
+ return annotated_images
24
+
25
+ interface = gr.Interface(
26
+ fn=yolo_inference,
27
+ inputs=[
28
+ gr.Image(type="pil", label="Upload Image(s)", tool="editor", source="upload", multiple=True),
29
+ gr.Dropdown(
30
+ choices=['yolo11n.pt', 'yolo11s.pt', 'yolo11m.pt', 'yolo11l.pt', 'yolo11x.pt',
31
+ 'yolo11n-seg.pt', 'yolo11s-seg.pt', 'yolo11m-seg.pt', 'yolo11l-seg.pt', 'yolo11x-seg.pt',
32
+ 'yolo11n-pose.pt', 'yolo11s-pose.pt', 'yolo11m-pose.pt', 'yolo11l-pose.pt', 'yolo11x-pose.pt',
33
+ 'yolo11n-obb.pt', 'yolo11s-obb.pt', 'yolo11m-obb.pt', 'yolo11l-obb.pt', 'yolo11x-obb.pt',
34
+ 'yolo11n-cls.pt', 'yolo11s-cls.pt', 'yolo11m-cls.pt', 'yolo11l-cls.pt', 'yolo11x-cls.pt'],
35
+ label="Model Name",
36
+ value="yolo11n.pt",
37
+ ),
38
+ gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence Threshold"),
39
+ gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU Threshold"),
40
+ gr.Slider(minimum=1, maximum=300, step=1, value=300, label="Max Detection"),
41
+ ],
42
+ outputs=gr.Gallery(label="Annotated Image"),
43
+ title="Yolo11: Object Detection",
44
+ description="Upload image(s) for inference using the latest Ultralytics YOLO11 models.",
45
+ examples=[
46
+ [["zidane.jpg"], "yolo11s.pt", 0.25, 0.45, 300],
47
+ [["bus.jpg"], "yolo11m.pt", 0.25, 0.45, 300],
48
+ [["yolo_vision.jpg"], "yolo11x.pt", 0.25, 0.45, 300],
49
+ ],
50
+ )
51
+ interface.launch()