Ask Your Question

Revision history [back]

Why does recognizer predict always return 1?

My code is as follows

#load all requirments
import numpy as np
import cv2
import os
face_cascade = cv2.CascadeClassifier(r'C:\Users\Bharat Kalluri\Miniconda3\pkgs\opencv3-3.1.0-py35_0\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
recognizer = cv2.face.createEigenFaceRecognizer()


#load some useful functions
def show_img(img):
    cv2.imshow('grey',img);cv2.waitKey(0);cv2.destroyAllWindows();

def read_images(path,siz=None):
    im_arr = [];lab_arr = [];c = 0;
    for img in os.listdir(path):
        imgpath = (path+"\\"+img)
        img = cv2.imread(imgpath, cv2.IMREAD_GRAYSCALE)
        if (siz!=None):
            img = cv2.resize(siz)
        im_arr.append(np.asarray(img,dtype=np.uint8))
        c=c+1;lab_arr.append(c)
        return im_arr,lab_arr


#load images into respective sets
train_images,train_lables = read_images(r"C:\Users\Bharat Kalluri\Documents\Mini Projects\Python\db")
predict_images,predict_lables = read_images(r"C:\Users\Bharat Kalluri\Documents\Mini Projects\Python\train_test")
recognizer.train(train_images,np.asarray(train_lables))

temp = r"C:\Users\Bharat Kalluri\Documents\Mini Projects\Python\train_test"
for img in os.listdir(temp):
    img_path = temp+"\\"+img
    img1 = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
    a = (recognizer.predict(np.asarray(img1, dtype=np.uint8)))
    print(a)

So my code uses Python 3 with opencv 3.

I have some 30*30 faces images in the train_test and dbfolders with which i am trying to load their image data(as numpy arrays) and finally in the last for loop, try to predict all the images in the predict folder using the trained recognizer.

But whenever I run the recognizer, I get a one from the print statment, What is going wrong?