博主介绍:✌全网粉丝50W+,前互联网大厂软件研发、集结硕博英豪成立软件开发工作室,专注于计算机相关专业项目实战6年之久,累计开发项目作品上万套。凭借丰富的经验与专业实力,已帮助成千上万的学生顺利毕业,选择我们,就是选择放心、选择安心毕业✌
> 🍅想要获取完整文章或者源码,或者代做,拉到文章底部即可与我联系了。🍅

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

2、大数据、计算机专业选题(Python/Java/大数据/深度学习/机器学习)(建议收藏)✅

1、项目介绍

技术栈:
python语言、YOLOv8、汽车速度检测与跟踪、车辆超速检测、深度学习、PyQt5
功能:
(1)车辆检测跟踪:车辆检测识别、车辆跟踪
(2)超速检测:速度计算、超速截图、语音报警、数据库记录

本系统设置直行速度阈值为60千米每小时,转弯速度阈值为30千米每小时。

2、项目界面

(1)车辆超速检测显示为红色
在这里插入图片描述

(2)车辆超速检测显示为红色

在这里插入图片描述

(3)车辆速度检测与跟踪
在这里插入图片描述

(4)车辆速度检测与跟踪
在这里插入图片描述

(5)车辆速度检测与跟踪
在这里插入图片描述

3、项目说明

随着城市化进程的加速和机动车辆数量的激增,传统的交通管理策略已难以满足当前的需求,尤其是在应对日益严重的交通拥堵和事故频发问题上。基于深度学习的智能交通监控技术显示出巨大的潜力。本研究旨在设计并实现一个基于YOLOv8算法的汽车速度检测系统,主要工作内容如下:
(1)技术评估与算法选择。对现有的基于深度学习的目标检测技术进行了全面评估。通过深入分析YOLOv5和YOLOv8这两种先进的目标检测算法,对比了它们的平均精度,并基于此选择了YOLOv8算法。鉴于YOLOv8在目标跟踪方面的局限性,研究引入了IOU-Tracker算法,以增强系统对目标车辆的追踪能力,从而为精确的速度检测打下了坚实的基础。
(2)数据集构建与模型训练。在模型训练阶段,保留了800张满足算法要求的汽车图片。这些图片经过标注工具labellmg的精确标注,转化为包含位置和分类信息的数据集,为YOLOv8模型的训练提供了必要的Ground-truth。
(3)系统设计与实现。设计并实现了一个综合性的汽车速度检测系统,该系统不仅能够实时监测道路上的车辆速度,还能够对超速行为进行及时的检测、截图记录、报警通知以及数据存储。
(4)系统测试与结果分析。在系统测试阶段,对YOLOv8与IOU-Tracker相结合的算法模型进行了验证,以确保其在视频车辆识别和检测跟踪方面的准确性和可行性。测试结果表明,系统能够高效地实时显示目标车辆的跟踪轨迹和速度测量情况,并且还能够导出详细的检测结果和截图文件,为进一步的分析和处理提供了支持。

关键词:交通管控;目标检测;YOLOv8算法;速度检测

4、核心代码


import cv2
from collections import  defaultdict
import numpy as np
from time import time
import time as tim

from PyQt5.QtGui import QImage, QPixmap
from ultralytics.utils.plotting import Annotator, colors
from utils.playMusic import *
from utils.dataBaseService import DataBaseService,ChaoSu


