0
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 i.e face_list and convert it to grayscale and append it to a list called faces

Jason Abba
  • 23
  • 2

1 Answers1

0

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. Think of it as a way of iterating over all elements of an n dimensional array without n nested loops. 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.

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
sehrob
  • 1,034
  • 12
  • 24