how to dispaly percentage pridiction in fisher face recoginition algorithm

asked 2016-10-04 01:18:55 -0600

irum gravatar image

updated 2016-10-04 01:24:47 -0600

berak gravatar image

i am using fisher-face to recognize faces and i want to show a percentage prediction on a rectangle with the matching name. secondly, i want to set a value e.g if the matching percentage is 80% it should display the name of a person otherwise it should display unknown. third, i do not want to run recognition all the time. once the face is recognized by the algorithm as a known or unknown person it should not do it again and again. in short it should run recognition once on every face.

if someone can help i will be very grateful.

import cv2, sys, numpy, os
size = 4
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'

# Part 1: Create fisherRecognizer
# 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))
        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
# NOTE FOR OpenCV2: remove '.face'
model = cv2.createFisherFaceRecognizer()
model.train(images, lables)

# Part 2: Use fisherRecognizer on camera stream
haar_cascade = cv2.CascadeClassifier(fn_haar)
webcam = cv2.VideoCapture(0)
while True:
    (rval, frame) =
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))
    faces = haar_cascade.detectMultiScale(mini)
    for i in range(len(faces)):
        face_i = faces[i]
        (x, y, w, h) = [v * size for v in face_i]
        face = gray[y:y + h, x:x + w]
        face_resize = cv2.resize(face, (im_width, im_height))

        # Try to recognize the face
        prediction  = model.predict(face_resize)
    result = {
          'face': {

        'distance': prediction[1],
        'coords': {
          'x': str(faces[0][0]),
          'y': str(faces[0][1]),
          'width': str(faces[0][2]),
          'height': str(faces[0][3])
    print result
    print "prediction",prediction 
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)

        # Write the name of recognized face
        # [1]

        if prediction[1]<700:
              '%s - %.0f' % (names[prediction[0]],prediction[1]),
              (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
               (x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
    cv2.imshow('OpenCV', frame)
    key = cv2.waitKey(10)
    if key == 27:
edit retag flag offensive close merge delete



it's actually not that easy to get a proper % value, since the distance calculation is non-linear.

i'd rather propose, you refine your current algorithm based on the distance.

berak gravatar imageberak ( 2016-10-04 01:33:33 -0600 )edit