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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
#import torch
|
3 |
+
import yolov7
|
4 |
+
import subprocess
|
5 |
+
import tempfile
|
6 |
+
import time
|
7 |
+
from pathlib import Path
|
8 |
+
|
9 |
+
import cv2
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
|
13 |
+
|
14 |
+
# Images
|
15 |
+
#torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg', 'zidane.jpg')
|
16 |
+
#torch.hub.download_url_to_file('https://raw.githubusercontent.com/obss/sahi/main/tests/data/small-vehicles1.jpeg', 'small-vehicles1.jpeg')
|
17 |
+
|
18 |
+
def image_fn(
|
19 |
+
image: gr.inputs.Image = None,
|
20 |
+
model_path: gr.inputs.Dropdown = None,
|
21 |
+
image_size: gr.inputs.Slider = 640,
|
22 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
23 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
24 |
+
):
|
25 |
+
"""
|
26 |
+
YOLOv7 inference function
|
27 |
+
Args:
|
28 |
+
image: Input image
|
29 |
+
model_path: Path to the model
|
30 |
+
image_size: Image size
|
31 |
+
conf_threshold: Confidence threshold
|
32 |
+
iou_threshold: IOU threshold
|
33 |
+
Returns:
|
34 |
+
Rendered image
|
35 |
+
"""
|
36 |
+
|
37 |
+
model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
|
38 |
+
model.conf = conf_threshold
|
39 |
+
model.iou = iou_threshold
|
40 |
+
results = model([image], size=image_size)
|
41 |
+
return results.render()[0]
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
def video_fn(model_path, video_file, conf_thres, iou_thres, start_sec, duration):
|
46 |
+
model = yolov7.load(model_path, device="cpu", hf_model=True, trace=False)
|
47 |
+
start_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec))
|
48 |
+
end_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec + duration))
|
49 |
+
|
50 |
+
suffix = Path(video_file).suffix
|
51 |
+
|
52 |
+
clip_temp_file = tempfile.NamedTemporaryFile(suffix=suffix)
|
53 |
+
subprocess.call(
|
54 |
+
f"ffmpeg -y -ss {start_timestamp} -i {video_file} -to {end_timestamp} -c copy {clip_temp_file.name}".split()
|
55 |
+
)
|
56 |
+
|
57 |
+
# Reader of clip file
|
58 |
+
cap = cv2.VideoCapture(clip_temp_file.name)
|
59 |
+
|
60 |
+
# This is an intermediary temp file where we'll write the video to
|
61 |
+
# Unfortunately, gradio doesn't play too nice with videos rn so we have to do some hackiness
|
62 |
+
# with ffmpeg at the end of the function here.
|
63 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4") as temp_file:
|
64 |
+
out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*"MP4V"), 30, (1280, 720))
|
65 |
+
|
66 |
+
num_frames = 0
|
67 |
+
max_frames = duration * 30
|
68 |
+
while cap.isOpened():
|
69 |
+
try:
|
70 |
+
ret, frame = cap.read()
|
71 |
+
if not ret:
|
72 |
+
break
|
73 |
+
except Exception as e:
|
74 |
+
print(e)
|
75 |
+
continue
|
76 |
+
print("FRAME DTYPE", type(frame))
|
77 |
+
out.write(model(frame, conf_thres, iou_thres))
|
78 |
+
num_frames += 1
|
79 |
+
print("Processed {} frames".format(num_frames))
|
80 |
+
if num_frames == max_frames:
|
81 |
+
break
|
82 |
+
|
83 |
+
out.release()
|
84 |
+
|
85 |
+
# Aforementioned hackiness
|
86 |
+
out_file = tempfile.NamedTemporaryFile(suffix="out.mp4", delete=False)
|
87 |
+
subprocess.run(f"ffmpeg -y -loglevel quiet -stats -i {temp_file.name} -c:v libx264 {out_file.name}".split())
|
88 |
+
|
89 |
+
return out_file.name
|
90 |
+
|
91 |
+
image_interface = gr.Interface(
|
92 |
+
fn=image_fn,
|
93 |
+
inputs=[
|
94 |
+
gr.inputs.Image(type="pil", label="Input Image"),
|
95 |
+
gr.inputs.Dropdown(
|
96 |
+
choices=[
|
97 |
+
"alshimaa/model_baseline",
|
98 |
+
"alshimaa/model_yolo7",
|
99 |
+
#"kadirnar/yolov7-v0.1",
|
100 |
+
],
|
101 |
+
default="alshimaa/model_baseline",
|
102 |
+
label="Model",
|
103 |
+
)
|
104 |
+
#gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size")
|
105 |
+
#gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
106 |
+
#gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold")
|
107 |
+
],
|
108 |
+
outputs=gr.outputs.Image(type="filepath", label="Output Image"),
|
109 |
+
title="Smart Environmental Eye (SEE)",
|
110 |
+
examples=[['image1.jpg', 'alshimaa/model_yolo7', 640, 0.25, 0.45], ['image2.jpg', 'alshimaa/model_yolo7', 640, 0.25, 0.45], ['image3.jpg', 'alshimaa/model_yolo7', 640, 0.25, 0.45]],
|
111 |
+
cache_examples=True,
|
112 |
+
theme='huggingface',
|
113 |
+
)
|
114 |
+
|
115 |
+
|
116 |
+
video_interface = gr.Interface(
|
117 |
+
fn=video_fn,
|
118 |
+
inputs=[
|
119 |
+
gr.Video(type="file"),
|
120 |
+
gr.inputs.Dropdown(
|
121 |
+
choices=[
|
122 |
+
"alshimaa/model_baseline",
|
123 |
+
"alshimaa/model_yolo7",
|
124 |
+
#"kadirnar/yolov7-v0.1",
|
125 |
+
],
|
126 |
+
default="alshimaa/model_baseline",
|
127 |
+
label="Model",
|
128 |
+
),
|
129 |
+
],
|
130 |
+
outputs=gr.outputs.Video(type="filepath", format="mp4", label="Output Video"),
|
131 |
+
# examples=[
|
132 |
+
# ["video.mp4", 0.25, 0.45, 0, 2],
|
133 |
+
|
134 |
+
# ],
|
135 |
+
title="Smart Environmental Eye (SEE)",
|
136 |
+
cache_examples=True,
|
137 |
+
theme='huggingface',
|
138 |
+
|
139 |
+
)
|
140 |
+
|
141 |
+
if __name__ == "__main__":
|
142 |
+
gr.TabbedInterface(
|
143 |
+
[image_interface, video_interface],
|
144 |
+
["Run on Images", "Run on Videos"],
|
145 |
+
).launch()
|
146 |
+
|
147 |
+
# import subprocess
|
148 |
+
# import tempfile
|
149 |
+
# import time
|
150 |
+
# from pathlib import Path
|
151 |
+
|
152 |
+
# import cv2
|
153 |
+
# import gradio as gr
|
154 |
+
|
155 |
+
# from inferer import Inferer
|
156 |
+
|
157 |
+
# pipeline = Inferer("alshimaa/model_yolo7", device='cuda')
|
158 |
+
|
159 |
+
|
160 |
+
# def fn_image(image, conf_thres, iou_thres):
|
161 |
+
# return pipeline(image, conf_thres, iou_thres)
|
162 |
+
|
163 |
+
|
164 |
+
# def fn_video(video_file, conf_thres, iou_thres, start_sec, duration):
|
165 |
+
# start_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec))
|
166 |
+
# end_timestamp = time.strftime("%H:%M:%S", time.gmtime(start_sec + duration))
|
167 |
+
|
168 |
+
# suffix = Path(video_file).suffix
|
169 |
+
|
170 |
+
# clip_temp_file = tempfile.NamedTemporaryFile(suffix=suffix)
|
171 |
+
# subprocess.call(
|
172 |
+
# f"ffmpeg -y -ss {start_timestamp} -i {video_file} -to {end_timestamp} -c copy {clip_temp_file.name}".split()
|
173 |
+
# )
|
174 |
+
|
175 |
+
# # Reader of clip file
|
176 |
+
# cap = cv2.VideoCapture(clip_temp_file.name)
|
177 |
+
|
178 |
+
# # This is an intermediary temp file where we'll write the video to
|
179 |
+
# # Unfortunately, gradio doesn't play too nice with videos rn so we have to do some hackiness
|
180 |
+
# # with ffmpeg at the end of the function here.
|
181 |
+
# with tempfile.NamedTemporaryFile(suffix=".mp4") as temp_file:
|
182 |
+
# out = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*"MP4V"), 30, (1280, 720))
|
183 |
+
|
184 |
+
# num_frames = 0
|
185 |
+
# max_frames = duration * 30
|
186 |
+
# while cap.isOpened():
|
187 |
+
# try:
|
188 |
+
# ret, frame = cap.read()
|
189 |
+
# if not ret:
|
190 |
+
# break
|
191 |
+
# except Exception as e:
|
192 |
+
# print(e)
|
193 |
+
# continue
|
194 |
+
# print("FRAME DTYPE", type(frame))
|
195 |
+
# out.write(pipeline(frame, conf_thres, iou_thres))
|
196 |
+
# num_frames += 1
|
197 |
+
# print("Processed {} frames".format(num_frames))
|
198 |
+
# if num_frames == max_frames:
|
199 |
+
# break
|
200 |
+
|
201 |
+
# out.release()
|
202 |
+
|
203 |
+
# # Aforementioned hackiness
|
204 |
+
# out_file = tempfile.NamedTemporaryFile(suffix="out.mp4", delete=False)
|
205 |
+
# subprocess.run(f"ffmpeg -y -loglevel quiet -stats -i {temp_file.name} -c:v libx264 {out_file.name}".split())
|
206 |
+
|
207 |
+
# return out_file.name
|
208 |
+
|
209 |
+
|
210 |
+
# image_interface = gr.Interface(
|
211 |
+
# fn=fn_image,
|
212 |
+
# inputs=[
|
213 |
+
# "image",
|
214 |
+
# gr.Slider(0, 1, value=0.5, label="Confidence Threshold"),
|
215 |
+
# gr.Slider(0, 1, value=0.5, label="IOU Threshold"),
|
216 |
+
# ],
|
217 |
+
# outputs=gr.Image(type="file"),
|
218 |
+
# examples=[["image1.jpg", 0.5, 0.5], ["image2.jpg", 0.25, 0.45], ["image3.jpg", 0.25, 0.45]],
|
219 |
+
# title="Smart Environmental Eye (SEE)",
|
220 |
+
# allow_flagging=False,
|
221 |
+
# allow_screenshot=False,
|
222 |
+
# )
|
223 |
+
|
224 |
+
# video_interface = gr.Interface(
|
225 |
+
# fn=fn_video,
|
226 |
+
# inputs=[
|
227 |
+
# gr.Video(type="file"),
|
228 |
+
# gr.Slider(0, 1, value=0.25, label="Confidence Threshold"),
|
229 |
+
# gr.Slider(0, 1, value=0.45, label="IOU Threshold"),
|
230 |
+
# gr.Slider(0, 10, value=0, label="Start Second", step=1),
|
231 |
+
# gr.Slider(0, 10 if pipeline.device.type != 'cpu' else 3, value=4, label="Duration", step=1),
|
232 |
+
# ],
|
233 |
+
# outputs=gr.Video(type="file", format="mp4"),
|
234 |
+
# # examples=[
|
235 |
+
# # ["video.mp4", 0.25, 0.45, 0, 2],
|
236 |
+
|
237 |
+
# # ],
|
238 |
+
# title="Smart Environmental Eye (SEE)",
|
239 |
+
# allow_flagging=False,
|
240 |
+
# allow_screenshot=False,
|
241 |
+
# )
|
242 |
+
|
243 |
+
|
244 |
+
|
245 |
+
# if __name__ == "__main__":
|
246 |
+
# gr.TabbedInterface(
|
247 |
+
# [image_interface, video_interface],
|
248 |
+
# ["Run on Images", "Run on Videos"],
|
249 |
+
# ).launch()
|
250 |
+
|