【深度学习】Yolov8追踪从0到1, 这要是做计数啥的,简单的一批,一套工程化的代码

33 篇文章 3 订阅
19 篇文章 2 订阅


前言

在这里插入图片描述
在这里插入图片描述

用了将近2年的yolov5了,之前主要做目标检测后面,还做了yolov5的分割任务和模型转换,现在又有了新任务了。是时间玩玩新模型了。
最好的方式是看文档:https://docs.ultralytics.com/

可以看到和以前相比,yolov已经由一个目标监测的模型,变成了一个综合魔板,可以做包括物体检查、跟踪、实例分割、图像分类和姿态估计任务的绝佳选择。
在这里可以下载它的预训练模型:https://github.com/ultralytics/ultralytics/blob/main/README.zh-CN.md
比如监测:
在这里插入图片描述
分割:
在这里插入图片描述
分类:
在这里插入图片描述
姿态估计:
在这里插入图片描述

1.任务追踪

git:https://github.com/mikel-brostrom/yolov8_tracking
Real-time multi-object, segmentation and pose tracking using Yolov8 with DeepOCSORT and LightMBN

1.1 搭建环境

# yolov8 现在格高了,封装成库了
pip install ultralytics
pip install lap filterpy easydict
pip install gdown

这里没有完成安装requirement.txt上来,本身有torch环境,不是从0开始的.

在这里下载,重识别的权重:https://kaiyangzhou.github.io/deep-person-reid/MODEL_ZOO
在这里插入图片描述
放到weights文件夹下.

运行命令即可:

$ python track.py --yolo-model yolov8n.pt      # bboxes only
                                 yolov8n-seg.pt  # bboxes + segmentation masks
                                 yolov8n-pose.pt # bboxes + pose estimation

这是最简单的方式,其它的都走默认



$ python track.py --source 0 --yolo-model yolov8n.pt --img 640
                                          yolov8s.tflite
                                          yolov8m.pt
                                          yolov8l.onnx 
                                          yolov8x.pt --img 1280
                                          ...

source 0 是默认走webcam,电脑摄像头

在这里插入图片描述
用摄像头,默认是带显卡的,10ms一张图,显卡站30% 12G 3070 , 感觉还可以.
因为已经能够追踪了,只需要业务化,画一条线,就能计数了.有需要的来留言,我来实现.
也可以多来几个参数: --show --save 会保留文件,并且显示出识别情况.

在这里插入图片描述
–classes 16 17 可以过滤类别

2.跨摄像头追踪(进阶)

https://blog.csdn.net/qq_42312574/article/details/128880805
跨摄像头一直是自己想尝试的方向,终于找到了关键词:Multi-Target Multi-Camera Tracking (MTMC Tracking)
https://zhuanlan.zhihu.com/p/35391826, 罗大神的,不过年代稍微有点长,估计现在有了新sota了.

https://github.com/JunweiLiang/Object_Detection_Tracking base Tensorflow的就是跨摄像头追踪的,有效果图
https://github.com/Jason-cs18/Awesome-Multi-Camera-Network 列了好多学习资料,没代码
https://github.com/SurajDonthi/Multi-Camera-Person-Re-Identification/tree/master 这个是基于torch的2021年的
https://github.com/cw1204772/AIC2018_iamai 这个是2018年的,也有代码, 好像是追踪车辆的

3.补充

我再体验另外一个车辆计数
指引文档在这里:https://github.com/MuhammadMoinFaisal/YOLOv8-DeepSORT-Object-Tracking
验证cuda 是否好用

import torch

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)

x = torch.randn(3, 3).to(device)
y = torch.randn(3, 3).to(device)

z = x + y

print(z)

在体验另一个计算操作时,用的ref:
https://github.com/MuhammadMoinFaisal/YOLOv8-DeepSORT-Object-Tracking

执行推理的时候报错:

RuntimeError: GET was unable to find an engine to execute this computation

解决方案在这里:
https://github.com/microsoft/TaskMatrix/issues/283

降低torch版本,多半是由于torch==2.0.0,使用如下命令(这是我自己解决的方式,仅供参考):
pip install torch==1.13.0 torchvision==0.14.0 torchaudio==0.13.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
降低torch版本同时把torchvision和torchaudio版本都降低,此外,可以查看自己的torch对应的cuda版本
print(torch.version)
print(torch.version.cuda)
去官网上找匹配的torch安装

下载完成跑代码又遇到另外一个问题
aaa.py

import torch

print(torch.cuda.is_available())
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(device)

x = torch.randn(3, 3).to(device)
y = torch.randn(3, 3).to(device)

z = x + y

