YOLOv8 目标跟踪、车速检测、车流量统计
爱编程的鱼 2024-10-03 12:31:04 阅读 90
原文:YOLOv8 目标跟踪、车速检测、车流量统计 - 知乎 (zhihu.com)
一、目标跟踪
YOLOv8_ ByteTrack目标跟踪、模型部署_yolov8-track-CSDN博客
二、车速检测、车流量统计
YOLO 车辆测速-CSDN博客
<code>import cv2
import pandas as pd
import numpy as np
from ultralytics import YOLO
import ultralytics
from track import *
from deep_sort.deep_sort import DeepSort
import time
model = YOLO('yolov8_visdrone.pt')
# cap=cv2.VideoCapture('veh2.mp4')
cap = cv2.VideoCapture('b.mov')
# cap=cv2.VideoCapture('test_traffic.mp4')
w, h = int(cap.get(3)), int(cap.get(4))
fps = cap.get(cv2.CAP_PROP_FPS)
print("fps: ", cap.get(cv2.CAP_PROP_FPS))
# video write
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# out = cv2.VideoWriter('out.avi',fourcc, fps, (w,h))
out = cv2.VideoWriter('out2.avi',fourcc, 20, (w,h))
class_list = ['pedestrain', 'people', 'bicycle', 'car', 'van', 'truck', 'tricycle', 'awning-tricycle', 'bus', 'motor']
# print(class_list)
count = 0
tracker=Tracker()
cy1 = 222
cy2 = 368
offset = 10 # 6
vh_down = {}
counter = []
vh_up = {}
counter1 = []
while True:
ret, frame = cap.read()
if not ret:
break
count += 1
if count % 3 != 0:
continue
frame = cv2.resize(frame, (1020, 500))
mytracker = Tracker()
im, pred_boxes, bbox_id = mytracker.track(frame)
for bbox in bbox_id:
x3, y3, x4, y4, id = bbox
x3, y3, x4, y4 = int(x3), int(y3), int(x4), int(y4)
# print(x3, y3, x4, y4)
cx = int(x3 + x4) // 2
cy = int(y3 + y4) // 2
cv2.rectangle(frame,(x3,y3),(x4,y4),(0,0,255),2)
#####going DOWN#####
if cy1 < (cy + offset) and cy1 > (cy - offset):
vh_down[id] = time.time()
if id in vh_down:
if cy2 < (cy + offset) and cy2 > (cy - offset):
elapsed_time = time.time() - vh_down[id]
if counter.count(id) == 0:
counter.append(id)
distance = 10 # meters
a_speed_ms = distance / elapsed_time
a_speed_kh = a_speed_ms * 3.6
cv2.circle(frame, (cx, cy), 4, (0, 0, 255), -1)
cv2.putText(frame, str(id), (x3, y3), cv2.FONT_HERSHEY_COMPLEX, 0.6, (0, 0, 255), 1)
cv2.putText(frame, str(int(a_speed_kh)) + 'Km/h', (x4, y4), cv2.FONT_HERSHEY_COMPLEX, 0.8,
(0, 0, 255), 3)
#####going UP#####
if cy2 < (cy + offset) and cy2 > (cy - offset):
vh_up[id] = time.time()
if id in vh_up:
if cy1 < (cy + offset) and cy1 > (cy - offset):
elapsed1_time = time.time() - vh_up[id]
if counter1.count(id) == 0:
counter1.append(id)
distance1 = 10 # meters
a_speed_ms1 = distance1 / elapsed1_time
a_speed_kh1 = a_speed_ms1 * 3.6
cv2.circle(frame, (cx, cy), 4, (0, 0, 255), -1)
cv2.putText(frame, str(id), (x3, y3), cv2.FONT_HERSHEY_COMPLEX, 0.6, (0, 0, 255), 1)
cv2.putText(frame, str(int(a_speed_kh1)) + 'Km/h', (x4, y4), cv2.FONT_HERSHEY_COMPLEX, 0.8,
(0, 0, 255), 2)
cv2.line(frame, (176, cy1), (880, cy1), (0, 0, 255), 3)
cv2.putText(frame, ('L1'), (182, cy1), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
cv2.line(frame, (177, cy2), (880, cy2), (0, 0, 255), 3)
cv2.putText(frame, ('L2'), (182, cy2), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
d = (len(counter))
u = (len(counter1))
cv2.putText(frame, ('goingdown:-') + str(d), (60, 90), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
cv2.putText(frame, ('goingup:-') + str(u), (60, 130), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), 2)
cv2.imshow("RGB", frame)
frame = cv2.resize(frame, (w, h))
out.write(frame)
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
视频见原文。
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。