YOLOv5和DeepSORT的智能交通流量监测系统

一、Yolov5的环境搭建

(1)软硬件配置:

1)软件配置:

python版本(3.9.7)+pytorch版本(2.0.0+cu117)+anaconda

2)硬件配置:Window 10(64位)+Nvidia Geforce 1080ti

(2)需要安装的包:

1)依赖库,用下面命令安装:

1
pip install -r requirements.txt

2)opencv、torchreid等手动安装,直接pip install xxx即可。

(3)问题和解决方法:

1)遇到报错“UserWarning: Cython evaluation (very fast so highly recommended) is unavailable, now use python evaluation.”

解决方法:

1
pip install Cython

2)遇到报错“AttributeError: module ‘numpy‘ has no attribute ‘float‘“

解决方法:

1
pip install 更低版本的numpy(最好是和python版本对应的)

3)遇到报错no module named “torchreid”

1
不要pip install torchreid
1
pip install https://github.com/KaiyangZhou/deep-person-reid/archive/master.zip

遇到网络错误,就直接本地下载,然后pip install

遇到C++报错,如下:

1

解决方法:Visual Studio 官网地址

勾选使用C++的桌面开发这个组件再运行上面的命令就解决了。

二、代码介绍

代码地址:https://github.com/Lucki-ly/High-speed-video-detection

这里介绍修改的内容:(1)main.py;(2)webui.py

main和webui功能上差别不大,区别在于前者是做程序命令,运行完就没了;后者是一个UI界面,可以一直提交任务。

整个代码分为detect函数和一个主函数,我改的是detect函数。所以我把detect代码分为这三大块:

1)初始化代码(包括deepsort模型、数据加载器、追踪器等);

2)数据处理(预测、NMS非极大值抑制、检测结果后处理);

1、在代码的开头定义一个全局字典,用于存储识别结果和计数:

1
2
global count_dict
count_dict = {}

2、对线宽进行增大,可视化更可观。

1
annotator = Annotator(im0, line_width=10, pil=not ascii)  #标签处理

3)可视化

show_vid显示视频参数、save_vid保存视频参数和打印结果,可视化改动比较大,也是主要的任务:

1、对于show_vid显示视频参数:

a)动态显示当前检测到的每种类别的数量:

1
2
3
4
y_offset = 120
for class_name, count in count_dict.items():
cv2.putText(im0, f'{class_name}: {count}', (10, y_offset), cv2.FONT_HERSHEY_SIMPLEX, 6, (0, 255, 0), 5)
y_offset += 200

b)创建一个用于显示的缩放图像:

1
2
3
4
5
6
7
8
display_scale_percent = 25  # 调整显示窗口的缩放比例
display_width = int(im0.shape[1] * display_scale_percent / 100)
display_height = int(im0.shape[0] * display_scale_percent / 100)
display_dim = (display_width, display_height)
im0_display = cv2.resize(im0, display_dim, interpolation=cv2.INTER_AREA)

cv2.imshow(str(p), im0_display)
cv2.waitKey(1) # 1 millisecond

2、对于save_vid保存视频参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# yield imres,im
if vid_path[i] != save_path: # new video
vid_path[i] = save_path
if isinstance(vid_writer[i], cv2.VideoWriter):
vid_writer[i].release() # release previous video writer
if vid_cap: # video
fps = vid_cap.get(cv2.CAP_PROP_FPS)
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
else: # stream
fps, w, h = 30, im0.shape[1], im0.shape[0]
save_path = str(Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos
vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
vid_writer[i].write(im0)

3、删掉了Count_obj函数。

三、运行方法

1
2
cd xxx\Vedio\code\VehicleCountor
python main.py --save-vid --output \xxx\xxx\1\Vedio\code\VehicleCountor\inference\output

四、项目结果

2

3

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2024 John Doe
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信