cv2.dnn读取模型报错

在尝试使用OpenCV 4.5.1加载由torch 1.9.0导出的ONNX模型时,遇到了解析错误。错误提示表明OpenCV版本不支持该ONNX模型。尝试将torch回退到1.7.1及对应的torchvision版本,但会导致其他依赖冲突。目前尚未找到有效解决方法。
摘要由CSDN通过智能技术生成

cv2.dnn读取模型报错

D:\ProgramData\Miniconda3\python.exe D:/project/detect/face/yolov5-face-landmarks-opencv/main_new.py
[ERROR:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\dnn\src\onnx\onnx_importer.cpp (1878) cv::dnn::dnn4_v20201117::ONNXImporter::handleNode DNN/ONNX: ERROR during processing node with 2 inputs and 1 outputs: [Add]:(430)
Traceback (most recent call last):
  File "D:/project/detect/face/yolov5-face-landmarks-opencv/main_new.py", line 126, in <module>
    yolonet = yolov5(confThreshold=args.confThreshold, nmsThreshold=args.nmsThreshold, objThreshold=args.objThreshold)
  File "D:/project/detect/face/yolov5-face-landmarks-opencv/main_new.py", line 23, in __init__
    self.net = cv2.dnn.readNet(r'D:\project\detect\face\yolov5-face-master\yolov5n-face.onnx')
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\dnn\src\onnx\onnx_importer.cpp:1887: error: (-2:Unspecified error) in function 'cv::dnn::dnn4_v20201117::ONNXImporter::handleNode'
> Node [Add]:(430) parse error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-oduouqig\opencv\modules\dnn\src\onnx\onnx_importer.cpp:780: error: (-215:Assertion failed) blob_0.size == blob_1.size in function 'cv::dnn::dnn4_v20201117::ONNXImporter::handleNode'

Process finished with exit code 1
 

我这的原因:

opencv4.5.1 torch 1.9.0

torch1.9.0导出的onnx,opencv4.5.1不支持

解决方法1:

torch换1.7.1版本,torchvision 0.8.2版本,一一对应的,换版本就报错。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
可以按照以下步骤使用`cv2.dnn.readNet`加载yolov5模型: 1. 下载yolov5模型权重文件和配置文件。 2. 加载权重文件和配置文件: ```python model_weights = "yolov5s.pt" model_config = "yolov5s.yaml" net = cv2.dnn.readNet(model_weights, model_config) ``` 3. 设置预测输入尺寸: ```python net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA) net.setPreferableLayout(cv2.dnn.DNN_TARGET_CUDA_FP16) net.setInputSize(640, 640) ``` 4. 设置类别标签: ```python class_labels = ["person", "bicycle", "car", "motorcycle", "airplane", "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", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"] ``` 5. 进行预测: ```python image = cv2.imread("image.jpg") blob = cv2.dnn.blobFromImage(image, 1/255.0, (640, 640), swapRB=True, crop=False) net.setInput(blob) outputs = net.forward(net.getUnconnectedOutLayersNames()) ``` 6. 处理预测结果: ```python confidence_threshold = 0.5 nms_threshold = 0.4 for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > confidence_threshold: center_x = int(detection[0] * image.shape[1]) center_y = int(detection[1] * image.shape[0]) width = int(detection[2] * image.shape[1]) height = int(detection[3] * image.shape[0]) left = int(center_x - width / 2) top = int(center_y - height / 2) cv2.rectangle(image, (left, top), (left + width, top + height), (0, 255, 0), 2) cv2.putText(image, class_labels[class_id], (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) ``` 以上代码仅为示例,具体实现可能需要根据自己的需求进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值