训练面部识别模型

时间:2019-06-17 09:22:29

标签: python opencv face-recognition

我正在用Python编写人脸识别器,但是在培训部分遇到了麻烦。

这是我的代码:

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!!!!!")

我遇到以下错误:

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'

2 个答案:

答案 0 :(得分:0)

看看这行代码是否对我的程序有用:

cv2.face.createLBPHFaceRecognizer()

如果仍然无法正常运行,则应考虑使用重新安装OpenCV

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

答案 1 :(得分:0)

请以以下方式安装opencv-contrib:

pip install opencv-contrib

pip install opencv_contrib_python