Why does recognizer predict always return 1?

asked 2016-08-13 17:09:05 -0500

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):

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)
        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")

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)))

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?

edit retag flag offensive close merge delete