Clementapa commited on
Commit
1316948
1 Parent(s): 82f7f8f

Add video detection

Browse files
Files changed (1) hide show
  1. app.py +75 -17
app.py CHANGED
@@ -1,6 +1,8 @@
 
1
  from typing import List
2
 
3
  import gradio as gr
 
4
  import supervision as sv
5
  import torch
6
  from PIL import Image
@@ -60,7 +62,7 @@ def annotate(
60
  return Image.fromarray(annotated_bgr_image[:, :, ::-1])
61
 
62
 
63
- def inference(image_rgb_pil: Image.Image, confidence: float) -> List[Image.Image]:
64
  output = YOLO_MODEL(image_rgb_pil, imgsz=640, verbose=False)[0]
65
  detections = sv.Detections.from_ultralytics(output)
66
 
@@ -79,30 +81,86 @@ def inference(image_rgb_pil: Image.Image, confidence: float) -> List[Image.Image
79
  )
80
 
81
 
82
- custom_theme = gr.themes.Soft(primary_hue="blue").set(
83
- button_secondary_background_fill="*neutral_100",
84
- button_secondary_background_fill_hover="*neutral_200",
85
- )
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  with gr.Blocks(theme=custom_theme, css="style.css") as demo:
88
  gr.Markdown(MARKDOWN)
89
 
90
- with gr.Row():
91
- with gr.Column():
92
- input_image = gr.Image(image_mode="RGB", type="pil", height=500)
93
- confidence_slider = gr.Slider(
94
- label="Confidence", minimum=0.1, maximum=1.0, step=0.05, value=0.6
95
- )
96
- submit_button = gr.Button("Let's find orang outans 🦧 !")
97
- output_image = gr.Image(label="Results", type="pil")
98
-
99
- submit_button.click(
100
- inference,
101
- inputs=[input_image, confidence_slider],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  outputs=output_image,
103
  queue=True,
104
  )
105
 
 
 
 
 
 
 
106
 
107
  if __name__ == "__main__":
108
  demo.queue(max_size=20, api_open=False).launch()
 
1
+ import os.path as osp
2
  from typing import List
3
 
4
  import gradio as gr
5
+ import numpy as np
6
  import supervision as sv
7
  import torch
8
  from PIL import Image
 
62
  return Image.fromarray(annotated_bgr_image[:, :, ::-1])
63
 
64
 
65
+ def inference_image(image_rgb_pil: Image.Image, confidence: float) -> List[Image.Image]:
66
  output = YOLO_MODEL(image_rgb_pil, imgsz=640, verbose=False)[0]
67
  detections = sv.Detections.from_ultralytics(output)
68
 
 
81
  )
82
 
83
 
84
+ def process_frame(frame: np.ndarray, _) -> np.ndarray:
85
+ output = YOLO_MODEL(frame, imgsz=640, verbose=False)[0]
 
 
86
 
87
+ detections = sv.Detections.from_ultralytics(output)
88
+
89
+ labels = [
90
+ f"{output.names[class_id]} {confidence:0.2f}"
91
+ for _, _, confidence, class_id, _ in detections
92
+ ]
93
+
94
+ thickness = 2
95
+ text_thickness = 1
96
+ text_scale = 1.0
97
+
98
+ height, width, _ = output.orig_img.shape
99
+
100
+ thickness_ratio = ((width + height) / 2) / 400
101
+ text_scale_ratio = ((width + height) / 2) / 600
102
+ text_thickness_ratio = ((width + height) / 2) / 400
103
+
104
+ BOX_ANNOTATOR.thickness = int(thickness * thickness_ratio)
105
+ BOX_ANNOTATOR.text_scale = float(text_scale * text_scale_ratio)
106
+ BOX_ANNOTATOR.text_thickness = int(text_thickness * text_thickness_ratio)
107
+
108
+ annotated_frame = BOX_ANNOTATOR.annotate(
109
+ scene=output.orig_img.copy(), detections=detections, labels=labels
110
+ )
111
+ return annotated_frame
112
+
113
+
114
+ def inference_video(path_video):
115
+ path_output_video = "temp.mp4"
116
+ sv.process_video(
117
+ source_path=path_video,
118
+ target_path=path_output_video,
119
+ callback=process_frame,
120
+ )
121
+ return path_output_video
122
+
123
+
124
+ custom_theme = gr.themes.Soft(primary_hue="green")
125
  with gr.Blocks(theme=custom_theme, css="style.css") as demo:
126
  gr.Markdown(MARKDOWN)
127
 
128
+ with gr.Tab("Detect on an image 🖼️"):
129
+ with gr.Row():
130
+ with gr.Column():
131
+ input_image = gr.Image(
132
+ image_mode="RGB",
133
+ sources=["upload", "clipboard"],
134
+ type="pil",
135
+ )
136
+ confidence_image_slider = gr.Slider(
137
+ label="Confidence", minimum=0.1, maximum=1.0, step=0.05, value=0.6
138
+ )
139
+ submit_button_image = gr.Button("Let's find orang outans 🦧 !")
140
+ output_image = gr.Image(label="Results", type="pil")
141
+ with gr.Tab("Detect on a video 📹"):
142
+ with gr.Row():
143
+ with gr.Column():
144
+ input_video = gr.Video(sources=["upload"])
145
+ # confidence_video_slider = gr.Slider(
146
+ # label="Confidence", minimum=0.1, maximum=1.0, step=0.05, value=0.6
147
+ # )
148
+ submit_button_video = gr.Button("Let's find orang outans 🦧 !")
149
+ output_video = gr.Video(label="Results")
150
+
151
+ submit_button_image.click(
152
+ inference_image,
153
+ inputs=[input_image, confidence_image_slider],
154
  outputs=output_image,
155
  queue=True,
156
  )
157
 
158
+ submit_button_video.click(
159
+ inference_video,
160
+ inputs=[input_video],
161
+ outputs=output_video,
162
+ queue=True,
163
+ )
164
 
165
  if __name__ == "__main__":
166
  demo.queue(max_size=20, api_open=False).launch()