[英]OpenCV 4 TypeError: Expected cv::UMat for argument 'labels'
I am writing a facial recognition program and I keep getting this error when I try to train my recognizer我正在编写面部识别程序,但在尝试训练识别器时不断收到此错误
TypeError: Expected cv::UMat for argument 'labels'
my code is我的代码是
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces)==0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
def prepare_training_data():
faces = []
labels = []
for img in photo_name_list: #a collection of file locations as strings
image = cv2.imread(img)
face, rect = detect_face(image)
if face is not None:
faces.append(face)
labels.append("me")
return faces, labels
def test_photos():
face_recognizer = cv2.face.LBPHFaceRecognizer_create()
faces, labels = prepare_training_data()
face_recognizer.train(faces, np.ndarray(labels))
labels is list of labels for each photo in the image list returned from prepare_training_data, and I convert it to a numpy array because I read that is what train() needs it to be.标签是从 prepare_training_data 返回的图像列表中每张照片的标签列表,我将其转换为一个 numpy 数组,因为我读到了 train() 需要它。
Solution - labels should be list of integers, and you should use numpy.array(labels)
(or np.array(labels)
).解决方案 - 标签应该是整数列表,你应该使用
numpy.array(labels)
(或np.array(labels)
)。
Dummy example to check an error absence:检查错误缺失的虚拟示例:
labels=[0]*len(faces)
face_recognizer.train(faces, np.array(labels))
I haven't found any documentation for openCV face recognizers on python, so I've started to look over c++ documentation and examples.我还没有在 python 上找到任何关于 openCV 人脸识别器的文档,所以我开始查看 C++ 文档和示例。 And due to documentation this library uses
labels
input for train
as a std::vector<int>
.并且由于文档,该库使用
labels
输入作为train
作为std::vector<int>
。 A cpp example , provided by openCV docs, also uses vector<int> labels
.由 openCV 文档提供的cpp 示例也使用
vector<int> labels
。 And so on, library even have an error for not an integer input .等等,库甚至有一个错误不是整数 input 。
Possible Solution:可能的解决方案:
As the error, "TypeError: Expected Ptr<cv::UMat> for argument 'labels'"
expects Integer value, when inserting values to the list
, convert them to Integer
.作为错误,
"TypeError: Expected Ptr<cv::UMat> for argument 'labels'"
需要 Integer 值,当将值插入list
,将它们转换为Integer
。
eg:例如:
labels.append(int(value))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.