简体   繁体   English

OpenCV 人脸识别灰度转换错误,同时训练我的分类器

[英]OpenCV Face Recognition grayscale conversion error while training my classifier

import numpy as np
import cv2
from skimage.io import imread_collection

dataset = r'C:\Users\JasonPC\Documents\CodeVault\Python\FaceRecognition\dataset\*.jpg' # path for images

List = imread_collection(dataset)
faces_list = np.array(List)

def classifier_trainer(faces_list):
    img_id = 0
    faces = []
    faceID = []
    for face in np.nditer(faces_list):
        gray_face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) # coverting color image to gray scale
        np_face = np.array(gray_face, 'uint8') # converting gray image into numpy array
        img_id += 1
        
        faces.append(np_face)
        faceID.append(img_id)
    faceID = np.array(faceID)
    classifier = cv2.face.LBPHFaceRecognizer_create()
    classifier.train(faces, faceID)
    classifier.write('Classifier.yml')

classifier_trainer(faces_list) 

I'm trying to train a classifier to recognize my face.我正在尝试训练一个分类器来识别我的脸。 I'm stuck with this really huge error.我被这个非常大的错误所困扰。

Traceback (most recent call last):
  File "c:/Users/JasonPC/Documents/CodeVault/Python/FaceRecognition/trainer.py", line 26, in <module>
    classifier_trainer(faces_list)
  File "c:/Users/JasonPC/Documents/CodeVault/Python/FaceRecognition/trainer.py", line 15, in classifier_trainer   
    gray_face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) # 
coverting color image to gray scale
cv2.error: OpenCV(4.2.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__thiscall cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<1,-1,-1>,struct cv::impl::A0xe227985e::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 1 

All I want my code to do is seek the images from the numpy array ie face_list and convert it to grayscale and append it to a list called faces我想要我的代码做的就是从 numpy 数组即face_list中查找图像并将其转换为灰度和 append 将其转换为名为faces的列表

The problem is in how you're iterating over your images.问题在于您如何迭代图像。 You're using nditer and in your case as it turns out it flattens the n dimensional array to say 1 dimensional and then iterates over all of it's elements.您正在使用nditer ,在您的情况下,事实证明它将n维数组展平为一维,然后迭代它的所有元素。 Think of it as a way of iterating over all elements of an n dimensional array without n nested loops.可以将其视为一种迭代n维数组的所有元素而无需n嵌套循环的方法。 So here, the face variable in your loop is an integer, float or whatever numerical value and you're passing it to cvtColor and getting this error message.因此,在这里,循环中的face变量是 integer、浮点数或任何数值,您将其传递给cvtColor并收到此错误消息。

If you want to iterate over the images I think, you can just iterate over them like this:如果你想迭代我认为的图像,你可以像这样迭代它们:

for face in faces_list: 
    # your code goes here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM