Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

What is the label that 'predict' sends?

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:

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==):
                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(i),(x,y+h),font,255)
        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.imshow('img',img)
    if(cv2.waitKey(10)==ord('q')):
        break

cam.release()
cv2.destroyAllWindows()

What is the label that 'predict' sends?

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:

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==):
                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(i),(x,y+h),font,255)
        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.imshow('img',img)
    if(cv2.waitKey(10)==ord('q')):
        break

cam.release()
cv2.destroyAllWindows()

From opencv doc:

C++: int FaceRecognizer::predict(InputArray src) const = 0

C++: void FaceRecognizer::predict(InputArray src, int& label, double& confidence) const = 0

Predicts a label and associated confidence (e.g. distance) for a given input image.
Parameters: 

    src – Sample image to get a prediction from.
    label – The predicted label for the given image.
    confidence – Associated confidence (e.g. distance) for the predicted label.

The suffix const means that prediction does not affect the internal model state, so the method can be safely called from within different threads.

What is the label that 'predict' sends?

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: 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==):
                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(i),(x,y+h),font,255)
        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.imshow('img',img)
    if(cv2.waitKey(10)==ord('q')):
        break

cam.release()
cv2.destroyAllWindows()

From opencv doc:

C++: int FaceRecognizer::predict(InputArray src) const = 0

C++: void FaceRecognizer::predict(InputArray src, int& label, double& confidence) const = 0

Predicts a label and associated confidence (e.g. distance) for a given input image.
Parameters: 

    src – Sample image to get a prediction from.
    label – The predicted label for the given image.
    confidence – Associated confidence (e.g. distance) for the predicted label.

The suffix const means that prediction does not affect the internal model state, so the method can be safely called from within different threads.

What is the label that 'predict' sends?

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==):
(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(i),(x,y+h),font,255)
        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.imshow('img',img)
    if(cv2.waitKey(10)==ord('q')):
        break

cam.release()
cv2.destroyAllWindows()

From opencv doc:

C++: int FaceRecognizer::predict(InputArray src) const = 0

C++: void FaceRecognizer::predict(InputArray src, int& label, double& confidence) const = 0

Predicts a label and associated confidence (e.g. distance) for a given input image.
Parameters: 

    src – Sample image to get a prediction from.
    label – The predicted label for the given image.
    confidence – Associated confidence (e.g. distance) for the predicted label.

The suffix const means that prediction does not affect the internal model state, so the method can be safely called from within different threads.