Ask Your Question
0

Face recognition with opencv and python

asked 2017-06-04 06:41:42 -0600

K_K gravatar image

updated 2017-06-04 08:11:46 -0600

berak gravatar image

I am trying to implement the real-time face recognition using opencv and python. Each time i open my detector it gets stuck and gives error, could please help ?

import cv2
import numpy as np

recognizer = cv2.face.createLBPHFaceRecognizer()
recognizer.load('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);


cam = cv2.VideoCapture(0)
font = (cv2.FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1)
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.2,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        if(conf<50):
            if(Id==1):
                Id="Anirban"
            elif(Id==2):
                Id="Sam"
        else:
            Id="Unknown"
        cv2.cv.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)
    cv2.imshow('im',im) 
    if cv2.waitKey(10) & 0xFF==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

The error:

   TypeError                                 Traceback (most recent call last)
    <ipython-input-1-91c07dcf5ed1> in <module>()
         16     for(x,y,w,h) in faces:
         17         cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
    ---> 18         Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
         19         if(conf<50):
         20             if(Id==1):

    TypeError: 'int' object is not iterable
edit retag flag offensive close merge delete

Comments

opencv version ? (there was a bug in 3.1.0)

berak gravatar imageberak ( 2017-06-04 06:44:00 -0600 )edit

@berak opencv 3.2.0

K_K gravatar imageK_K ( 2017-06-04 07:02:06 -0600 )edit

are you sure ? cv2.__version__ ? help(recognizer) ?

berak gravatar imageberak ( 2017-06-04 07:04:49 -0600 )edit

@berak yes it was '3.1.0', so how t fix that ?

K_K gravatar imageK_K ( 2017-06-04 07:15:09 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-06-04 07:23:14 -0600

berak gravatar image

this is a bug, that was around about opencv3.1. in your version of the python wrappers only

 id = recognizer.predict(image)

is available, it does not return any confidence.

your options are:

  1. update. get latest master branch for both opencv and opencv_contrib, rerun cmake && make && make install.
  2. hack it locally. (remove "CV_WRAP" from this line and rebuild.) you will lose the "id only" variant, but get the "id and confidence" version.
  3. just live with it. you could pass your confidence threshold to the create function, instead of checking the confidence manually: cv2.face.createLBPHFaceRecognizer(threshold=50) and recieve -1 ids for predictions, which exceed that distance value.
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-06-04 06:41:42 -0600

Seen: 1,724 times

Last updated: Jun 04 '17