-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (51 loc) · 1.5 KB
/
app.py
File metadata and controls
67 lines (51 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gradio as gr
import os
import cv2
from ultralytics import YOLO
model = YOLO('yolov8n.pt')
a = os.path.join(os.path.dirname(__file__), "video/test.mp4") # Test Video
def video_demo(video):
cap = cv2.VideoCapture(video)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'h264')
fps = int(cap.get(cv2.CAP_PROP_FPS))
out = cv2.VideoWriter(
os.path.join(os.path.dirname(__file__),'video/out/output.mp4'),
fourcc,
fps,
(width,height)
)
output_file = os.path.join(os.path.dirname(__file__),'video/out/output.mp4')
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLOv8 inference on the frame
results = model(frame)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# # write video
out.write(annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
cap.release()
out.release()
return output_file
demo = gr.Interface(
fn=video_demo,
inputs=gr.Video(),
outputs=gr.Video(),
examples=[
a
]
)
if __name__ == "__main__":
demo.launch(
server_name='0.0.0.0',
server_port=7862
)