Abin Titus John
Abin Titus John

Reputation: 23

Unknown part of the code,not detecting unknown faces in the face recognition python program[opencv]

Please Help me. I'm running a face recognition detector python program that will display the data from sqllite studio database and i wrote a code displaying the unknown faces as unknown...

import cv2,os
import sqlite3
import numpy as np
from PIL import Image 
import pickle

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "Classifiers/face.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
path = 'dataSet'

def getProfile(Id):
   conn=sqlite3.connect("facebase.db")
   cmd="SELECT * FROM people WHERE ID="+str(Id)
   cursor=conn.execute(cmd)
   profile=None
   for row in cursor:
       profile=row
   conn.close()
   return profile 


cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX #Creates a font
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])
       profile=getProfile(Id)
       if(profile!=None):
         cv2.putText(im,str(profile[0]), (x,y+10), font, 2, (255,255,255),3)
         cv2.putText(im,str(profile[1]), (x,y+40), font, 2, (255,255,255),3)

       else:
         Id="Unknown"
         cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
         cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)
cv2.imshow('im',im) 
if cv2.waitKey(10) & 0xFF==ord('q'):
    break
cam.release()
cv2.destroyAllWindows()

The problem were is the unknown part of the code is note working. for example if an unknown face is detected instead of showing unknown,it displays random names from the database.

       if(profile!=None):
         cv2.putText(im,str(profile[0]), (x,y+10), font, 2, (255,255,255),3)
         cv2.putText(im,str(profile[1]), (x,y+40), font, 2, (255,255,255),3)

       else:
         Id="Unknown"
         cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
         cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)

And i'm using python 3.4 and opencv 3.4 Can anyone help me??? i'm new in python.

Thank you....

Upvotes: 0

Views: 2901

Answers (1)

Abin Titus John
Abin Titus John

Reputation: 23

I solved the problem, the conf measures the accuracy of the prediction in most case so the conf will be less than 50.The unknown will be greater than 50.So it could be solved by adding if(conf).

    if(conf<60):
            profile=getProfile(Id)
    else:
            Id=0
            profile=getProfile(Id)
    if(profile!=None):
        cv2.rectangle(im, (x-22,y-90), (x+w+80, y-22), (0,255,0), -1)
        cv2.putText(im,str(profile[0]), (x,y-40), font, 2, (255,255,255), 3)
        cv2.putText(im,str(profile[1]), (x+50,y-40), font, 2(255,255,255),3)

instead of

   if(profile!=None):
     cv2.putText(im,str(profile[0]), (x,y+10), font, 2, (255,255,255),3)
     cv2.putText(im,str(profile[1]), (x,y+40), font, 2, (255,255,255),3)

   else:
     Id="Unknown"
     cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1)
     cv2.putText(im, str(Id), (x,y-40), font, 2, (255,255,255), 3)

Upvotes: 1

Related Questions