使用OpenCV 4.5.1运行OpenVINO推理套件报错求助
问题:OpenCV 4.5.1加载OpenVINO模型失败报错
环境信息
- 系统:macOS Catalina 10.15.7
- 设备:2020款13寸MacBook Pro(3个雷电接口)
- OpenCV版本:4.5.1
- 使用模型:OpenVINO自带的
face-detection-adas-0001.xml和face-detection-adas-0001.bin
错误信息
cv2.error: OpenCV(4.5.1) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-yaf6rry6/opencv/modules/dnn/src/dnn.cpp:3901: error: (-2:Unspecified error) Build OpenCV with Inference Engine to enable loading models from Model Optimizer. in function 'readFromModelOptimizer'
调用代码
import cv2 as cv # 检查包路径 print(cv.__file__) print(cv.getBuildInformation()) # 加载模型 net = cv.dnn.readNet('face-detection-adas-0001.xml', 'face-detection-adas-0001.bin') # 指定目标设备(CPU) net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU) # 读取图像 frame = cv.imread('faces.jpg') # 准备输入blob blob = cv.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv.CV_8U) # 执行推理(人脸检测) net.setInput(blob) out = net.forward() # 在图像上绘制检测到的人脸 for detection in out.reshape(-1, 7): confidence = float(detection[2]) xmin = int(detection[3] * frame.shape[1]) ymin = int(detection[4] * frame.shape[0]) xmax = int(detection[5] * frame.shape[1]) ymax = int(detection[6] * frame.shape[0]) if confidence > 0.2: cv.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(0, 255, 0)) # 将图像保存为文件 cv.imwrite('output_02.png', frame)
问题分析与解决
这个错误既不是神经网络架构问题,也不是硬件设置问题,核心原因是当前安装的OpenCV未编译集成OpenVINO的Inference Engine模块。
解决步骤:
卸载现有OpenCV
执行命令:pip uninstall opencv-python安装带OpenVINO集成的OpenCV
有两种可行方式:- 安装OpenVINO Toolkit官方包,使用其自带的OpenCV版本(默认集成Inference Engine),并配置好对应环境变量。
- 从源码编译OpenCV,编译时添加
-DWITH_INF_ENGINE=ON参数,确保链接OpenVINO相关库文件。
验证集成效果
重新运行代码中的print(cv.getBuildInformation()),查看输出内容里是否包含Inference Engine: YES的标识,确认模块已成功集成。
内容的提问来源于stack exchange,提问作者Qiyu Zhong
相关产品推荐
相关产品推荐

