【问题标题】:cv2.error when training image (Overload Resolution Failed)训练图像时出现 cv2.error(重载解析失败)
【发布时间】:2021-07-26 23:21:48
【问题描述】:

我正在通过以下链接linklink2学习opencv。我得到了与人脸识别图像训练过程相关的错误。请纠正我或帮助解决我遇到的问题。谢谢

系统信息:

  • OpenCV 4.5.2.52
  • Python 3.9.5

详细说明

我需要使用下面的代码来训练图像,结果是:

- OpenCV/faces-train.py", line 50, in <module>
    recognizer.train(x_train, np.array(y_labels))
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'train'
> Overload resolution failed:
>  - src is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'src'

代码

import cv2
import os
import numpy as np
from PIL import Image
import pickle

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "images")

face_cascade = cv2.CascadeClassifier('cascade/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") or file.endswith("jpeg") :
            path = os.path.join(root, file)
            label = os.path.basename(root).replace(" ", "-").lower()
            # print(label, path)
            if not label in label_ids:
                label_ids[label] = current_id
                current_id += 1
                
            id_ = label_ids[label]
            # print(label_ids)
            
            y_labels.append(label)
            x_train.append(path)
            pil_image = Image.open(path).convert("L")
            image_array = np.array(pil_image, "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:x+w]
                x_train.append(roi)
                y_labels.append(id_)
                
# print(y_labels)
# print(x_train)

with open("labels.pickle", 'wb') as f:
    pickle.dump(label_ids, f)
    
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainner.yml")

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    删除以下行:

    • y_labels.append(标签)

    • x_train.append(路径)

    您实际上并不需要附加标签或路径,因为您基本上是在训练人脸识别器来识别/分类人脸。同样,您也不需要图像的路径,因为它也不是必需的。猜测这是导致错误的原因!

    如果修复有效,请告诉我!干杯:)

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 2019-02-20
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2020-03-15
      相关资源
      最近更新 更多