as i added my images in at data base but it shows unknown when i run the program following is the code [closed]

asked 2016-01-30 00:54:27 -0600

rss gravatar image

updated 2016-01-30 00:57:24 -0600

berak gravatar image
#!/usr/bin/python
import cv2
import sys
import numpy
import os
#for PIL import Image

size=4
fn_haar='haarcascade_frontalface_default.xml'
fn_dir='att_faces'

(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
            label=id
            images.append(cv2.imread(path,0))
            lables.append(int(label))

            id+=1

            (im_width,im_height)=(112,92)

            (images,lables)=[numpy.array(lis) for lis in [images,lables]]

            #model=cv2.face.createEigenFaceRecognizer()
            model=cv2.face.createFisherFaceRecognizer()
            #model=cv2.face.createLBPHFaceRecognizer()

            model.train(images,lables)

            haar_cascade=cv2.CascadeClassifier(fn_haar)
            webcam=cv2.VideoCapture(0)
            while True:
                (rval,frame)=webcam.read()
                frame=cv2.flip(frame,1,0)
                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))

                    predicition = model.predict(face_resize)
                    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3)

                if predicition[1]>x:

                    cv2.putText(frame,'%s - %.0f' % (names[predicition[0]],predicition[1]),(x-10,y-10),cv2.FONT_HERSHEY_PLAIN,1,(0,255,0))

                else:

                    cv2.putText(frame,'UnKnown',(x-10,y-10),cv2.FAST_FEATURE_DETECTOR_THRESHOLD,1,(0,0,255))


                    cv2.imshow('Face Recognition',frame)
                    key=cv2.waitKey(10)
                    if key == 27:
                        break
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-12-08 18:14:49.104433

Comments

  • is the indentation correct ? (does it all happen inside the for filename in os.listdir(subjectpath): loop ?) anything from (im_width,im_height)=(112,92) down should go top level, out of the loop.
  • what is if predicition[1]>x: supposed to do ? comparing the confidence/distance to the x position does not make any sense at all.
  • you need to increase the label id per subject folder , not per image, when reading data
  • believe it or not, spwelling matters. :)
berak gravatar imageberak ( 2016-01-30 01:02:11 -0600 )edit

as it was detecting my face and showing my name but another person comes in (added imgages to data base)....it also showing my name only is there any problem with prediction

rss gravatar imagerss ( 2016-01-30 01:12:44 -0600 )edit

i'm just suspecting, that you don't know , what you're doing at all here ;( again, see the issues above

berak gravatar imageberak ( 2016-01-30 01:22:09 -0600 )edit

hello berak sorry for delay response ....actually i was very new to opencv.....it is hard to understand the code... ...i browsed in net got it....i tried on raspberry pi with opencv3,python 3.....my streming is loading on screen and is comparing with trained images but .iam getting names[predicition[0]] as my name and predicition[1] 0 i think this value is confidence......i dont know where the problem...pls clarify me pls.....

rss gravatar imagerss ( 2016-02-03 02:21:25 -0600 )edit

do you understand above issues ? fix them.

berak gravatar imageberak ( 2016-02-03 02:22:58 -0600 )edit

why the confidence value is showing 0 (prediction[1])...? as i removed if else condition of prediction[1] and directly as

cv2.putText(frame,'%s - %.0f' % (names[predicition[0]],predicition[1]),(x-10,y-10),cv2.FONT_HERSHEY_PLAIN,1,(0,255,0))

rss gravatar imagerss ( 2016-02-03 02:32:22 -0600 )edit

another thing if i use model=cv2.face.createFisherFaceRecognizer() it showing some sort of error as

error: (-5) at least two classes are needed to perform a lda. reason: only one class was given! in function lda

rss gravatar imagerss ( 2016-02-03 02:39:10 -0600 )edit

^^ that's all due to your broken logic. again, the whole face.create... train and VideoCapture stuff has to go out of the loop

berak gravatar imageberak ( 2016-02-03 02:48:11 -0600 )edit

okay il try that ...... Actually i got it from ..... http://noahingham.com/blog/facerec-py...

rss gravatar imagerss ( 2016-02-03 02:55:21 -0600 )edit

again, if prediction[1]>x: comparing distance to x-position is dumb as shit.

berak gravatar imageberak ( 2016-02-03 03:01:40 -0600 )edit