print(z)
(people_count) [jianming_ge@localhost detect]$ python aaa.py 
Traceback (most recent call last):
  File "aaa.py", line 1, in <module>
    import torch
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/torch/__init__.py", line 191, in <module>
    _load_global_deps()
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/torch/__init__.py", line 153, in _load_global_deps
    ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/ctypes/__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/torch/lib/../../nvidia/cublas/lib/libcublas.so.11: symbol cublasLtHSHMatmulAlgoInit, version libcublasLt.so.11 not defined in file libcublasLt.so.11 with link time reference

原因:https://stackoverflow.com/questions/74394695/how-does-one-fix-when-torch-cant-find-cuda-error-version-libcublaslt-so-11-no

Like eval said, it is because pytorch1.13 automatically install nvidia_cublas_cu11, nvidia_cuda_nvrtc_cu11, nvidia_cuda_runtime_cu11 and nvidia_cudnn_cu11. While I have my own CUDA toolKit already installed, I have the same problem.

In my case, I used pip uninstall nvidia_cublas_cu11 and solved the problem. I think the PyTorch team should solve this issue, since users often have their own CUDAtoolkit installed.

因为torch1.13 自己装了cu11, 而我们若自己也装了,就有可能能产生这个问题。
解决方案:

pip uninstall nvidia_cublas_cu11

或者装个更低版本的皆可以了

source cuda11.1
# To see Cuda version in use
nvcc -V
pip3 install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio==0.9.1 -f https://download.pytorch.org/whl/torch_stable.html

执行计数操作:

python Tracking_and_vehicle_counting.py model=yolov8l.pt source="./test3.mp4" show=True

报错

