用opencv的dnn模块做yolov5目标检测,包含C++和Python两个版本的程序

Overview

yolov5-dnn-cpp-py

yolov5s,yolov5l,yolov5m,yolov5x的onnx文件在百度云盘下载, 链接:https://pan.baidu.com/s/1d67LUlOoPFQy0MV39gpJiw 提取码:bayj

python版本的主程序是main_yolov5.py,C++版本的主程序是main_yolo.cpp

运行整套程序只需要安装opencv库(4.0以上版本的),彻底摆脱对深度学习框架的依赖

如果你想运行生成onnx文件的程序,那么就cd到convert-onnx文件夹,在百度云盘下载yolov5s,yolov5l,yolov5m,yolov5x的.pth文件放在该目录里, 百度云盘链接: https://pan.baidu.com/s/1oIdwpp6kuasANMInTpHnrw 密码: m3n1

这4个pth文件是从https://github.com/ultralytics/yolov5 的pth文件里抽取出参数,保存到顺序字典OrderedDict里,最后生成新的pth文件 在convert-onnx文件夹里,我把4种yolov5的网络结构全都定义在.py文件里,这样便于读者直观的了解网络结构以及层与层的连接关系。 下载完成pth文件后,运行convert_onnx.py就可以生成.onnx文件,这个程序需要依赖pytorch1.7.0框架,如果pytorch版本低了,程序运行会报错。 因为在yolov5里有新的激活函数,旧版本pytorch可能不支持的

在编写这套程序时,遇到的bug和解决办法,可以阅读我的csdn博客 https://blog.csdn.net/nihate/article/details/112731327

Comments
  • TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect.

    TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect.

    运行convert_onnx.py时出现下列警告: Namespace(net_type='yolov5s') 360 360 360 360 H:\yuanbaoxi\ybx_gitee\yolov5-dnn-cpp-py\yolov5s.py:36: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! route = F.interpolate(xRt2, size=(int(xRt2.shape[2] * 2), int(xRt2.shape[3] * 2)), mode='nearest') H:\yuanbaoxi\ybx_gitee\yolov5-dnn-cpp-py\yolov5s.py:40: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs! route = F.interpolate(xRt3, size=(int(xRt3.shape[2] * 2), int(xRt3.shape[3] * 2)), mode='nearest') convert yolov5s.onnx to onnx finish!!! 然后出现下面错误: read failed

    opened by ybxbupt 10
  • run c++ main_yolo.cpp error

    run c++ main_yolo.cpp error

    OpenCV(4.0.0) Error: Requested object was not found (Blob 363 not found in const blobs) in cv::dnn::dnn4_v20180917::ONNXImporter::getBlob, file c:\build\master_winpack-build-win64-vc15\opencv\modules\dnn\src\onnx\onnx_importer.cpp, line 272

    opened by masommao 4
  • python 如何用opencv部署?

    python 如何用opencv部署?

    1.修改,因为提示参数不多,返回的只有三个参数 #bs, _, ny, nx = outs[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) bs, ny, nx = outs[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) 修改参数之后,包这个错误: ValueError: cannot reshape array of size 151200 into shape (1,3,6,25200,6)

    [INFO]blob shape: (1, 3, 640, 640) 0 (1, 25200, 6) Traceback (most recent call last): File "main_yolov5.py", line 124, in dets = yolonet.detect(srcimg) File "main_yolov5.py", line 99, in detect outs[i] = outs[i].reshape(bs, self.na, self.no, ny, nx).transpose(0, 1, 3, 4, 2) ValueError: cannot reshape array of size 151200 into shape (1,3,6,25200,6) image

    opened by xiaodingding 1
  • cpp 加载模型失败 Required argument

    cpp 加载模型失败 Required argument "starts" not found into dictionary in function 'get' 求助!!!!

    OpenCV(4.1.2) /usr/local/src/opencv/modules/dnn/include/opencv2/dnn/dnn.inl.hpp:349: error: (-204:Requested object was not found) Required argument "starts" not found into dictionary in function 'get' 完全按照大佬说的做的,但是加载失败。

    opened by Naughty-Galileo 2
  • error. can you help me plz

    error. can you help me plz

    Traceback (most recent call last): File "C:/Users/xtuye/yolov5-dnn-cpp-python/main_yolov5.py", line 114, in dets = yolonet.detect(srcimg) File "C:/Users/xtuye/yolov5-dnn-cpp-python/main_yolov5.py", line 90, in detect outs[i] = outs[i].reshape(bs, self.na, self.no, ny, nx).transpose(0, 1, 3, 4, 2) TypeError: 'tuple' object does not support item assignment

    opened by tuyeen1509 0
  •  error: (-215:Assertion failed) model_proto.has_graph() in function 'populateNet'

    error: (-215:Assertion failed) model_proto.has_graph() in function 'populateNet'

    /media/yzl/C4A8E8FBE4769FDE/sr/cpp/opencv-4.2.0/modules/dnn/src/onnx/onnx_importer.cpp:327: error: (-215:Assertion failed) model_proto.has_graph() in function 'populateNet'

    为什么会报这样的错误?

    opened by yzl1014 4
  • undefined symbol: _ZN2cv3dnn14dnn4_v202009083Net7forwardERKNS_12

    undefined symbol: _ZN2cv3dnn14dnn4_v202009083Net7forwardERKNS_12

    cmake .. make 都没报错 opencv4.5.0

    ./opencv_test_project: symbol lookup error: ./opencv_test_project: undefined symbol: _ZN2cv3dnn14dnn4_v202009083Net7forwardERKNS_12_OutputArrayERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISC_EE

    opened by zkailinzhang 1
Owner
深度学习算法工程师
null
使用OpenCV部署全景驾驶感知网络YOLOP,可同时处理交通目标检测、可驾驶区域分割、车道线检测,三项视觉感知任务,包含C++和Python两种版本的程序实现。本套程序只依赖opencv库就可以运行, 从而彻底摆脱对任何深度学习框架的依赖。

YOLOP-opencv-dnn 使用OpenCV部署全景驾驶感知网络YOLOP,可同时处理交通目标检测、可驾驶区域分割、车道线检测,三项视觉感知任务,依然是包含C++和Python两种版本的程序实现 onnx文件从百度云盘下载,链接:https://pan.baidu.com/s/1A_9cldU

null 178 Jan 7, 2023