PYNQ: PYTHON PRODUCTIVITY FOR ZYNQ

Python : face racognition (cv2.face.LBPHFaceRecognizer_create() )

I am trying to implement face recognition algorithm on PYNQ-Z1 platform; v2.4 PYNQ image. The code is bellow :slight_smile:

from pynq import Overlay
Overlay(“base.bit”).download()
from pynq import Overlay
from pynq.lib.video import *

base = Overlay(‘base.bit’)
#hdmi_in = base.video.hdmi_in
hdmi_out = base.video.hdmi_out
Mode = VideoMode(640,480,24)
hdmi_out = base.video.hdmi_out
hdmi_out.configure(Mode,PIXEL_BGR)
hdmi_out.start()

monitor (output) frame buffer size

frame_out_w = 1280 #1920 800 1024 1280
frame_out_h = 1024 #1080 600 768 720

camera (input) configuration

frame_in_w = 640
frame_in_h = 480

import cv2
import numpy as np
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read(‘trainer/trainer.yml’)
cascadePath = “haarcascade_frontalface_default.xml”

multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades

faceCascade = cv2.CascadeClassifier(cascadePath);
font = cv2.FONT_HERSHEY_SIMPLEX
#iniciate id counter
id = 0

names related to ids: example ==> Marcelo: id=1, etc

names = [‘None’, ‘X’, ‘Y’, ‘Z’, ‘W’]

Initialize and start realtime video capture

videoIn = cv2.VideoCapture(0)
videoIn.set(cv2.CAP_PROP_FRAME_WIDTH, frame_in_w);
videoIn.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_in_h);

Define min window size to be recognized as a face

minW = 0.1videoIn.get(3)
minH = 0.1
videoIn.get(4)
print("capture device is open: " + str(videoIn.isOpened()))

I get this message


AttributeError Traceback (most recent call last)
in ()
3 import numpy as np
4
----> 5 recognizer = cv2.face.LBPHFaceRecognizer_create()
6 recognizer.read(‘trainer/trainer.yml’)
7 cascadePath = “haarcascade_frontalface_default.xml”
AttributeError: module ‘cv2.face’ has no attribute ‘LBPHFaceRecognizer_create’

I searched on the web, some suggested installing opencv-contrib-python :

pip uninstall opencv-contrib-python
pip install opencv-contrib-python

I got this :

root@pynq:/home/xilinx# sudo pip3 install opencv-contrib-python
Collecting opencv-contrib-python
Could not find a version that satisfies the requirement opencv-contrib-python (from versions: )
No matching distribution found for opencv-contrib-python
root@pynq:/home/xilinx#

Any help ?

Looks like you miugh tneed this: createLBPHFaceRecognizer()

I also want to highlight you have redundant code here:

You import “Overlay” twice.
You instantiate Overlay once but don’t assign it to anything. (download() isn’t needed as the .bit is downloaded by default when you instantiate.)

Overlay(“base.bit”).download()

You then instantiate it again:

base = Overlay(‘base.bit’)

I suggest you just do this:

from pynq import Overlay
from pynq.lib.video import *

base = Overlay(‘base.bit’)

Cathal

Thank you Cathal
what do you mean by :
Looks like you miugh tneed this: createLBPHFaceRecognizer() ?

Sorry, you might need createLBPHFaceRecognizer() instead of LBPHFaceRecognizer_create(). Looks like the API has chnaged in different versions of OpenCV.

Cathal

1 Like

Many thanks … .

To learn any thing about Python and enhancing skills and knowledge then you can join Python course where you will learn basic and advance level concepts.