Create app-vid.py
Browse files- app-vid.py +57 -0
app-vid.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
## https://github.com/BlueMirrors/Yolov5-ONNX/blob/main/detect.py
|
2 |
+
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from cvu.detector.yolov5 import Yolov5 as Yolov5Onnx
|
6 |
+
from vidsz.opencv import Reader, Writer
|
7 |
+
|
8 |
+
def process_input(input_file, weight='./jtbz_opt.onnx', device='cpu'):
|
9 |
+
# Determine if the input is an image or a video
|
10 |
+
if input_file.name.endswith(('.jpg', '.jpeg', '.png')):
|
11 |
+
# load model
|
12 |
+
model = Yolov5Onnx(classes="coco",
|
13 |
+
backend="onnx",
|
14 |
+
weight=weight,
|
15 |
+
device=device)
|
16 |
+
|
17 |
+
# read image
|
18 |
+
image = cv2.imread(input_file.name)
|
19 |
+
|
20 |
+
# inference
|
21 |
+
preds = model(image)
|
22 |
+
|
23 |
+
# draw image
|
24 |
+
preds.draw(image)
|
25 |
+
|
26 |
+
# return image
|
27 |
+
return image
|
28 |
+
|
29 |
+
else:
|
30 |
+
model = Yolov5Onnx(classes="coco",
|
31 |
+
backend="onnx",
|
32 |
+
weight=weight,
|
33 |
+
device=device)
|
34 |
+
reader = Reader(input_file.name)
|
35 |
+
writer = Writer(reader, name="output.mp4")
|
36 |
+
|
37 |
+
# warmup
|
38 |
+
warmup = np.random.randint(0, 255, reader.read().shape).astype("float")
|
39 |
+
for i in range(100):
|
40 |
+
model(warmup)
|
41 |
+
|
42 |
+
inference_time = 0
|
43 |
+
for frame in reader:
|
44 |
+
# inference
|
45 |
+
start = time.time()
|
46 |
+
preds = model(frame)
|
47 |
+
inference_time += time.time() - start
|
48 |
+
|
49 |
+
# draw on frame
|
50 |
+
preds.draw(frame)
|
51 |
+
writer.write(frame)
|
52 |
+
|
53 |
+
writer.release()
|
54 |
+
reader.release()
|
55 |
+
|
56 |
+
# return video
|
57 |
+
return gr.outputs.Video("output.mp4")
|