jasmcaus / opencv-course

Learn OpenCV in 4 Hours - Code used in my Python and OpenCV course on freeCodeCamp.
https://youtu.be/oXlwWbU8l2o
MIT License
1.11k stars 955 forks source link

Face Recognition, AttributeError: module 'cv2.cv2' has no attribute 'face_LBPHFaceRecognizer' #14

Closed hanifizzudinrahman closed 3 years ago

hanifizzudinrahman commented 3 years ago

I have this error, and I can't resolve this issue image This is the program

import os
import cv2 as cv
import numpy as np

#people = ['...','....']

people = []
for i in os.listdir(r'C:\Users\owner\_Learning_\_Computer Vision - OpenCV_\OpenCV - Python\OpenCV Course - Full Tutorial with Python - YouTube\Resources\Faces\train'):
    people.append(i)
print(people)

DIR = r'C:\Users\owner\_Learning_\_Computer Vision - OpenCV_\OpenCV - Python\OpenCV Course - Full Tutorial with Python - YouTube\Resources\Faces\train'

features = []
labels = []
haar_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')

def create_train():
    for person in people:
        print("Person:", person)
        path = os.path.join(DIR, person)
        label = people.index(person)
        #print("Label:", label)

        for img in os.listdir(path):
            img_path = os.path.join(path,img)
            #print("img_path:", img_path)

            img_array = cv.imread(img_path)
            gray = cv.cvtColor(img_array, cv.COLOR_BGR2GRAY)

            faces_rect = haar_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=4)

            for (x,y,w,h) in faces_rect:
                faces_roi = gray[y:y+h, x:x+w]

                features.append(faces_roi)
                labels.append(label)

                #cv.imshow(img, faces_roi)

create_train()
print('Training Done-------------')
#print(f'length of features = {len(features)}')
#print(f'length of labels = {len(labels)}')

features = np.array(features, dtype='object')
labels = np.array(labels)

face_recognizer = cv.face.LBPHFaceRecognizer_create()

#train
face_recognizer.train(features, labels)

face_recognizer.save('3.2_face_trained.yml')
np.save('3.2_features.py', features)
np.save('3.2_labels.py', labels)

cv.waitKey(0)
cv.destroyAllWindows()
jasmcaus commented 3 years ago

Okay, I've found a couple of potential fixes:

  1. https://github.com/opencv/opencv/issues/13848
  2. https://stackoverflow.com/questions/44633378/attributeerror-module-cv2-cv2-has-no-attribute-createlbphfacerecognizer

Let me know if this fixes your issue

jasmcaus commented 3 years ago

Closing this now as there hasn't been any reply to this thread. Feel free to reopen it if need be