Intel® Distribution of OpenVINO™ Toolkit
Community assistance about the Intel® Distribution of OpenVINO™ toolkit, OpenCV, and all aspects of computer vision-related on Intel® platforms.
6401 Discussions

AttributeError: module cv2 has no attribute 'face' when code run in openvino environment

zapp02
Beginner
11,958 Views

So i was trying to use cv2.face.LBPHFaceRecognizer_create() in my face recognition program. When i run my program in python compiler everything worked perfectly , but when i try to run this program with NCS2 inside OpenVINO environment it create an error :

Traceback ( most recent call last ) :

  File "vino_recognizer.py", line 13, in <module>

      recognizer = cv2.face.LBPHFaceRecognizer_create()

AttributeError: module 'cv2' has no attribute 'face'

 

I already tried reinstalling opencv and opencv-contrib-python but it does not seems to working at all. Can someone help me with this problem ? Thank you.

 

Here is my code:

import os
import cv2
import time
import imutils
import numpy as np

net = cv2.dnn.readNet('face-detection-adas-0001.xml',
                     'face-detection-adas-0001.bin')

net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
cap = cv2.VideoCapture(0)

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainer/traindata.yml")
id=0


if cap.isOpened():
    vc,frame = cap.read()
else:
    vc = False
   

while vc:
    vc, frame = cap.read()
    orig= frame.copy()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   
    blob = cv2.dnn.blobFromImage(frame, size=(672, 384), ddepth=cv2.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.5:
            cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), color=(0, 255, 0))
            id,conf=recognizer.predict(gray[y1:y1+h,x1:x1+w1])
           
            org = x1,rect.bottom() + 25
            font = cv2.FONT_HERSHEY_COMPLEX_SMALL
            fontscale = int(1)
            color = (75,255,0)
            thickness = int(1)
            if id == 1:
                id = "Person1"
            if id == 2:
                id = "Person2"
            if id == 3:
                id = "Person3"
            cv2.putText(frame,str(id),(org),font,fontscale,color,thickness,cv2.LINE_AA)
       
    cv2.imshow("Face Landmarks", frame)

    k = cv2.waitKey(30) & 0xff

    if k == 27:
        print("Closing...")
        break

cap.release()
cv2.destroyAllWindows()
0 Kudos
3 Replies
Peh_Intel
Moderator
11,901 Views

Hi zapp02,

 

Installing prebuilt OpenCV (pip install opencv-contrib-python) allows you to use cv2.face.LBPHFaceRecognizer_create(). However, using this prebuilt OpenCV, OpenCV DNN module cannot be used.

Python_OpenCV.JPG

 

To use OpenCV DNN module, OpenCV is required to be built with OpenVINO™ Inference Engine (OpenVINO™ backend). You can use OpenCV from OpenVINO™. It's already compiled with OpenVINO Inference Engine. (Assuming you’re using OpenVINO™ 2021.4.2 since starting from 2022.1.1 release, OpenVINO™ does not include OpenCV.)

 

Anyhow, OpenCV from OpenVINO™ doesn't include the extra modules in OpenCV_Contrib which results in getting the following error if use cv2.face.LBPHFaceRecognizer_create().

AttributeError: module 'cv2' has no attribute 'face'

OpenVINO_OpenCV.JPG

 

In this case, you have to build custom OpenCV along with OpenVINO Inference Engine and extra modules in opencv_contrib.

custom_OpenCV.JPG

 

 

Steps to build custom OpenCV with minimal set of compilation flags:

1.      (Recommend) Uninstall pre-built OpenCV.

pip uninstall opencv opencv-contrib-python

 

2.      Download OpenCV from opencv/opencv repository.

git clone --recurse-submodules https://github.com/opencv/opencv.git

 

3.      Download OpenCV’s extra modules from opencv/opencv_contrib repository.

git clone --recurse-submodules https://github.com/opencv/opencv_contrib.git

 

4.      Create build directory and navigate to the build directory.

cd opencv

 

mkdir build && cd build

 

5.      (Optional) You might need to install some additional dependencies for OpenCV.

sudo apt install libcanberra-gtk-module libtbb-dev

 

6.      (Optional) Download and install OpenVINO™.

https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit/download.html

 

7.      Setup environment variables to detect Inference Engine.

source l_openvino_toolkit_ubuntu20_2022.2.0.7713.af16ea1d79a_x86_64.tgz/setupvars.sh

 

8.      Compile and install OpenCV.

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DOPENCV_EXTRA_MODULES_PATH=/home/pse/Downloads/opencv_contrib/modules -DWITH_OPENVINO=ON -DPYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages ..

 

make -j5

 

sudo make install

 

9.      Open new Terminal and import OpenCV with Python.

 

 

 

Regards,

Peh

 

0 Kudos
zapp02
Beginner
11,846 Views

Thank you for the respond and suggestion, sorry it's taken me a few days for the update or reply. We gonna try and do your suggestion after a few days of vacation out of town, we currently do not have access to the Raspberry Pi 4 and the NCS 2, both are university properties.
Please wait for further update, Thanks again.

0 Kudos
Peh_Intel
Moderator
11,495 Views

Hi zapp02,


As of today, we have not heard back from you. Therefore, this thread will no longer be monitored.


Please open a new case if you have further questions and we'll be happy to help you.



Regards,

Peh


0 Kudos
Reply