【问题标题】:OpenCV 4 TypeError: Expected cv::UMat for argument 'labels'OpenCV 4 TypeError:参数“标签”的预期 cv::UMat
【发布时间】:2019-01-20 07:02:49
【问题描述】:

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

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

【问题讨论】:

    标签: python python-3.x numpy opencv facial-identification


    【解决方案1】:

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

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

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

    我还没有在 python 上找到任何关于 openCV 人脸识别器的文档,所以我开始查看 c++ 文档和示例。由于documentation,这个库使用labels 输入train 作为std::vector<int>。 openCV 文档提供的cpp example 也使用vector<int> labels。以此类推,图书馆甚至还有error for not an integer input

    【讨论】:

    • 是的,传递数值可以解决问题。我认为如果标签中包含字符,则需要先对标签进行编码
    【解决方案2】:

    可能的解决方案:

    作为错误,"TypeError: Expected Ptr<cv::UMat> for argument 'labels'" 需要整数值, 向list 插入值时,将它们转换为Integer

    例如:

    labels.append(int(value))  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2020-09-18
      相关资源
      最近更新 更多