Ask Your Question
0

I am getting an error "recognizer.train(x_train, np.array(y_labels)) TypeError: src is not a numpy array, neither a scalar"

asked 2019-07-26 02:23:18 -0500

updated 2019-07-26 03:26:47 -0500

LBerger gravatar image

program :

import os

from PIL import Image

import numpy as np

import cv2

import pickle

BASE_DIR=os.path.dirname(os.path.abspath(__file__))

image_dir=os.path.join(BASE_DIR,"photos")

face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.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"):

            path=os.path.join(root,file)

            label=os.path.basename(root).replace(" ","-").lower()


        if not label in label_ids:

                label_ids[label]=current_id

                current_id+=1

            id_=label_ids[label]



            y_labels.append(label) #some number

            x_train.append(path) #verify this image,turn into a NUMPY array,GRAY

            pil_image= Image.open(path).convert("L")#grayscale

            image_array=np.array(pil_image,"uint8")



            faces=face_cascade.detectMultiScale(image_array,1.5, 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_)












with open("labels.pickle",'wb')as f:

    pickle.dump(label_ids, f)


recognizer.train(x_train, np.array(y_labels))

recognizer.save("trainner.yml")
edit retag flag offensive close merge delete

Comments

I tested it about one yrs ago. It can be found on github.

supra56 gravatar imagesupra56 ( 2019-07-26 06:05:30 -0500 )edit

What if you try this:

recognizer.update(x_train, y_labels)
supra56 gravatar imagesupra56 ( 2019-07-26 06:11:57 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
-1

answered 2019-07-26 06:18:09 -0500

supra56 gravatar image

updated 2019-07-26 06:37:45 -0500

Change this:

recognizer.train(x_train, np.array(y_labels))

to

recognizer.train(x_train, y_labels)
edit flag offensive delete link more

Comments

no, both x_train and y_labels must be numpy arrays.

berak gravatar imageberak ( 2019-07-27 23:45:04 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-07-26 02:23:18 -0500

Seen: 61 times

Last updated: Jul 26