Spaces:
Runtime error
Runtime error
dongfang2021
commited on
Commit
•
26e2b1f
1
Parent(s):
9e753bd
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import torch
|
4 |
+
|
5 |
+
def detect_objects(frame):
|
6 |
+
results = model(frame)
|
7 |
+
|
8 |
+
for *box, conf, cls in results.xyxy[0]:
|
9 |
+
label = f'{model.names[int(cls)]} {conf:.2f}'
|
10 |
+
frame = cv2.rectangle(frame, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), (255, 0, 0), 2)
|
11 |
+
frame = cv2.putText(frame, label, (int(box[0]), int(box[1]) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
12 |
+
|
13 |
+
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
14 |
+
|
15 |
+
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
|
16 |
+
video = gr.inputs.Video(type='opencv', label="输入视频")
|
17 |
+
output = gr.outputs.Image(type='pil',label="检测结果")
|
18 |
+
|
19 |
+
iface = gr.Interface(fn=detect_objects, inputs=video, outputs=output, title="目标检测")
|
20 |
+
iface.launch(share=True)
|