OpenCV、Python、cv 2错误:(-5:错误的参数)给定了空的定型数据,您需要多个样本来学习模型,在函数'train'中

5jdjgkvh  于 2022-11-15  发布在  Python
关注(0)|答案(1)|浏览(106)

这是我的代码。我有一个学习问题。给出错误
错误:(-5:错误的参数)提供了空的定型数据。您将需要多个样本来学习模型。在函数“train”中。
我无法解决此问题。我找不到如何解决此问题的说明?我可以在哪里找到此问题的解决方案?我的图片大小为200 x200,格式为. pgm。

import os

import cv2
import numpy as np

def read_images(path, image_size):
    names = []
    training_images, training_labels = [], []
    label = 0
    for dirname, subdirnames, filenames in os.walk(path):
        for subdirname in subdirnames:
            names.append(subdirname)
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                img = cv2.imread(os.path.join(subject_path, filename),
                                 cv2.IMREAD_GRAYSCALE)
                if img is None:
                    # The file cannot be loaded as an image.
                    # Skip it.
                    continue
                img = cv2.resize(img, image_size)
                training_images.append(img)
                training_labels.append(label)
            label += 1
    training_images = np.asarray(training_images, np.uint8)
    training_labels = np.asarray(training_labels, np.int32)
    return names, training_images, training_labels

path_to_training_images = '/home/ace/OpenCV/cascades/A_M'# not properly. This is  = '/home/ace/OpenCV/cascades/'

training_image_size = (200, 200)
names, training_images, training_labels = read_images(path_to_training_images, training_image_size)

model = cv2.face.EigenFaceRecognizer_create()
model.train(training_images, training_labels)

face_cascade = cv2.CascadeClassifier('/haarcascade_frontalface_default.xml')

camera = cv2.VideoCapture(2)
while (cv2.waitKey(1) == -1):
    success, frame = camera.read()
    if success:
        faces = face_cascade.detectMultiScale(frame, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            roi_gray = gray[x:x+w, y:y+h]
            if roi_gray.size == 0:
                # The ROI is empty. Maybe the face is at the image edge.
                # Skip it.
                continue
            roi_gray = cv2.resize(roi_gray, training_image_size)
            label, confidence = model.predict(roi_gray)
            text = '%s, confidence=%.2f' % (names[label], confidence)
            cv2.putText(frame, text, (x, y - 20),
                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
        cv2.imshow('Face Recognition', frame)
wvmv3b1j

wvmv3b1j1#

虽然使用递归的walk-function然后分别列出子目录看起来有点奇怪,但是如果我创建一个如下的文件夹结构,那么read_images函数就可以工作:

images/
  - sub1/
    - img1.png
    - img2.png
  - sub2/
    - img3.png
    - img4.png

找到文件并填充返回值。可能您的文件夹结构与代码预期的不同。
您可以尝试使用一些类似下面的print语句来调试您的设置:

def read_images(path, image_size):
    names = []
    training_images, training_labels = [], []
    label = 0
    for dirname, subdirnames, filenames in os.walk(path):
        print(f"dirname={dirname}")
        for subdirname in subdirnames:
            print(f"- subdirname={subdirname}")
            names.append(subdirname)
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                print(f"  - filename={filename}")
                img = cv2.imread(os.path.join(subject_path, filename),
                                 cv2.IMREAD_GRAYSCALE)
                if img is None:
                    # The file cannot be loaded as an image.
                    # Skip it.
                    continue
                img = cv2.resize(img, image_size)
                training_images.append(img)
                training_labels.append(label)
            label += 1
    training_images = np.asarray(training_images, np.uint8)
    training_labels = np.asarray(training_labels, np.int32)
    return names, training_images, training_labels

请检查这些打印的输出,并将其与您的文件夹结构进行比较。如果这没有帮助,请与我们共享调试输出和您的文件夹结构。请不要将其作为评论共享,而是更新您的问题!

相关问题