Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

#!/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
click to hide/show revision 2
No.2 Revision

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

berak gravatar image

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

#!/usr/bin/python
import cv2
import sys
import numpy
import os

for os #for PIL import Image

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

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))

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