python traffic detection traffic statistics vehicle count yolov5 deepsort traffic detection

python traffic detection traffic statistics vehicle count yolov5 deepsort traffic detection

Based on the previous yolo+deepsort, change the person category to the vehicle category, because there are several vehicle categories in the coco data set [car, bus, truck], so they must be saved.

First, let's take a look at the initial effect of yolov5+deepsort's vehicle tracking, look at the dense boxes and ids, and think about how to design these rules for statistics.


labelsPath = "./yolo-coco/coco.names"
LABELS = open(labelsPath).read().strip().split("\n")

np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(200, 3),dtype="uint8")

weightsPath = "./yolo-coco/yolov3.weights"
configPath = "./yolo-coco/yolov3.cfg"

net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net.getLayerNames()
ln = [ln[i - 1] for i in net.getUnconnectedOutLayers()]

# ----------------------------------------------------------------------------------------------------------------------
"""
# cv2.VideoCapture() --- 里面参数如果地址,打开视频文件 --- 里面参数是0/1,打开摄像头
# 当参数是0的时候,打开计算机的内置摄像头,当参数为1的时候打开计算机的外置摄像头
# (W, H) = (None, None) --- 视频的宽度和高度,初始化视频编写器(writer)和帧尺寸

"""
# ----------------------------------------------------------------------------------------------------------------------

vs = cv2.VideoCapture('./input/123.mp4')
(W, H) = (None, None)

# ----------------------------------------------------------------------------------------------------------------------
"""
# try to determine the total number of frames in the video file
# 打开一个指向视频文件的文件指针,循环读取帧 --- 尝试确定视频文件中的总帧数(total),以便估计整个视频的处理时间;
# CV_CAP_PROP_FRAME_COUNT --- 视频的帧数
# 这里使用是处理视频的时候固定的过程,不必过度的纠结其使用 ---
#					   if imutils.is_cv2():
#					   	  prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT
#					   else:
#					   	  prop = cv2.CAP_PROP_FRAME_COUNT
#
# vs.get(prop) --- cv2.VideoCapture.get(prop) --- 得到视频的总帧数 
# print("[INFO] {} total frames in video".format(total)) --- 输出视频的帧数

"""
# ----------------------------------------------------------------------------------------------------------------------
try:
    prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() \
        else cv2.CAP_PROP_FRAME_COUNT
    total = int(vs.get(prop))
    print("[INFO] {} total frames in video".format(total))

except:
    print("[INFO] could not determine # of frames in video")
    print("[INFO] no approx. completion time can be provided")
    total = -1

while True:
    # ----------------------------------------------------------------------------------------------------------------------

    """
    # cv2.VideoCapture.read() ---> 读取视频,在while中循环读取视频的frame
    # vs.read() ---> 得到两个参数,其中ret是布尔值,如果读取帧是正确的则返回True,
    # 如果文件读取到结尾,它的返回值就为False。frame就是每一帧的图像,是个三维矩阵。
    # 第一个参数为False的时候,if not grabbed --- True --- 循环结束,

    """
    (grabbed, frame) = vs.read()

    if not grabbed:
        break

    if W is None or H is None:
        (H, W) = frame.shape[:2]


 

python traffic flow detection two-way traffic counting

 python traffic detection traffic statistics vehicle count yolov5deepsort traffic detection - machine learning documentation resources - CSDN download

Guess you like

Origin blog.csdn.net/babyai996/article/details/123840302