当前位置:   article > 正文

Python人脸检查识别代码详解(完整版)_python人脸识别代码csdn

python人脸识别代码csdn

#在这个python文件中,训练识别人脸
#我们使用的是openCV库提供的默认识别器
#我们还使用os模块来查找图像的目录
#我们将使用名为cv2.face.LBPHFaceRecognizer_create()的函数来实现识别器
#然后我们将使用Python pillow库从目录中加载图像
#我们还将使用Pickle库来存储标签 

项目如图

Face_Recognition_Trainaing.py文件

  1. #________________________Lets start the project_______________________#
  2. # Importing Modules necessary for the Training
  3. import cv2
  4. import os
  5. import numpy
  6. from PIL import Image
  7. import pickle
  8. #Determining the Base or root directory where our python file is present
  9. #确定python文件所在的基目录或根目录
  10. BaseDirectory = os.path.dirname( os.path.abspath(__file__))
  11. print(BaseDirectory)
  12. #我们在操作系统库的帮助下找到了工作目录
  13. #初始化Images文件夹的路径
  14. ImageDirectory = os.path.join(BaseDirectory,"Training Data")
  15. print(ImageDirectory)
  16. #As the path of the image directory is been found out
  17. # We are going to create an recognizer
  18. #创建LBPH识别器并开始训练,当然也可以选择Eigen或者Fisher识别器
  19. Recogniser =cv2.face.LBPHFaceRecognizer_create()
  20. #Recogniser is created
  21. #加载Face Casecade或haarcascade文件来预测图像中的人脸
  22. FaceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
  23. #Now the Haarcascade.xml file is loaded
  24. # 创建一个变量CurrentID来初始化我们必须跟踪的图片的id
  25. CurrentId = 0
  26. #创建字典以存储具有相应标签ID的标签名称
  27. LabelID = {}
  28. #创建列表以存储标签id数据
  29. YLabel=[]
  30. #创建列表以数组的形式存储图像
  31. XTrain=[]
  32. #现在在找到的映像目录中运行循环
  33. for root , dir, files in os.walk(ImageDirectory):
  34. # running another loop across files in files that has been found
  35. for file in files:
  36. #实现If语句以仅捕获.jpg文件
  37. if file.endswith('jpg'):
  38. # 对于该特定文件,有一个名为persons Name的文件夹
  39. # #文件夹名初始化为标签名
  40. label = os.path.basename(root)
  41. # 添加根目录和文件名以创建文件的完整路径
  42. path = os.path.join(root ,file)
  43. print(path)
  44. print(label)
  45. # 检查标签是否已用ID初始化如果未初始化ID
  46. if not label in LabelID:
  47. #Initialising to the label
  48. LabelID[label]= CurrentId
  49. # Incrementing the ID
  50. CurrentId += 1
  51. ID = LabelID[label]
  52. # Now Pillow library is used
  53. # Pillow 模块中的图像对象用于通过给定路径打开图像
  54. # 平行图像被转换成灰度
  55. OriginalImage = Image.open(path).convert("L")
  56. ##正在调整图像大小以更好地理解
  57. OriginalImage = OriginalImage.resize((550 , 550) , Image.ANTIALIAS )
  58. # 将图像转换为numpy数组
  59. ImageArray = numpy.array(OriginalImage,"uint8")
  60. print(ImageArray)
  61. print(LabelID)
  62. #使用FaceCascade 检测图像中的多个人脸
  63. Faces = FaceCascade.detectMultiScale(ImageArray , 1.3 , 5)
  64. for x,y,w,h in Faces:
  65. # Finding the region of intrest
  66. ROI = ImageArray[y:y+h , x:x+w]
  67. # Appending the region of intrest in to Xtrain List
  68. XTrain.append(ROI)
  69. #Appending ID in to label list
  70. YLabel.append(ID)
  71. print(YLabel)
  72. #print(XTrain)
  73. # 现在标签被写入一个pickel文件,可以在识别时进一步使用
  74. with open('Label.pickle' , 'wb') as file:
  75. pickle.dump(LabelID , file)
  76. #为给定的带有标签id的图像设置训练计算机
  77. Recogniser.train( XTrain ,numpy.array(YLabel))
  78. #将训练过的数据保存到.yml文件中,以便识别
  79. Recogniser.save('Training.yml')

FaceRecognition.py文件 

  1. import cv2#下载opencv-contrib-python
  2. import numpy
  3. import pickle
  4. #打开摄像头
  5. Webcam = cv2.VideoCapture(0)
  6. # 加载OpenCV人脸检测分类器Haar
  7. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
  8. Recognizer = cv2.face.LBPHFaceRecognizer_create()
  9. Recognizer.read('Training.yml')
  10. Labels = {}
  11. with open('Label.pickle' , 'rb') as file:
  12. OriginalLabel = pickle.load(file)
  13. Labels = {v:k for k,v in OriginalLabel.items()}
  14. while (True):
  15. Return ,Frame =Webcam.read() #读取该帧的画面
  16. Frame = cv2.flip(Frame , 1)
  17. GrayScale = cv2.cvtColor(Frame , cv2.COLOR_BGR2GRAY)# 6灰度处理
  18. Faces = face_cascade.detectMultiScale(GrayScale , 1.3 ,5)# 检查人脸
  19. for X,Y,W,H in Faces:
  20. ROI_GRAY = GrayScale[Y:Y+H , X:X+W]
  21. ROI_COLOR = Frame[Y:Y+H , X:X+W]
  22. ID , Confidence = Recognizer.predict(ROI_GRAY)#预测函数
  23. if Confidence >= 45:
  24. print(Labels[ID])
  25. cv2.putText(Frame , Labels[ID],(X,Y) , cv2.FONT_HERSHEY_SIMPLEX , 1,(0,0,255),2)
  26. cv2.rectangle(Frame , (X,Y) , (X+W , Y+H) , (255 , 0 , 0) , 1)
  27. # 9显示图片
  28. cv2.imshow('Image' , Frame)
  29. # 10暂停窗口
  30. if cv2.waitKey(1) == ord('q'):
  31. break
  32. # 11释放资源
  33. Webcam.release()
  34. # #12销毁窗口
  35. cv2.destroyAllWindows()

运行结果:

参考:https://www.cnblogs.com/ningxinjie/p/11542950.html

为什么使用yml文件https://blog.csdn.net/sereasuesue/article/details/1052942364

Python数据存储:pickle模块的使用讲解https://blog.csdn.net/sereasuesue/article/details/105293917

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/733704
推荐阅读
相关标签
  

闽ICP备14008679号