OpenVINO: How to build OpenCV with Inference Engine to enable loading models from Model Optimizer

爱⌒轻易说出口 提交于 2021-01-28 01:08:58

问题


I installed OpenVINO and want to run the following code on 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() 

I want to run a pre-trained OpenVINO model, but get the error:

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'

I need to build OpenCV with Inference Engine. I'm not experienced in programming and don't know what this means.

When I try this: https://github.com/opencv/opencv/wiki/Intel%27s-Deep-Learning-Inference-Engine-backend

and try

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

in the C:\Program Files (x86)\IntelSWTools\openvino_2019.1.148\opencv\samples it gives an error saying:

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

even though there is a CMakeLists.txt in that folder.

Can someone please help me?


回答1:


You should add OpenCV to your environment variables.

Open environment variables window: Type environment variables in search box.

Edit Path from System Variables, insert OpenCV path as your installation path. In my case, I added and able to work with it.

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

You also need to check for preferable backend and target to pick inference backend and target hardware.

see following API references:

  • https://docs.opencv.org/master/d6/d0f/group__dnn.html
  • https://docs.opencv.org/3.4/db/d30/classcv_1_1dnn_1_1Net.html#a7f767df11386d39374db49cd8df8f59e
  • https://docs.opencv.org/3.4/db/d30/classcv_1_1dnn_1_1Net.html#a9dddbefbc7f3defbe3eeb5dc3d3483f4



回答2:


For me, the solution was to remove the OpenCV Python libraries and install opencv-python-inference-engine

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



回答3:


You can just use OpenCV from OpenVINO. It's already compiled with Intel's Inference Engine.



来源:https://stackoverflow.com/questions/57007007/openvino-how-to-build-opencv-with-inference-engine-to-enable-loading-models-fro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!