Ask Your Question
0

What is the label that 'predict' sends?

asked 2017-04-10 01:24:38 -0600

MS23 gravatar image

updated 2017-04-10 02:02:08 -0600

I have this python code for opencv for face recognition. AFAIK, a sample number of 100 should be good enough and I'm trying my best to have consistent illumination and pose. And yet at the result, I keep getting unknown. I'm pretty sure it's because the ID that I'm getting in the detector.py module is the sample number of the person instead of the person's id. How do I fix this? How do i make 'predict' give the sample id instead of the sample number? I'm attaching both the trainer.py and detector.py modules. I'm also attaching the datasetGenerator.py module so you may know how the samples are saved.

datasetGenerator.py:

import cv2
import numpy as np

faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeDetect = cv2.CascadeClassifier('haarcascade_eye.xml')

cam = cv2.VideoCapture(0)

id=raw_input('enter user id')
sampleNum = 0

while True:
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceDetect.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        sampleNum=sampleNum+1
        cv2.imwrite("dataSet/User"+str(id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+h])
        cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]
        eyes = eyeDetect.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color, (ex,ey),(ex+ew,ey+eh),(0,255,0),2)
        cv2.waitKey(100)
    cv2.imshow('img',img)
    cv2.waitKey(1)
    if(sampleNum>100):
        break

cam.release()
cv2.destroyAllWindows()

Trainer.py:

import os
import cv2
import numpy as np
from PIL import Image

recognizer = cv2.createLBPHFaceRecognizer()
path = 'dataSet'

def getImageWithLabels(path):
    imagePaths=[os.path.join(path,f) for f in os.listdir(path) if not f.startswith('.')]
    faces=[]
    labels=[]
    for imagePath in imagePaths:
        faceImg=Image.open(imagePath).convert('L')
        faceNp=np.array(faceImg,'uint8')
        nbr=int(os.path.split(imagePath)[-1].split('.')[1])
        faces.append(faceNp)
        print nbr
        labels.append(nbr)
        cv2.imshow("training",faceNp)
        cv2.waitKey(10)
    return labels, faces

labels,faces=getImageWithLabels(path)
recognizer.train(faces,np.array(labels))
recognizer.save('recognizer/trainingData.yml')
cv2.destroyAllWindows()

Detector.py

import cv2
import numpy as np

faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeDetect = cv2.CascadeClassifier('haarcascade_eye.xml')

cam = cv2.VideoCapture(0)
rec = cv2.createLBPHFaceRecognizer()
rec.load('recognizer/trainingData.yml')
i=0
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX,3,1,0,3,1)
while True:
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceDetect.detectMultiScale(gray, 1.2, 5)
    for (x,y,w,h) in faces:
        cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
        i,conf=rec.predict(gray[y:y+h,x:x+w])
        if (conf<50):
            if (i == 1):
                i="MS"
            elif (i==2):
                i="KR"
            else:
                i="Unknown" #had to put this to prevent the sample numbers such as 45 and 30 popping up
        else:
            i="Unknown"

        cv2.cv.PutText(cv2.cv.fromarray(img),str ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-04-10 08:39:33 -0600

berak gravatar image

predict() just returns the id, you trained it on, whatever that was.

the problem seems more, like you train it on a lot of images from a single person, and then try to distinguish another by the confidence. this simply won't work.

if your intent was, to use this as a authentification mechanism fro a single person, this also won't work, it's the wrong class for that. (you'd need more like a 2-class SVM, trained on person vs anyone-else)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-04-10 01:24:38 -0600

Seen: 1,737 times

Last updated: Apr 10 '17