python - 我在 cv2 人脸识别器中遇到错误 - 您需要多个样本来学习模型。在函数'cv::face::LBPH::train'中
问题描述
如您所见,这是一个 face_recognizer。我收到了这个错误
**
文件“c:\Users\admin\Desktop\Vigyantram\faces\script7.py”,第 30 行,在 model.train(images, lables) cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData \Local\Temp\1\pip-req-build-5rb_9df3\opencv_contrib\modules\face\src\lbph_faces.cpp:362:错误:(-210:不支持的格式或格式组合)给出了空的训练数据。您需要多个样本来学习模型。在函数'cv::face::LBPH::train'中
**
并想知道如何解决它。我也查看了其他帖子,但找不到解决方案。这是我的代码-
# It helps in identifying the faces
import cv2, sys, numpy, os
size = 4
haar_file = 'haarcascade_frontalface_default.xml'
datasets = 'datasets'
# Part 1: Create fisherRecognizer
print('Recognizing Face Please Be in sufficient light...')
# Create a list of images and a list of corresponding names
(images, lables, 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
lable = id
images.append(cv2.imread(path, 0))
lables.append(int(lable))
id += 1
(width, height) = (130, 100)
# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]
# OpenCV trains a model from the images
# NOTE FOR OpenCV2: remove '.face'
model = cv2.face.LBPHFaceRecognizer_create()
model.train(images, lables)
# 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
我不知道该怎么办。
解决方案
推荐阅读
- tableau-api - 从 tableau javascript api 解析从 rememberCustomViewAsync 收到的响应
- java - 将linux路径转换为windows路径
- python - 以最有效的方式组合数字
- hyperledger-fabric - IBM Blockchain Platform V2、API 和链码
- python - 如何解决错误 EGL Driver message (Error) eglQueryDeviceAttribEXT: Bad attribute using Selenium and Python
- bash - 如何查找文件名以字母开头并将其保存到变量并将其保存到var?
- excel - 需要替换工作表名称
- ionic3 - 如何从 API 调用中获取属性?
- python - GeoAlchemy 中所有 Shapely 类型的一列
- python - Python - 使用 GetRows() 函数时出现 EOF/BOF 异常(来自 MDX 查询)