我试着按照"trying.py“运行,但是没有错误。怎么修呢?
trying.py
import cv2, os
import numpy as np
from PIL import Image
# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.LBPHFaceRecognizer_create()
# Using prebuilt frontal face training model, for face detection
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");
# Create method to get the images and label data
def getImagesAndLabels(path):
# Get all file path
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
# Initialize empty face sample
faceSamples = []
# Initialize empty id
ids = []
# Loop all the file path
for imagePath in imagePaths:
# Get the image and convert it to grayscale
PIL_img = Image.open(imagePath).convert('L')
# PIL image to numpy array
img_numpy = np.array(PIL_img, 'uint8')
# Get the image id
id = int(os.path.split(imagePath)[-1].split(".")[1])
print(id)
# Get the face from the training images
faces = detector.detectMultiScale(img_numpy)
# Loop for each face, append to their respective ID
for (x, y, w, h) in faces:
# Add the image to face samples
faceSamples.append(img_numpy[y:y + h, x:x + w])
# Add the ID to IDs
ids.append(id)
# Pass the face array and IDs array
return faceSamples, ids
# Get the faces and IDs
faces, ids = getImagesAndLabels('dataset')
# Train the model using the faces and IDs
recognizer.train(faces, np.array(ids))
# Save the model into trainer.yml
recognizer.save('trainer/trainer.yml')错误:
"C:\Users\HP\PycharmProjects\face_identificiation\trying.py",跟踪(最近一次调用):
第12行,在识别器= cv2.face.LBPHFaceRecognizer_create() AttributeError:模块'cv2‘中没有属性“face”
发布于 2022-11-23 10:42:19
很可能,您安装了错误的OpenCV版本。
导入face将失败(我使用的是v4.6.0),因为模块没有包含在OpenCv的“正常”安装中。
尝试运行pip list并检查OpenCv版本。我的猜测是,您安装了普通的OpenCv,它将为您提供如下的条目:
opencv-python 4.6.0.66
如果是这样的话,您应该先用pip uninstall opencv-python卸载OpenCv,然后再使用pip install opencv-contrib-python安装带有contrib包的OpenCv。
然后,使用pip list的条目应该包含
opencv-contrib-python 4.6.0.66
这样,您的代码就可以工作了!
编辑:我应该补充一下,您在代码中重写了关键字id,并且应该将变量重命名为其他东西,例如id_doc之类的。
发布于 2022-11-23 10:45:23
尝试以下命令:
pip install opencv-contrib-python --upgrade如果它不起作用,可以使用以下命令卸载opencv-:
pip uninstall opencv-contrib-python再安装一次。
https://stackoverflow.com/questions/74544839
复制相似问题