-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
65 lines (51 loc) · 1.71 KB
/
Copy pathmain.py
File metadata and controls
65 lines (51 loc) · 1.71 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
import cv2
import torch
import time
from ultralytics import YOLO
# Load YOLO model with GPU acceleration
model = YOLO("yolo11n.pt").to("cuda")
# Open video capture
cap = cv2.VideoCapture("trafic_video.mkv")
# Get video properties
width = 640
height = 360
fps_input = cap.get(cv2.CAP_PROP_FPS)
# Define VideoWriter to save output
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # You can also use 'XVID'
out = cv2.VideoWriter('output_video.mp4', fourcc, fps_input, (width, height))
fps = 0
frame_count = 0
start_time = time.time()
frame_skip = 1 # Process every 2nd frame
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_count += 1
# if frame_count % frame_skip != 0:
# continue # Skip frames for efficiency
frame = cv2.resize(frame, (width, height))
current_time = time.time()
elapsed_time = current_time - start_time
fps = frame_count / elapsed_time if elapsed_time > 0 else 0
# Run YOLOv8 object detection
results = model(frame, conf=0.4, iou=0.5, verbose=False)
for result in results:
for box in result.boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
conf = float(box.conf[0])
class_id = int(box.cls[0])
label = model.names[class_id]
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, f'{label} {conf:.2f}', (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Write the frame to output video
out.write(frame)
# Display the frame (optional)
cv2.imshow("Object Detection", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
out.release()
cv2.destroyAllWindows()