#在这个python文件中,训练识别人脸
#我们使用的是openCV库提供的默认识别器
#我们还使用os模块来查找图像的目录
#我们将使用名为cv2.face.LBPHFaceRecognizer_create()的函数来实现识别器
#然后我们将使用Python pillow库从目录中加载图像
#我们还将使用Pickle库来存储标签
项目如图
Face_Recognition_Trainaing.py文件
#________________________Lets start the project_______________________#
# Importing Modules necessary for the Training
import cv2
import os
import numpy
from PIL import Image
import pickle
#Determining the Base or root directory where our python file is present
#确定python文件所在的基目录或根目录
BaseDirectory = os.path.dirname( os.path.abspath(__file__))
print(BaseDirectory)
#我们在操作系统库的帮助下找到了工作目录
#初始化Images文件夹的路径
ImageDirectory = os.path.join(BaseDirectory,"Training Data")
print(ImageDirectory)
#As the path of the image directory is been found out
# We are going to create an recognizer
#创建LBPH识别器并开始训练,当然也可以选择Eigen或者Fisher识别器
Recogniser =cv2.face.LBPHFaceRecognizer_create()
#Recogniser is created
#加载Face Casecade或haarcascade文件来预测图像中的人脸
FaceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
#Now the Haarcascade.xml file is loaded
# 创建一个变量CurrentID来初始化我们必须跟踪的图片的id
CurrentId = 0
#创建字典以存储具有相应标签ID的标签名称
LabelID = {}
#创建列表以存储标签id数据
YLabel=[]
#创建列表以数组的形式存储图像
XTrain=[]
#现在在找到的映像目录中运行循环
for root , dir, files in os.walk(ImageDirectory):
# running another loop across files in files that has been found
for file in files:
#实现If语句以仅捕获.jpg文件
if file.endswith('jpg'):
# 对于该特定文件,有一个名为persons Name的文件夹
# #文件夹名初始化为标签名
label = os.path.basename(root)
# 添加根目录和文件名以创建文件的完整路径
path = os.path.join(root ,file)
print(path)
print(label)
# 检查标签是否已用ID初始化如果未初始化ID
if not label in LabelID:
#Initialising to the label
LabelID[label]= CurrentId
# Incrementing the ID
CurrentId += 1
ID = LabelID[label]
# Now Pillow library is used
# Pillow 模块中的图像对象用于通过给定路径打开图像
# 平行图像被转换成灰度
OriginalImage = Image.open(path).convert("L")
##正在调整图像大小以更好地理解
OriginalImage = OriginalImage.resize((550 , 550) , Image.ANTIALIAS )
# 将图像转换为numpy数组
ImageArray = numpy.array(OriginalImage,"uint8")
print(ImageArray)
print(LabelID)
#使用FaceCascade 检测图像中的多个人脸
Faces = FaceCascade.detectMultiScale(ImageArray , 1.3 , 5)
for x,y,w,h in Faces:
# Finding the region of intrest
ROI = ImageArray[y:y+h , x:x+w]
# Appending the region of intrest in to Xtrain List
XTrain.append(ROI)
#Appending ID in to label list
YLabel.append(ID)
print(YLabel)
#print(XTrain)
# 现在标签被写入一个pickel文件,可以在识别时进一步使用
with open('Label.pickle' , 'wb') as file:
pickle.dump(LabelID , file)
#为给定的带有标签id的图像设置训练计算机
Recogniser.train( XTrain ,numpy.array(YLabel))
#将训练过的数据保存到.yml文件中,以便识别
Recogniser.save('Training.yml')
FaceRecognition.py文件
import cv2#下载opencv-contrib-python
import numpy
import pickle
#打开摄像头
Webcam = cv2.VideoCapture(0)
# 加载OpenCV人脸检测分类器Haar
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
Recognizer = cv2.face.LBPHFaceRecognizer_create()
Recognizer.read('Training.yml')
Labels = {}
with open('Label.pickle' , 'rb') as file:
OriginalLabel = pickle.load(file)
Labels = {v:k for k,v in OriginalLabel.items()}
while (True):
Return ,Frame =Webcam.read() #读取该帧的画面
Frame = cv2.flip(Frame , 1)
GrayScale = cv2.cvtColor(Frame , cv2.COLOR_BGR2GRAY)# 6灰度处理
Faces = face_cascade.detectMultiScale(GrayScale , 1.3 ,5)# 检查人脸
for X,Y,W,H in Faces:
ROI_GRAY = GrayScale[Y:Y+H , X:X+W]
ROI_COLOR = Frame[Y:Y+H , X:X+W]
ID , Confidence = Recognizer.predict(ROI_GRAY)#预测函数
if Confidence >= 45:
print(Labels[ID])
cv2.putText(Frame , Labels[ID],(X,Y) , cv2.FONT_HERSHEY_SIMPLEX , 1,(0,0,255),2)
cv2.rectangle(Frame , (X,Y) , (X+W , Y+H) , (255 , 0 , 0) , 1)
# 9显示图片
cv2.imshow('Image' , Frame)
# 10暂停窗口
if cv2.waitKey(1) == ord('q'):
break
# 11释放资源
Webcam.release()
# #12销毁窗口
cv2.destroyAllWindows()