Skip to content

OpenCV(4.5.4) error: (-5:Bad argument) in function 'train' #22916

@Abdallahdameri

Description

@Abdallahdameri

System Information

hi im getting an error in the face training code
how can i slove it

Detailed description

Traceback (most recent call last):
File "/home/pi/Downloads/OpenCV-Face-Recognition-master/FacialRecognition/02_face_training.py", line 49, in
recognizer.train(faces,np.array(ids))
cv2.error: OpenCV(4.5.4) 👎 error: (-5:Bad argument) in function 'train'

Overload resolution failed:

  • Can't parse 'src'. Sequence item with index 0 has a wrong type
  • Can't parse 'src'. Sequence item with index 0 has a wrong type

Steps to reproduce

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

Path for face image database

path = '/home/pi/Downloads/OpenCV-Face-Recognition-master/FacialRecognition/dataset'

recognizer = cv2.face.LBPHFaceRecognizer_create()

detector = cv2.CascadeClassifier("/home/pi/Downloads/OpenCV-Face-Recognition-master/FacialRecognition/haarcascade_frontalface_default.xml");

function to get the images and label data

def getImagesAndLabels(path):

imagePaths = [os.path.join(path,f) for f in os.listdir(path)]     
faceSamples=['mohammad']
ids = []

for imagePath in imagePaths:

    PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
    img_numpy = np.array(PIL_img,'uint8')

    id = int(os.path.split(imagePath)[-1].split(".")[1])
    faces = detector.detectMultiScale(img_numpy)

    for (x,y,w,h) in faces:
        faceSamples.append(img_numpy[y:y+h,x:x+w])
        ids.append(id)

return faceSamples,ids

print ("\n [INFO] Training faces. It will take a few seconds. Wait ...")
faces,ids = getImagesAndLabels(path)
print(faces)
recognizer.train(faces,np.array(ids))

Save the model into trainer/trainer.yml

recognizer.write('/home/pi/Downloads/OpenCV-Face-Recognition-master/FacialRecognition/trainer')

recognizer.save() worked on Mac, but not on Pi

Print the numer of faces trained and end program

print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids))))

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions