在生成yml文件时,时不时会出现以下报错
D:\miniconda\envs\py37\python.exe "C:/Users/38273/Desktop/code/hope/whole test/facetest/13.py"
images/cap20230403161937
id: 1
face: ()
Traceback (most recent call last):
File "C:/Users/38273/Desktop/code/hope/whole test/facetest/13.py", line 125, in train_face
recognizer.train(faces, np.array(ids))
cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv_contrib\modules\face\src\lbph_faces.cpp:362: error: (-210:Unsupported format or combination of formats) Empty trainin
g data was given. You'll need more than one sample to learn a model. in function 'cv::face::LBPH::train'
进程已结束,退出代码为 -1073740791 (0xC0000409)
以下是代码
def save_face(self):
self.open_camera()
path = './photo'
path1 = './face_id'
exist = os.path.exists(path)
exist1 = os.path.exists(path1)
if not exist:
os.mkdir(path)
if not exist1:
os.mkdir(path1)
id = '1'
name = '张三'
classroom = '通信1902'
name = self.pinyin(name)
classroom = self.pinyin(classroom)
# cap = cv2.VideoCapture(0)
# ret, frame = cap.read()
if self.cap.isOpened():
FName = fr"images/cap{time.strftime('%Y%m%d%H%M%S', time.localtime())}"
print(FName)
self.showImage.save('./photo/' + id + '.' + name + '.' + classroom + '.jpg')
# cv2.imwrite(path + str(id) + '.' + str(classroom) + str(name) + ".jpg", frame)
else:
QMessageBox.critical(self, '错误', '摄像头未打开!')
return None
def get_face_id(self):
self.save_face()
path = './photo'
picture = []
ids = []
imagepaths = [os.path.join(path, f) for f in os.listdir(path)]
detector = cv2.CascadeClassifier('D:/opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_alt2.xml')
for imagepath in imagepaths:
img = Image.open(imagepath).convert('L')
img_np = np.array(img, 'uint8')
faces = detector.detectMultiScale(img_np)
id = int(os.path.split(imagepath)[1].split('.')[0])
for x, y, w, h in faces:
ids.append(id)
picture.append(img_np[y:y + h, x:x + w])
print('id:', id)
print('face:', faces)
return picture, ids
#
# recognizer = cv2.face.LBPHFaceRecognizer_create()
# recognizer.train(picture, np.array(ids))
# recognizer.write('./face_id/face_id.yml')
def train_face(self):
# self.open_camera
faces, ids = self.get_face_id()
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.train(faces, np.array(ids))
recognizer.write('./face_id/face_id.yml')
return QMessageBox.information(
self,
'成功',
'保存成功'
)