简体   繁体   English

模块 'cv2' 没有属性 LBPHFaceRecognizer_create()

[英]module 'cv2' has no attribute LBPHFaceRecognizer_create()

I am trying to learn face detection and I got this code from GeeksforGeeks tutorial.我正在尝试学习人脸检测,我从GeeksforGeeks教程中获得了这段代码。 However When I run one of the two files, it shows the error AttributeError: module 'cv2' has no attribute 'LBPHFaceRecognizer_create' .但是,当我运行这两个文件之一时,它显示错误AttributeError: module 'cv2' has no attribute 'LBPHFaceRecognizer_create' I tried uninstalling open cv, installing pip install opencv-contrib-python as well as reinstalling open cv and running it.我尝试卸载 open cv,安装pip install opencv-contrib-python以及重新安装 open cv 并运行它。 I am currently running open cv2 4.5.5.我目前正在运行 open cv2 4.5.5。 The tutorial advised to remove the '.face' from cv2.face.LBPHFaceRecognizer_create() for running cv2, however when I run it with .face, it displays module 'cv2' has no attribute 'face' .本教程建议从cv2.face.LBPHFaceRecognizer_create()中删除 '.face' 以运行 cv2,但是当我使用 .face 运行它时,它显示模块 'cv2' has no attribute 'face' Please, someone, help me with this请有人帮我解决这个问题

# It helps in identifying the faces
import cv2, sys, numpy, os
from cv2 import *
size = 4
haar_file = 'haarcascade_frontalface_default.xml'
datasets = 'datasets'

# Part 1: Create fisherRecognizer
print('Recognizing Face Please Be in sufficient Lights...')

# Create a list of images and a list of corresponding names
(images, labels, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(datasets):
    for subdir in dirs:
        names[id] = subdir
        subjectpath = os.path.join(datasets, subdir)
        for filename in os.listdir(subjectpath):
            path = subjectpath + '/' + filename
            label = id
            images.append(cv2.imread(path, 0))
            labels.append(int(label))
        id += 1
(width, height) = (130, 100)

# Create a Numpy array from the two lists above
(images, labels) = [numpy.array(lis) for lis in [images, labels]]

# OpenCV trains a model from the images
# NOTE FOR OpenCV2: remove '.face'
model = cv2.LBPHFaceRecognizer_create()
model.train(images, labels)

for i in range[0, 20]:
    if i<10:
        print(i)
        i += 1
    else:
        print('Done wit it')
# Part 2: Use fisherRecognizer on camera stream
face_cascade = cv2.CascadeClassifier(haar_file)
webcam = cv2.VideoCapture(0)
while True:
    (_, im) = webcam.read()
    gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 2)
        face = gray[y:y + h, x:x + w]
        face_resize = cv2.resize(face, (width, height))
        # Try to recognize the face
        prediction = model.predict(face_resize)
        cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)

        if prediction[1]<500:
            cv2.putText(im, '% s - %.0f' % (names[prediction[0]], prediction[1]), (x-10, y-10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))

        else:
            cv2.putText(im, 'not recognized', (x-10, y-10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))

    cv2.imshow('OpenCV', im)
    
    key = cv2.waitKey(10)
    if key == 27:
        break

I think that you may need to explicitly state "cv2.face" not just "face..."我认为您可能需要明确声明“cv2.face”而不仅仅是“face ...”

model = cv2.face.LBPHFaceRecognizer_create()

before doing so...did you confirm that you have a version of Opencv installed that contains the module Face?在这样做之前...您是否确认您安装了包含 Face 模块的 Opencv 版本? You can check like this:你可以这样检查:

import cv2

functions = dir(cv2)
for f in functions:
    print (f)

and if not...install like this:如果没有......像这样安装:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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