OpenCV 4 TypeError:参数“标签"的预期 cv::UMat [英] OpenCV 4 TypeError: Expected cv::UMat for argument 'labels'

查看:88
本文介绍了OpenCV 4 TypeError:参数“标签"的预期 cv::UMat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写面部识别程序,但在尝试训练识别器时不断收到此错误

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'

我的代码是

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 是从 prepare_training_data 返回的图像列表中每张照片的标签列表,我将其转换为一个 numpy 数组,因为我读到了 train() 需要它.

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.

推荐答案

解决方案 - 标签应该是整数列表,并且你应该使用 numpy.array(labels)(或 np.数组(标签)).

Solution - labels should be list of integers, and you should use numpy.array(labels) (or np.array(labels)).

检查错误缺失的虚拟示例:

Dummy example to check an error absence:

labels=[0]*len(faces)
face_recognizer.train(faces, np.array(labels))

我没有在 python 上找到任何关于 openCV 人脸识别器的文档,所以我开始查看 C++ 文档和示例.由于 文档 这个库将 trainlabels 输入用作 std::vector.cpp 示例,由 openCV 文档提供,也使用 <代码>向量标签.等等,图书馆甚至有一个 不是整数输入.

I haven't found any documentation for openCV face recognizers on python, so I've started to look over c++ documentation and examples. And due to documentation this library uses labels input for train as a std::vector<int>. A cpp example, provided by openCV docs, also uses vector<int> labels. And so on, library even have an error for not an integer input.

这篇关于OpenCV 4 TypeError:参数“标签"的预期 cv::UMat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