#!/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
for filename in os.listdir(subjectpath):
loop ?) anything from(im_width,im_height)=(112,92)
down should go top level, out of the loop.if predicition[1]>x:
supposed to do ? comparing the confidence/distance to the x position does not make any sense at all.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
i'm just suspecting, that you don't know , what you're doing at all here ;( again, see the issues above
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.....
do you understand above issues ? fix them.
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))
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
^^ that's all due to your broken logic. again, the whole face.create... train and VideoCapture stuff has to go out of the loop
okay il try that ...... Actually i got it from ..... http://noahingham.com/blog/facerec-py...
again,
if prediction[1]>x:
comparing distance to x-position is dumb as shit.