我正在构建图像处理分类器,除此行外,整个代码都在右边
recognizer.train(x_train, np.array(y_labels))
我的代码:
import os
import numpy as np
from PIL import Image
import cv2
import pickle
os.path.join(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
print(BASE_DIR)
image_dir =os.path.join(BASE_DIR, "images") #immages is the picture's folder
print(image_dir)
face_cascade = cv2.CascadeClassifier('C:/Users/lucai/Desktop/python/tutorial/cv2/data/haarcascade_frontalface_alt2.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
current_id = 0
label_ids = {}
y_labels =[]
x_train = []
for root, dirs, files in os.walk(image_dir):
for file in files:
if file.endswith("png") or file.endswith("jpg"):
path = os.path.join(root, file)
label = os.path.basename(root).replace(" ", "-").lower()
print(label, path)
if label in label_ids:
pass
else:
label_ids[label] = current_id
current_id += 1
id_ = label_ids[label]
print(label_ids)
pil_image = Image.open(path).convert("L") #grays scale
image_array = np.array(pil_image, dtype=np.uint8) #"uint8",
print(image_array)
faces = face_cascade.detectMultiScale(image_array, scaleFactor=1.5, minNeighbors=5)
for (x, y,w,h) in faces:
roi = image_array[y:y+h, x:h+w]
x_train.append(roi) #roi=region of interest
y_labels.append(id_)
#print(y_labels)
#print(x_train)
with open ("labels.pickle", 'wb') as f:
pickle.dump(label_ids , f ) #accumulare le label di training
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainer.ylm")
最后两行给我这个错误:
recognizer.train(x_train, np.array(y_labels))
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:235: error: (-215:Assertion failed) s >= 0 in function 'cv::setSize'
有人可以帮忙吗?