代码部分编译一直提示有一个问题
Traceback (most recent call last):
File "C:\Users\dw188\Desktop\python练习\人脸识别\数据训练.py", line 38, in
face,ids=getImageAndLabels(path)
File "C:\Users\dw188\Desktop\python练习\人脸识别\数据训练.py", line 32, in getImageAndLabels
return facesSamples.ids
AttributeError: 'list' object has no attribute 'ids'
我在指定保存文件的地方也没找到要保存的文件
代码部分如下:
import os
import cv2
from PIL import Image
import numpy as np
def getImageAndLabels(path):
#存储人脸数据
facesSamples=[]
#存储姓名数据
ids=[]
#存储图片信息
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
#加载分类器
face_detector=cv2.CascadeClassifier('D:/python_opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
#遍历列表中的图片
for imagePath in imagePaths:
#打开图片,灰度化PIL有九种不同模式:1,L,P,RGB,RGBA,CMYK,YCbCr,I,F
PIL_img=Image.open(imagePath).convert('L')
#将图片转换为数组,以黑白深浅
img_numpy=np.array(PIL_img,'uint8')
#获取图片人脸特征
faces=face_detector.detectMultiScale(img_numpy)
#获取每张照片的id和姓名
id=str(os.path.split(imagePath)[1].split('.')[0])
#预防无面容图片
for x,y,w,h in faces:
ids.append(id)
facesSamples.append(img_numpy[y:y+h,x:x+w])
#打印脸部特征和id
print('id',id)
print('fs',facesSamples)
print(list)
return facesSamples.ids
if __name__=='__main__':
#图片路径
path='D:/python_opencv/wodetest'
#获取图像数组和ID标签数组和姓名
face,ids=getImageAndLabels(path)
#加载识别器
recognizer=cv2.face.LBPHFaceRecognizer_create()
#训练数据
recognizer.train(face,np.array(ids))
#保存文件
recognizer.write('D:/python_opencv/new/test.yml')
这是运行结果。结果显示有训练出人脸特征,就是将id和特征合并保存时出了问题
C:\Users\dw188\AppData\Local\Programs\Python\Python39\python.exe C:/Users/dw188/Desktop/python练习/人脸识别/数据训练.py
id wang
fs [array([[183, 183, 181, ..., 112, 113, 112],
[183, 182, 181, ..., 111, 112, 112],
[183, 182, 181, ..., 111, 112, 112],
...,
[138, 138, 139, ..., 117, 117, 117],
[139, 139, 139, ..., 116, 117, 117],
[141, 141, 140, ..., 115, 116, 117]], dtype=uint8)]
<class 'list'>
Traceback (most recent call last):
File "C:\Users\dw188\Desktop\python练习\人脸识别\数据训练.py", line 38, in <module>
face,ids=getImageAndLabels(path)
File "C:\Users\dw188\Desktop\python练习\人脸识别\数据训练.py", line 32, in getImageAndLabels
return facesSamples.ids
AttributeError: 'list' object has no attribute 'ids'
进程已结束,退出代码 1
这是照着视频教学抄下来的,视频课程人间保存成功了我只是把路径改为了我的,但是就保存不了
这是图片获取路径:
这是人脸特征数据保存路径: