YOLOv8的汽车车流量统计计数系统 pytorch 车流量统计 多目标跟踪算法 COCO2017数据集 深度学习

博主介绍:✌全网粉丝10W+,前互联网大厂软件研发、集结硕博英豪成立工作室。专注于计算机相关专业项目实战6年之久,选择我们就是选择放心、选择安心毕业✌
> 🍅想要获取完整文章或者源码,或者代做,拉到文章底部即可与我联系了。🍅

点击查看作者主页,了解更多项目!

🍅感兴趣的可以先收藏起来,点赞、关注不迷路,大家在毕设选题,项目以及论文编写等相关问题都可以给我留言咨询,希望帮助同学们顺利毕业 。🍅

1、毕业设计:2026年计算机专业毕业设计选题汇总(建议收藏)✅

2、大数据毕业设计:2026年选题大全 深度学习 python语言 JAVA语言 hadoop和spark(建议收藏)✅

🍅感兴趣的可以先收藏起来,点赞、关注不迷路,大家在毕设选题,项目以及论文编写等相关问题都可以给我留言咨询,希望帮助同学们顺利毕业 。🍅

1、项目介绍

技术栈:
Python语言、COCO2017数据集、YOLOv8模型、pytorch、车流量统计、多目标跟踪算法

2、项目界面

(1)车流量统计,下图为2辆汽车通过红色检测线

在这里插入图片描述

(2)车流量统计,下图为已经有10辆汽车通过红色检测线

在这里插入图片描述

(3)车流量统计,下图为有45辆汽车通过红色检测线

在这里插入图片描述

3、项目说明

基于YOLOv8的汽车车流量统计系统是一种利用深度学习技术进行车辆检测和统计的系统。YOLOv8是一种目标检测模型,能够准确地识别图像中的多个车辆并提供车辆的位置和分类信息。以下是该系统的介绍:
数据采集:系统通过摄像头或视频源采集道路上的实时图像或视频。这些数据将作为模型训练和车流量统计的输入。
深度学习模型:系统使用YOLOv8深度学习模型进行车辆检测。YOLOv8模型具有高准确率和实时性能,能够在监测视频中实时地检测和识别车辆。
车流量统计:系统通过分析检测到的车辆,可以计算车流量、车速以及车道占有率等信息。这些统计数据可以用于交通管理、道路规划和安全评估等应用。
可视化界面:系统还可以提供一个可视化界面,用于实时显示车流量统计结果和图像/视频展示。用户可以通过界面查看和分析监测到的车辆数据。
系统优化:为了提高系统的实时性能,可能需要在硬件端使用加速器(如GPU)或在软件端进行模型优化和推理加速。
该系统的优点包括准确性高、实时性强、功能全面等。它在城市交通管理、道路监控和交通安全等领域具有广泛的应用前景。然而,系统的性能也受到数据质量、环境条件和模型训练等因素的影响,需要进行充分的测试和调优,以确保其在实际应用中的可靠性和稳定性。

二、功能
  使用YOLOv8模型和对象跟踪框架(abewley/sort)从视频中检测和跟踪汽车在感兴趣的区域进行检测和跟踪
YOLOv8是最新的最先进的YOLO模型,可用于对象检测、图像分类和实例分割任务
SORT是基于基本数据关联和状态估计技术的视觉多对象跟踪框架的基本实现

环境:Python3.10、torch、Pycharm

三、汽车车流量统计系统

四. 总结
  基于YOLOv8的汽车车流量统计系统是一种具有较高实时性和准确率的监测系统,能够精确地检测和识别道路上的车辆,并提供可靠的车流量统计信息。
综上所述,基于YOLOv8的汽车车流量统计系统是一种强大的监测工具,能够有效地提高交通管理和安全性,但在实际应用过程中仍需要进行测试和优化以确保其在实际应用中的可靠性和稳定性。

4、核心代码

# -*- encoding: utf-8 -*-




import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
from ultralytics import YOLO
# from ultralytics.tracker.trackers.byte_tracker import BYTETracker
import cv2
import cvzone
import math
from sort import *
import numpy as np

# 设置识别视频的文件路径
cap = cv2.VideoCapture("../Videos/test1.mp4")
# cap = cv2.VideoCapture("../Videos/test2.mp4")