settings = {
    "MYSQL_HOST":"localhost",
    "MYSQL_DBNAME":"cardatabase",
    "MYSQL_USER":"root",
    "MYSQL_PASSWD":"123456",
    "MYSQL_CHARSET":"utf8",
    "MYSQL_PORT":3306
}
db = DataBaseService.connect(settings)
PlayMusic.music_path='sound'
old_track_ids=[]
im0 = None     #原始图像
annotator = None    #注解器
#区域信息
reg_pts = [(20,400),(1260,400)] #速度计算区域点
region_thickness = 3  #区域线条厚度
#预测/跟踪信息
clss = None #类别
boxes = None #边界框
trk_ids = None #跟踪id
trk_pts = None  #跟踪点
trk_history = defaultdict(list)  #跟踪历史
#速度统计信息
current_time = 0  #当前时间
dist_data = {} #速度数据
trk_idslist = [] #跟踪ID列表
spdl_dist_thresh = 10  #欧式距离阈值
trk_previous_times={}  #先前时间的记录
trk_previous_points={}  #先前点的记录
chaosu=[]  #超速记载
def car(img,results,combo_box,track_history,width,height):

    global old_track_ids
    global chaosu
    global db
    bbox=results[0].boxes.data.tolist()  # 点的位置
    estimate_speed(img, results, width, height)   # 预估速度算法
    if results[0].boxes.id is not None:
        track_ids = results[0].boxes.id.int().tolist()
        for id1 in old_track_ids:
            if id1 not in track_ids:
                for i in range(combo_box.count()):
                    if str(combo_box.itemText(i)) == str(id1):
                        combo_box.removeItem(i)
                        break  # 追踪单个目标


        for id2 in track_ids:
            if id2 not in old_track_ids:
                combo_box.addItem(str(id2))
        old_track_ids = track_ids
    preprocess = results[0].speed['preprocess']
    inference = results[0].speed['inference']
    postprocess = results[0].speed['postprocess' ]

    FPS=int(1000/(preprocess+postprocess+inference))    #测fps   (preprocess+postprocess+inference)
    speed=0
    s=0
    re_x=0
    re_y=0

    id = 0

    if bbox != []:
        for temp in bbox:
            if len(temp) == 7:    #=7说明追踪到了
                score = temp[5]
                cls = temp[6]
                id = int(temp[4])

            else:
                score = temp[4]
                cls = temp[5]

            if combo_box.currentText() == str(id):  # 当前框选的是单个车辆
                speed = int(dist_data[id]/150) if id in dist_data else "未过速度检测线"
                re_x = (int(temp[0]) + int(temp[2]))/2  # 中心点x坐标
                re_y = (int(temp[1]) + int(temp[3]))/2  # 中心点y坐标
                s = round(score, 2)

            if combo_box.currentText() == str(id) or combo_box.currentText() == "全体车辆":
                if id in dist_data:
                    if int(dist_data[id]/150)>60:
                        cv2.rectangle(img,(int(temp[0]),int(temp[1])),(int(temp[2]),int(temp[3])),
                                           (0,0,255),2)  #左上角和右下角的位置
                        img = cv2.putText(img,"id:"+str(int(id)) + " " + "class:" + str(
                        "car") + " " + "speed:" + f"{int(dist_data[id]/150)}km/ph",
                                      (int(temp[0]),int(temp[1]) - 5),
                                       cv2.FONT_HERSHEY_SIMPLEX,0.5,(105,237,249),1)

                        if id not in chaosu:
                            PlayMusic.chaosu()
                            localtime = tim.localtime(tim.time())
                            bjtime = str(localtime[0]) + '年' +str(localtime[1])+'月'+str(
                                localtime[2]) + '日' +str(
                                localtime[3]) + '时' +str(localtime[4]) + '分' +str(localtime[5]) + '秒'
                            local = r'output\%sid%s.jpg' % (bjtime,str(id))    #超速的年月日
                            cv2.imencode('.jpg',img)[1].tofile(r'output\%sid%s.jpg' % (bjtime,str(id)))#写图片
                            data =ChaoSu() # 调用数据库算法
                            data.num = id
                            data.speed = int(dist_data[id] / 150)
                            data.time = tim.strftime("%Y-%m-%d %H:%M:%S",tim.localtime())
                            data.local = str(local)
                            db.insertToList(data)
                            chaosu.append(id) # 只截一张图

                    else:
                        cv2.rectangle(img, (int(temp[0]), int(temp[1])), (int(temp[2]), int(temp[3])),
                                      (0, 255, 0), 2)
                        img = cv2.putText(img, "id:" + str(int(id)) + " " + "class:" + str("car") + " " + "speed:"+ f"{int(dist_data[id]/150)}km/ph",      # f"{int(dist_data[id]/3)}km/ph"
                                          (int(temp[0]), int(temp[1]) - 5),
                                          cv2.FONT_HERSHEY_SIMPLEX, 0.5, (105, 237, 249), 1)

                else:
                    cv2.rectangle(img, (int(temp[0]), int(temp[1])), (int(temp[2]), int(temp[3])),
                                  (0, 255, 0), 2)
                    img = cv2.putText(img, "id:" + str(int(id)) + " " + "class:" + str("car") + " " +str(round(score,2)),
                                      (int(temp[0]), int(temp[1]) - 5),
                                      cv2.FONT_HERSHEY_SIMPLEX, 0.5, (105, 237, 249), 1)
        box_xy = results[0].boxes.xywh

