-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeed_est.py
40 lines (31 loc) · 1.18 KB
/
speed_est.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
from ultralytics import YOLO
from ultralytics.solutions import speed_estimation
import cv2
model = YOLO("Models\yolov8m.pt")
names = model.model.names
cap = cv2.VideoCapture("Media\sideroad.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Video writer
video_writer = cv2.VideoWriter("speed_estimation.mp4",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
line_pts = [(w//2, 50), (w//2, h)]
print(f"Line points: {line_pts[0]} and {line_pts[1]}")
# Init speed-estimation obj
speed_obj = speed_estimation.SpeedEstimator()
speed_obj.set_args(reg_pts=line_pts,
names=names,
view_img=False)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = speed_obj.estimate_speed(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()