#yolo原始模型可以识别的共计80个类别物体
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "potted plant", "bed",
              "dining table", "toilet", "tv monitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"]

model = YOLO("../Yolo-weights/yolov8n.pt")
mask = cv2.imread("mask.png")

tracker = Sort(max_age=20, min_hits=3, iou_threshold=0.3)

#设置统计红线的坐标(400,297)(673,297)
# 设置一条线段,起始点的坐标是(400, 297),终点的坐标是(673, 297)。
lines_coordinates = [400, 297, 673, 297]     # test1.mp4的设置
# lines_coordinates = [200, 457, 560, 457]   # test2.mp4的设置


totalCount = 0
visited_id_list = []
while True:
    success, img = cap.read()
    imgRegion = cv2.bitwise_and(mask, img)  # Will give the required region for counting
    results = model(imgRegion, stream=True)
    #读取graphics.png图片,包括alpha通道
    imgGraphics = cv2.imread("graphics.png", cv2.IMREAD_UNCHANGED)
    #将imgGraphics图片叠放在img图片的左上角,坐标(0,0)代表左上方
    img = cvzone.overlayPNG(img, imgGraphics, (0, 0))
    detections = np.empty((0, 5))
    for r in results:
        boxes = r.boxes
        for box in boxes:
            # 画框
            x1, y1, x2, y2 = box.xyxy[0]  # Gives coordinates to draw bounding box
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
            # cv2.rectangle(img, (x1, y1), (x2, y2), (255, 255, 0), 3)
            w, h = x2 - x1, y2 - y1
            bbox = (x1, y1, w, h)

            # 检测物体的置信度
            confidence_level = math.ceil((box.conf[0] * 100)) / 100

            # To get class name
            cls = int(box.cls[0])  # box.cls[0] will give us the class id(float so we need to typecast it) and with
            # the help of the id will get the class name from classNames list
            currentClass = classNames[cls]
            if currentClass == "car" or currentClass == "motorbike" or currentClass == "truck" or currentClass == "person" or currentClass == "bicycle" or \
                    currentClass == "bus" and confidence_level > 0.3:
                currentArray = np.array([x1, y1, x2, y2, confidence_level])  # Check update function in sort.py
                detections = np.vstack((detections, currentArray))
    resultTracker = tracker.update(detections)
    cv2.line(img, (lines_coordinates[0], lines_coordinates[1]), (lines_coordinates[2], lines_coordinates[3]),
             (0, 0, 255), thickness=2)

    for result in resultTracker:
        x1, y1, x2, y2, id = result
        x1, y1, x2, y2, id = int(x1), int(y1), int(x2), int(y2), int(id)
        w, h = x2 - x1, y2 - y1
        # print(result)
        cvzone.putTextRect(img, f'{id}', (max(0, x1), max(35, y1)), scale=2.3,
                           thickness=2, offset=3)
        cvzone.cornerRect(img, (x1, y1, w, h), l=9, t=3, rt=2, colorR=(255, 0, 255))

        # Now, we want the center point of the detected object and if it touches the line then increase the count
        cx, cy = x1 + w // 2, y1 + h // 2
        cv2.circle(img, (cx, cy), 4, (255, 0, 255), cv2.FILLED)

        if lines_coordinates[0] < cx < lines_coordinates[2] and lines_coordinates[1] - 10 < cy < lines_coordinates[
            1] + 10:
            if id not in visited_id_list:  # To count the number only once
                totalCount = totalCount + 1
                visited_id_list.append(id)
                cv2.line(img, (lines_coordinates[0], lines_coordinates[1]),
                         (lines_coordinates[2], lines_coordinates[3]),
                         (0, 255, 0), thickness=2)
    # cvzone.putTextRect(img, f'Count {totalCount}', (40, 40), colorT=(255, 255, 255), colorR=(0, 0, 0))
    cv2.putText(img, str(totalCount), (255, 100), cv2.FONT_HERSHEY_PLAIN, 5, (0, 0, 0), 8)

    cv2.imshow("Image", img)
    # cv2.imshow("Region", imgRegion)
    cv2.waitKey(1)

5、源码获取方式

🍅由于篇幅限制,获取完整文章或源码、代做项目的,查看【用户名】、【专栏名称】就可以找到我啦🍅

感兴趣的可以先收藏起来,点赞、关注不迷路,下方查看👇🏻获取联系方式👇🏻

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值