# 画轨迹
        if results[0].boxes.id is not None:
            track_ids = results[0].boxes.id.int().tolist()
            for box,track_id in zip(box_xy,track_ids):
                if combo_box.currentText() == str(track_id) or combo_box.currentText() == "全体车辆":
                    x,y,w,h = box
                    track =track_history[track_id]
                    track.append((float(x),float(y)))
                    if len(track)>15:
                        track.pop(0)
                    points =  np.hstack(track).astype(np.int32).reshape((-1,1,2))
                    cv2.polylines(img,[points],isClosed=False,color=(0,255,0),thickness=10)   # 画线
    return img,id,speed,s,int(re_x),int(re_y),FPS


#提取跟踪信息的方法
def extract_tracks( tracks):
    global boxes
    global trk_ids
    global clss
    boxes = tracks[0].boxes.xyxy.cpu()  # 边界框
    clss = tracks[0].boxes.cls.cpu().tolist()   # 类别
    trk_ids = tracks[0].boxes.id.int().cpu().tolist()   # 跟踪ID

def store_track_info(track_id, box):
    global trk_history
    global trk_pts
    # 存储追踪信息
    track = trk_history[track_id]    # 存在历史的点位,形成追踪的线条
    bbox_center = (float((box[0]+box[2]) / 2), float((box[1]+box[3])/2))  # 中心点的位置
    track.append(bbox_center)

    if len(track) > 30:
        track.pop(0)

    trk_pts = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
    return track

def calculate_speed(trk_id,track):
    global reg_pts
    global spdl_dist_thresh
    global trk_previous_times
    global trk_idslis
    global trk_previous_points
    global dist_data

    if not reg_pts[0][1] < track[-1][1] < reg_pts[1][1]:   # 基准线
        return
    if reg_pts[1][0] - spdl_dist_thresh < track[-1][0] < reg_pts[1][0] + spdl_dist_thresh:
        direction = "known"
    elif reg_pts[0][0] - spdl_dist_thresh < track[-1][0] < reg_pts[0][0] + spdl_dist_thresh:
        direction = "known"
    else:
        direction = "unknown"   # 方向

    if trk_previous_times[trk_id] != 0 and direction != "unknown" and trk_id not in trk_idslist:
        trk_idslist.append(trk_id)

        time_difference = time() - trk_previous_times[trk_id]
        if time_difference>0:
            dist_difference = np.abs(track[-1][0] - trk_previous_points[trk_id][1])
            speed = dist_difference / time_difference    # 速度计算
            dist_data[trk_id] = speed


    trk_previous_times[trk_id] = time()
    trk_previous_points[trk_id] = track[-1]


#  存储追踪信息的方法
def estimate_speed( im0, tracks, width, height):
    global reg_pts
    global boxes
    global trk_ids
    global clss
    global trk_previous_times
    # 计算基于跟踪数据的物体速度
    reg_pts = [(width/2, 0), (width/2, height)]   # 阈值线,计算物体的速度
    im0 =im0
    if tracks[0].boxes.id is not None:
        extract_tracks(tracks)
        annotator = Annotator(im0, line_width=2)
        annotator.draw_region(reg_pts=reg_pts, color=(255, 0, 0), thickness=region_thickness)
        for box, trk_id, cls in zip(boxes, trk_ids, clss):
            track = store_track_info(trk_id, box)
            if trk_id not in trk_previous_times:
                trk_previous_times[trk_id] = 0
            calculate_speed(trk_id, track)

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

5、源码获取方式

🍅由于篇幅限制,获取完整文章或源码、代做项目的,拉到文章底部即可看到个人联系方式。🍅

点赞、收藏、关注,不迷路,下方查看👇🏻获取联系方式👇🏻

Logo

码道开发者社区,聚焦华为云码道 CodeArts 代码智能体,沉淀 Agent、Skill、鸿蒙开发实战内容,供开发者查阅资料、交流技术、分享工程实践

更多推荐