-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
63 lines (49 loc) · 2.1 KB
/
Copy pathmain.py
File metadata and controls
63 lines (49 loc) · 2.1 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
import cv2
from ultralytics import YOLO
# 1. Load the YOLOv8 model
# (It will automatically download 'yolov8n.pt' the first time you run this)
# 'n' stands for nano - it's the fastest model, perfect for video!
model = YOLO('yolov8n.pt')
# 2. Open the video file
# Note: Ensure you have a video named 'sample_input.mp4' in the same folder.
# Tip: Change this to 0 (i.e., cv2.VideoCapture(0)) to use your webcam!
video_path = 'sample_input.mp4'
cap = cv2.VideoCapture(video_path)
# Check if the video opened successfully
if not cap.isOpened():
print("Error: Could not open video file. Check the file name and path.")
exit()
# 3. Get video properties to save the output correctly
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# Set up the VideoWriter to save the result
# 'mp4v' is the codec used to write .mp4 files
out = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
print("Processing video... A window will pop up. Press 'q' to stop early.")
# 4. The Processing Loop
while cap.isOpened():
# Read a single frame
success, frame = cap.read()
if success:
# Run YOLO object detection on that frame
# stream=True keeps it fast and memory efficient
results = model(frame, stream=True)
for result in results:
# .plot() automatically draws the bounding boxes and labels for us!
annotated_frame = result.plot()
# Write the annotated frame to our output video file
out.write(annotated_frame)
# Display the frame on your screen so you can watch it happen
cv2.imshow("YOLOv8 Object Detection", annotated_frame)
# Break the loop if the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# If success is False, the video has ended
break
# 5. Clean up! Release the camera/video and close windows.
cap.release()
out.release()
cv2.destroyAllWindows()
print("Done! Check your folder for 'output_video.mp4'")