在下面的代码中遇到此错误
def train_classifier(self):
data_dir = "Photo_data"
path = [os.path.join(data_dir, file) for file in os.listdir(data_dir)]
faces = []
ids = []
for image in path:
img = Image.open(image).convert('L') #Gray scale image
image_np = np.array(img, 'uint8')
id = int(os.path.split(image)[1].split('.')[1])
faces.append(image_np)
ids.append(id)
cv2.imshow("Training", image_np)
cv2.waitKey(1) == 13
ids = np.array(ids)
# Train the classifier and save
clf = cv2.face.LBPHFaceRecognizer_create()
clf.train(faces, ids)
clf.write("classifier.xml")
cv2.destroyAllWindows()
messagebox.showinfo("Result", "Training Photo sample Completed!!")
错误:
cv2.error: OpenCV(4.9.0) /io/opencv_contrib/modules/face/src/lbph_faces.cpp:362:
error: (-210:Unsupported format or combination of formats)
Empty training data was given. You'll need more than one sample
to learn a model. in function 'train'
尝试卸载 opencv-python 然后使用 pip 安装 opencv-contrib-python
您的文件可能已损坏,但目录中可能有非图像文件。
添加此行:
for image in path:
print(f"about to open {image}") # <<<<<<
img = Image.open(image).convert('L') #Gray scale image
或者直接进行类似的事情
for image in path:
if not image.endswith(('.jpg', '.gif', '.png')):
next
筛选出您不期望的文件。