recognition giving error of training and test data (size not same)

asked 2016-10-25 02:50:25 -0600

irum gravatar image

updated 2016-10-25 02:52:47 -0600

i want to run recognition for only 20 eq which are the list of faces on that i want to run recognition. but it is giving me error that training and test images are not of same size but they are same size. this is the error:

OpenCV Error: Bad argument (Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 10304 elements, but got 112.) in predict, file /home/irum/OpenCv/modules/contrib/src/facerec.cpp, line 623
Traceback (most recent call last):
  File "facerec-opencv.py", line 84, in <module>
    prediction  = model.predict(eq_i)
cv2.error: /home/irum/OpenCv/modules/contrib/src/facerec.cpp:623: error: (-5) Wrong input image size. Reason: Training and Test images must be of equal size! Expected an image with 10304 elements, but got 112. in function predict

and here is my code:

# facerec.py
import cv2, sys, numpy, os
import json
size = 3
fn_dir2 = 'unknown'
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
path2='/home/irum/Desktop/Face-Recognition/thakarrecog/unknown/UNKNOWNS'
path='/home/irum/Desktop/Face-Recognition/thakarrecog/att_faces'
# Part 1: Create fisherRecognizer
print('Training...')

# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)


for (subdirs, dirs, files) in os.walk(fn_dir):
    for subdir in dirs:
        names[id] = subdir 
        subjectpath = os.path.join(fn_dir, subdir) 
        for filename in os.listdir(subjectpath):
            path = subjectpath + '/' + filename
            lable = id
            images.append(cv2.imread(path, 0))
            lables.append(int(lable))
        id += 1



(im_width, im_height) = (112, 92)

# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]

# OpenCV trains a model from the images

model = cv2.createFisherFaceRecognizer()
model.train(images, lables)

# Part 2: Use fisherRecognizer on camera stream
haar_cascade = cv2.CascadeClassifier(fn_haar)
# Capturing camera feed
webcam = cv2.VideoCapture(0)
webcam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1920)
webcam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 1080)
frame_list=[]
counter = 0
while True:

    # Reading Frames from live stream
    (rval, frame) = webcam.read()
    frame=cv2.flip(frame,1,0)

    #Convert frame into gray
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #gray = cv2.GaussianBlur(gray, (21, 21), 0)
    # Resize the gary 
    mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))
    # Detecting the face 
    faces = haar_cascade.detectMultiScale(mini,1.1, 5)


    for i in range(len(faces)):
        face_i = faces[i]
        (x, y, w, h) = [v * size for v in face_i]
        # Croping face
        face = gray[y:y + h, x:x + w]
        face_resize = cv2.resize(face, (im_width, im_height))
        # Eualize Histogram
        eq = cv2.equalizeHist(face_resize)
        if counter<20:
            frame_list.append(eq)
                    counter +=1
                    print "frame_list", len(frame_list)
        # Try to recognize the face
        for i in range(len(eq)):
           eq_i = eq[i]
           prediction  = model.predict(eq_i)
           print "Recognition Prediction" ,prediction
           # Draw rectangle around the face
           cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

           if prediction[1]<=800:

             cv2.putText(frame,
                     '%s - %.0f' % (names[prediction[0]],prediction[1]),
                     (x-10, y-10), cv2.FONT_HERSHEY_DUPLEX,1,(255, 255, 0))
             break

           else:

             print "for prediction more than 600"
             print "prediction ...
(more)
edit retag flag offensive close merge delete

Comments

eq = cv2.equalizeHist(face_resize)

imho, this is your last valid line, anything below is broken. (throw away, and start from scratch)

why are you trying to iterate over eq (which is an image, not a list) ?

you should use eq for the prediction.

berak gravatar imageberak ( 2016-10-25 03:05:47 -0600 )edit

i am having a very slow fps on raspberry pi when i run this code without this part and do the recognition straight away if counter<20: frame_list.append(eq) counter +=1 print "frame_list", len(frame_list) # Try to recognize the face for i in range(len(eq)): eq_i = eq[i] i thought if i do recognition for limited faces every time it would be better. but i am having an error. point is i just want to make fps better.

irum gravatar imageirum ( 2016-10-25 03:10:33 -0600 )edit

still, make it work, then make it fast (in exactly that order.)

berak gravatar imageberak ( 2016-10-25 03:14:45 -0600 )edit

also: profile (don't guess).

you will find, that the face detection is eating all of the cpu, while the recognition is quite fast

berak gravatar imageberak ( 2016-10-25 03:16:26 -0600 )edit

i have a working copy of the code and that is working properly just slow.... i need to make that fast

irum gravatar imageirum ( 2016-10-25 03:48:20 -0600 )edit

sure, i see (also did not mean to bash you, or something..)

first thing, i'd try would be: use smaller webcam resolution, try to set min/max size in the detection, increase scale factor there, until you start to loose face detections.

berak gravatar imageberak ( 2016-10-25 03:54:36 -0600 )edit