我使用OpenCV创建了一个人脸识别代码。现在出现的问题是:
- >有时它会检测墙上的小物体并将它们显示为我。我怎么能阻止它这样做?所以它只能检测到不是objets的面孔。
- >每当一个不知名的人出现在scree上时,它会随机从数据库中显示任何名称。每当有人知道有人撞到凸轮时,我都会把它显示为一张Unknown Face。
注意:我没有将它添加到sql数据库中,但是在我通过这个之后会这样做。提前获得帮助
数据集创建者代码:
import cv2
import sqlite3
import numpy as np
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam=cv2.VideoCapture(0);
id = raw_input('Enter Your ID : ')
Name = raw_input('Enter Your Name : ')
sampleNum=0;
while(True):
ret,img=cam.read();
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
sampleNum=sampleNum+1;
cv2.imwrite("dataSet/User."+str(id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.waitKey(500);
cv2.imshow("Face",img);
cv2.waitKey(1);
if(sampleNum>20):
break;
cam.release()
cv2.destroyAllWindows()
培训师代码:
import os
import cv2
import numpy as np
from PIL import Image
recognizer=cv2.createLBPHFaceRecognizer();
path='dataSet'
def getImagesWithID(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
for imagePath in imagePaths:
faceImg=Image.open(imagePath).convert('L');
faceNp=np.array(faceImg,'uint8')
ID=int(os.path.split(imagePath)[-1].split('.')[1])
faces.append(faceNp)
print ID
IDs.append(ID)
cv2.imshow("training",faceNp)
cv2.waitKey(10)
return IDs, faces
Ids,faces=getImagesWithID(path)
recognizer.train(faces,np.array(Ids))
recognizer.save('recognizer/trainningData.yml')
cv2.destroyAllWindows()
探测器代码:
import cv2
import numpy as np
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
cam=cv2.VideoCapture(0);
rec=cv2.createLBPHFaceRecognizer();
rec.load("recognizer\\trainningData.yml")
id=0
font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,2,2,0,2)
while(True):
ret,img=cam.read()
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
id,conf=rec.predict(gray[y:y+h,x:x+w])
if(id==1):
id="Vivek"
cv2.cv.PutText(cv2.cv.fromarray(img),str(id),(x,y+h),font,255);
cv2.imshow("Face",img);
if(cv2.waitKey(1)==ord('q')):
break;
cam.release()
cv2.destroyAllWindows()