(people_count) [jianming_ge@localhost detect]$ python Tracking_and_vehicle_counting.py model=yolov8l.pt source="./test3.mp4" show=True
[2023-06-01 17:58:16,982][root.tracker][INFO] - Loading weights from deep_sort_pytorch/deep_sort/deep/checkpoint/ckpt.t7... Done!
Ultralytics YOLOv8.0.3 🚀 Python-3.8.16 torch-1.13.0+cu117 CUDA:0 (NVIDIA GeForce RTX 3080 Ti, 12054MiB)
Fusing layers... 
YOLOv8l summary: 268 layers, 43668288 parameters, 0 gradients, 165.2 GFLOPs
Error executing job with overrides: ['model=yolov8l.pt', 'source=./test3.mp4', 'show=True']
Traceback (most recent call last):
  File "Tracking_and_vehicle_counting.py", line 320, in <module>
    predict()
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/main.py", line 94, in decorated_main
    _run_hydra(
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/_internal/utils.py", line 394, in _run_hydra
    _run_app(
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/_internal/utils.py", line 457, in _run_app
    run_and_report(
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/_internal/utils.py", line 223, in run_and_report
    raise ex
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/_internal/utils.py", line 220, in run_and_report
    return func()
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/_internal/utils.py", line 458, in <lambda>
    lambda: hydra.run(
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/_internal/hydra.py", line 132, in run
    _ = ret.return_value
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/core/utils.py", line 260, in return_value
    raise self._return_value
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/hydra/core/utils.py", line 186, in run_job
    ret.return_value = task_function(task_cfg)
  File "Tracking_and_vehicle_counting.py", line 316, in predict
    predictor()
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/home/jianming_ge/workplace/zhongwaiyun/people_count/ultralytics/yolo/engine/predictor.py", line 189, in __call__
    s += self.write_results(i, preds, (p, im, im0s))
  File "Tracking_and_vehicle_counting.py", line 298, in write_results
    outputs = deepsort.update(xywhs, confss, oids, im0)
  File "/home/jianming_ge/workplace/zhongwaiyun/people_count/ultralytics/yolo/v8/detect/deep_sort_pytorch/deep_sort/deep_sort.py", line 31, in update
    detections = [Detection(bbox_tlwh[i], conf, features[i],oid) for i, (conf,oid) in enumerate(zip(confidences,oids)) if conf > self.min_confidence]
  File "/home/jianming_ge/workplace/zhongwaiyun/people_count/ultralytics/yolo/v8/detect/deep_sort_pytorch/deep_sort/deep_sort.py", line 31, in <listcomp>
    detections = [Detection(bbox_tlwh[i], conf, features[i],oid) for i, (conf,oid) in enumerate(zip(confidences,oids)) if conf > self.min_confidence]
  File "/home/jianming_ge/workplace/zhongwaiyun/people_count/ultralytics/yolo/v8/detect/deep_sort_pytorch/deep_sort/sort/detection.py", line 30, in __init__
    self.tlwh = np.asarray(tlwh, dtype=np.float)
  File "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/numpy/__init__.py", line 305, in __getattr__
    raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'float'.
`np.float` was a deprecated alias for the builtin `float`. To avoid this error in existing code, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

把detection.py", line 30 np.float 改成 np.float64 即可

在linux 黑窗口下还会报另外一个错误,因为源码是打开一个窗口,车过线+1 那种的。黑窗口没有这些。

(people_count) [jianming_ge@localhost detect]$ python Tracking_and_vehicle_counting.py model=yolov8l.pt source="./test3.mp4" show=True
[2023-06-01 18:00:29,024][root.tracker][INFO] - Loading weights from deep_sort_pytorch/deep_sort/deep/checkpoint/ckpt.t7... Done!
Ultralytics YOLOv8.0.3 🚀 Python-3.8.16 torch-1.13.0+cu117 CUDA:0 (NVIDIA GeForce RTX 3080 Ti, 12054MiB)
Fusing layers... 
YOLOv8l summary: 268 layers, 43668288 parameters, 0 gradients, 165.2 GFLOPs

qt.qpa.xcb: could not connect to display 172.18.19.191:0.0
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/jianming_ge/miniconda3/envs/people_count/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: xcb.

解决方案:

# centos
sudo yum install xorg-x11-server-Xorg -y
sudo yum install xorg-x11-apps
# ubuntu
apt-get install xorg
sudo apt-get install x11-apps

(people_count) [jianming_ge@localhost detect]$ echo D I S P L A Y 172.18.19.191 : 0.0 v i m / e t c / p r o f i l e ( p e o p l e c o u n t ) [ j i a n m i n g g e @ l o c a l h o s t d e t e c t ] DISPLAY 172.18.19.191:0.0 vim /etc/profile (people_count) [jianming_ge@localhost detect] DISPLAY172.18.19.191:0.0vim/etc/profile(peoplecount)[jianmingge@localhostdetect] sudo vim /etc/profile
(people_count) [jianming_ge@localhost detect]$ source /etc/profile
改成自己的
运行:
xclock 会出现一个小钟表,代表成功了
在这里插入图片描述
在这里插入图片描述

跑起来了,哈哈哈哈:
在这里插入图片描述
在这里插入图片描述
cd /home/jianming_ge/workplace/zhongwaiyun/people_count/ultralytics/yolo/v8/detect
执行:python Tracking_and_vehicle_counting.py model=yolov8l.pt source=“./test3.mp4” show=True
就可以得到如上的画面。哈哈哈哈,成功了

4.将3补充内容业务化

也就是每隔一段时间推送一条消息到业务平台做计数功能。

------------------------20230602 分割线
追踪了一下代码,没有发现行人计数的修改地方,已经提issue给作者了
https://github.com/MuhammadMoinFaisal/YOLOv8-DeepSORT-Object-Tracking/issues/23

总结

yolov8 作者大佬解析yolov8的内容
https://www.bilibili.com/video/BV17D4y1N7Zz/?spm_id_from=333.337.search-card.all.click&vd_source=3f7ae4b9d3a2d84bf24ff25f3294d107

  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
智慧交通基于YOLOv8的行人车辆检测计数系统源码(部署教程+训练好的模型+各项评估指标曲线).zip 平均准确率:0.91 类别:person、truck、car、bus、traffic light 【资源介绍】 1、ultralytics-main ultralytics-main为YOLOv8源代码,里面涵盖基于yolov8分类、目标检测额、姿态估计、图像分割四部分代码,我们使用的是detect部分,也就是目标检测代码 2、搭建环境 安装anaconda 和 pycharm windows系统、mac系统、Linux系统都适配 在anaconda中新建一个新的envs虚拟空间(可以参考博客来),命令窗口执行:conda create -n YOLOv8 python==3.8 创建完YOLOv8-GUI虚拟空间后,命令窗口执行:source activate YOLOv8 激活虚拟空间 然后就在YOLOv8虚拟空间内安装requirements.txt中的所有安装包,命令窗口执行:pip install -r requirements.txt 使用清华源安装更快 3、训练模型过程 进入到\ultralytics-main\ultralytics\yolo\v8\detect\文件夹下,datasets即为我们需要准备好的数据集,训练其他模型同理。 data文件夹下的bicycle.yaml文件为数据集配置文件,该文件为本人训练自行车检测模型时创建,训练其他模型,可自行创建。博文有介绍https://blog.csdn.net/DeepLearning_?spm=1011.2415.3001.5343 train.py中238行,修改为data = cfg.data or './bicycle.yaml' # or yolo.ClassificationDataset("mnist") 237行修改自己使用的预训练模型 若自己有显卡,修改239行,如我有四张显卡,即改成args = dict(model=model, data=data, device=”0,1,2,3“) 以上配置完成后运行train.py开始训练模型,训练完毕后会在runs/detect/文件夹下生成train*文件夹,里面包含模型和评估指标等 4、推理测试 训练好模型,打开predict.py,修改87行,model = cfg.model or 'yolov8n.pt',把yolov8n.pt换成我们刚才训练完生成的模型路径(在\ultralytics-main\ultralytics\yolo\v8\detect\runs\detect文件夹下),待测试的图片或者视频存放于ultralytics\ultralytics\assets文件夹, 运行predict.py即可,检测结果会在runs/detect/train文件夹下生成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值