Python OpenCV 4 TypeError:参数“标签”的预期 cv::UMat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54274298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:27:59  来源:igfitidea点击:

OpenCV 4 TypeError: Expected cv::UMat for argument 'labels'

pythonpython-3.xnumpyopencvfacial-identification

提问by Tyler Strouth

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() 需要它。

回答by PolyGlot

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 recodnisers on python, so i've started to look over c++ documentation and examples. And due to documentationthis library uses labelsinput for trainas 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.

我还没有在 python 上找到任何关于 openCV 人脸识别器的文档,所以我开始查看 C++ 文档和示例。并且由于文档,这个库使用labels输入train作为std::vector<int>. 甲CPP例如,通过OpenCV的文档提供,也使用vector<int> labels。等等,库甚至有一个错误不是整数 input