简体   繁体   中英

Training the facial recognition model

I'm writing a face recognizer in Python but I'm in trouble with the training part.

Here's my code:

import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
data_path = 'E:/Documents/7thSemester/Project/OpenCV-master/faces'
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path,f))]
Training_Data, Labels = [], []

for i, files in enumerate(onlyfiles):
    image_path = data_path + onlyfiles[i]
    images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    Training_Data.append(np.asarray(images, dtype=np.uint8))
    Labels.append(i)

Labels = np.asarray(Labels, dtype=np.int32)
model = cv2.face.LBPHFaceRecognizer_create()
model.train(np.asarray(Training_Data), np.asarray(Labels))
print("Model Training Complete!!!!!")

I'm getting the following error:

Traceback (most recent call last):
  File "facial_recognition_part2.py", line 19, in <module>
    model = cv2.face.LBPHFaceRecognizer_create()
AttributeError: module 'cv2.cv2' has no attribute 'face'

Please install opencv-contrib as:

pip install opencv-contrib

or

pip install opencv_contrib_python

See if this line of code works for you as it worked for my program:

cv2.face.createLBPHFaceRecognizer()

If it still doesn't work, you should consider reinstalling OpenCV with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM