-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
46 lines (35 loc) · 1.29 KB
/
app.py
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
from ultralytics import YOLO
import cv2
import pyresearch # Note: Ensure this module is installed/correct
# Load the model
model = YOLO('yolo12-seg.pt')
# Process a video
video_path = "demo.mp4" # Using 0 for webcam; replace with "demo.mp4" for a file
cap = cv2.VideoCapture(video_path)
# Get video properties
fps = int(cap.get(cv2.CAP_PROP_FPS))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Define the codec and create VideoWriter object to save the video
output_path = "output_video.mp4" # Output file name
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Perform object detection on the frame
results = model(frame)
# Get the annotated frame with detections
annotated_frame = results[0].plot()
# Write the annotated frame to the output video
out.write(annotated_frame)
# Display the results
cv2.imshow('YOLO Video Detection', annotated_frame)
# Press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
out.release() # Release the VideoWriter
cv2.destroyAllWindows()