【问题标题】:OpenVINO: How to build OpenCV with Inference Engine to enable loading models from Model OptimizerOpenVINO:如何使用推理引擎构建 OpenCV 以启用从模型优化器加载模型
【发布时间】:2019-07-12 12:26:19
【问题描述】:

我安装了 OpenVINO,想在 Windows 10 上运行以下代码。

import numpy as np
import cv2
import sys

from get_face_id import face_id_getter
from check import check
from win10toast import ToastNotifier 

FP = 32

targetId = 0
toaster = None

if '-use_notifications' in sys.argv:
    toaster = ToastNotifier() 

if len(sys.argv) > 1 and '-m' in sys.argv:
    FP =  16
    targetId = cv2.dnn.DNN_TARGET_MYRIAD



cap = cv2.VideoCapture(0)

getter = None

if '-get_face_id' in sys.argv:
    getter = face_id_getter()

weights = 'face-detection-adas-0001/FP{}/face-detection-adas-0001.bin'.format(FP)
config = 'face-detection-adas-0001/FP{}/face-detection-adas-0001.xml'.format(FP)

weights_emotions, config_emotions, emotions = None, None, None



if len(sys.argv) > 1 and '-use_emotions' in sys.argv:
    weights_emotions = 'emotions-recognition-retail-0003/FP{}/emotions-recognition-retail-0003.bin'.format(FP)
    config_emotions = 'emotions-recognition-retail-0003/FP{}/emotions-recognition-retail-0003.xml'.format(FP)
framework = 'DLDT'

model = cv2.dnn.readNet(weights, config, framework)
model.setPreferableTarget(targetId=targetId)

if len(sys.argv) > 1 and '-use_emotions' in sys.argv:
    emotions = cv2.dnn.readNet(weights_emotions, config_emotions, framework)
    emotions.setPreferableTarget(targetId=targetId)

emotions_decode = ('neutral', 'happy', 'sad', 'surprise', 'anger')

names = ["Plotnikov Egor", "Vainberg Roman", "Sataev Emil", "Unknown person"]

emotion_text = None

while(True):
    ret, frame = cap.read()

    blob = cv2.dnn.blobFromImage(frame, size=(672, 384), crop=False)

    have_nots = False

    model.setInput(blob)
    ans = model.forward()
    for i in range(0, 200):
        x_min, y_min, x_max, y_max = np.array(ans[0, 0, i, 3:7]) * np.array([640, 480, 640, 480])
        if ans[0, 0, i, 2] > 0.5:
            cv2.rectangle(frame, (int(x_min), int(y_min)), (int(x_max), int(y_max)), ( 0, 255, 255))

            if len(sys.argv) > 1 and '-use_emotions' in sys.argv:
                blob_emotions = cv2.dnn.blobFromImage(frame[int(y_min):int(y_max), int(x_min):int(x_max)], size=(64, 64), crop=False)
                emotions.setInput(blob_emotions)
                ans_emotions = emotions.forward()[0, : , 0 , 0]
                ans_emotions = list(map(lambda x: 1 if x > 0.5 else 0, ans_emotions))
                _t = ''.join(list(map(str,ans_emotions))).find('1')
                if _t == -1:
                    _t = 0
                emotion_text = emotions_decode[_t]

            if '-get_face_id' in sys.argv:
                _ans = getter.get_answer(frame[int(y_min):int(y_max), int(x_min):int(x_max)])

                t = check('labels.txt', _ans)
                #print(names[t])
                font = cv2.FONT_HERSHEY_SIMPLEX 
                cv2.putText(frame,names[t],(int(x_min), int(y_min)), font, 1,(255,255,255),2,cv2.LINE_AA)
                if emotion_text != None:
                    cv2.putText(frame,emotion_text,(int(x_min), int(y_max)), font, 1,(255,255,255),2,cv2.LINE_AA)

            if '-use_notifications' in sys.argv and not have_nots:
                toaster.show_toast("Welcome, " + names[t],"")
                have_nots = True

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows() 

我想运行一个预训练的 OpenVINO 模型,但出现错误:

v\modules\dnn\src\dnn.cpp:2670: error: (-2:Unspecified error) Build OpenCV with Inference Engine to enable loading models from Model Optimizer. in function 'cv::dnn::dnn4_v20190122::Net::readFromModelOptimizer'

我需要使用推理引擎构建 OpenCV。我没有编程经验,不知道这是什么意思。

当我尝试这个时: https://github.com/opencv/opencv/wiki/Intel%27s-Deep-Learning-Inference-Engine-backend

试试

cmake \
  -DWITH_INF_ENGINE=ON \
  -DENABLE_CXX11=ON \
  ...

在 C:\Program Files (x86)\IntelSWTools\openvino_2019.1.148\opencv\samples 它给出了一个错误说:

CMake Error: The source directory "C:/Program Files (x86)/IntelSWTools/openvino_2019.1.148/opencv/samples/..." does not appear to contain CMakeLists.txt.

即使该文件夹中有一个 CMakeLists.txt。

有人可以帮帮我吗?

【问题讨论】:

    标签: python opencv cmake openvino


    【解决方案1】:

    对我来说,解决方案是删除 OpenCV Python 库并安装 opencv-python-inference-engine

    pip3 uninstall opencv-python
    pip3 uninstall opencv-contrib-python
    pip3 install opencv-python-inference-engine
    

    【讨论】:

    • 另外,github page of a project 有说明如何从头开始构建它
    • 请注意,这是一个 64 位端口,因此不适用于官方 Raspbian 版本。
    【解决方案2】:

    您应该将 OpenCV 添加到您的环境变量中。

    打开环境变量窗口:在搜索框中输入environment variables

    System Variables 编辑Path,插入OpenCV 路径作为您的安装路径。就我而言,我添加并能够使用它。

    C:\Program Files (x86)\IntelSWTools\openvino\opencv\bin
    

    您还需要检查首选后端和目标以选择推理后端和目标硬件。

    请参阅以下 API 参考:

    【讨论】:

    【解决方案3】:

    您可以只使用来自 OpenVINO 的 OpenCV。它已经使用英特尔的推理引擎进行编译。

    【讨论】:

    • 但是我该怎么做呢?我可以在我的 python 文件的开头导入它吗?我可以使用“pip install”来使用 openvino 版本的 opencv 吗?这对我来说都是全新的。感谢您的回复!
    • 从英特尔网站下载,安装,调用source /opt/intel/openvino/bin/setupvars.sh,然后使用Python3:python3 -c "import cv2 as cv; print(cv.__file__)"得到/opt/intel/openvino_2019.1.144/python/python3.6/cv2.cpython-36m-x86_64-linux-gnu.so
    • 我正在使用 Windows 10,所以我正在尝试计算您的评论。我运行 /openvino/bin/setupvars.bat 文件。然后在我的 PyCharm 代码中尝试执行以下操作: import sys sys.path.append('C:/Program Files (x86)/IntelSWTools/openvino_2019.1.148/python/python3.6/cv2.cp36-win_amd64.pyd') import cv2 as cv 这不起作用。我究竟做错了什么?非常感谢您之前的回答!
    • 或者我应该通过命令提示符转到我的虚拟环境/脚本目录,然后执行类似于python3 -c "import cv2 as cv; print(cv.__file__)" gets /opt/intel/openvino_2019.1.144/python/python3.6/cv2.cpython-36m-x86_64-linux-gnu.so的操作吗?
    • sys.path.append 应该只包含目录路径,而不是文件。
    猜你喜欢
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 2021-04-12
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多