当前位置:   article > 正文

OpenCV人脸识别,训练模型为cv2.face.LBPHFaceRecognizer_create()

cv2.face.lbphfacerecognizer_create()

OpenCV内部自带有三种人脸检测方式:LBPH人脸识和其他两种方法(Eigen人脸识别,FisherFace人脸识别)本次主要说明第一种方式LBPH检测。

1.素材创建

1.创建需要训练的图片的集文件夹,和识别功能测试图片集的文件夹。

图(1)训练图集文件夹 

 图(1.1)taylorswift照片(尽量选用背景没其他人和尺寸小的照片,不然不好打开,也可以用resize函数进行缩小)

 图(2)识别文件夹,一样放入需要识别的照片

2.识别过程

1.使用Haar-cascade进行训练,针对与Haar-cascade的识别原理,大家可以自行Google查询,主要说明如何使用Haar-cascade进行训练。首先创建haar_cascade实例,

haar_cascade = cv2.CascadeClassifier('haar_face.xml')  # .xml文件可从opencv官网下载

2.导入文件内容(目标图片和对应标签)

  1. def create_train():
  2. # loop every folder
  3. for person in people:
  4. path = os.path.join(DIR, person)
  5. label = people.index(person)
  6. # loop every image in the folder
  7. for img in os.listdir(path):
  8. img_path = os.path.join(path, img)
  9. # read the image from the path
  10. img_array = cv2.imread(img_path)
  11. gary = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
  12. faces_rect = haar_cascade.detectMultiScale(gary, scaleFactor=1.1, minNeighbors=7)
  13. for (x, y, w, h) in faces_rect:
  14. faces_roi = gary[y:y + h, x:x + w]
  15. features.append(faces_roi)
  16. labels.append(label)

文件路径名根据自己创建的文件所在路径更改

3.用函数cv2.face.LBPHFaceRecognizer_ create ( ) 生成LBPH识别器实例模型

首先创建训练实例对象

cv2.face.LBPHFaceRecognizer_ create ( radius,neighbors,grid_ x,grid_ y, threshold)四个参数均可选,通常默认不设置

face_recognizer = cv2.face.LBPHFaceRecognizer_create()

4.训练模型

face_recognizer.train(features,labels)

通常传入两个参数scr和label

scr:训练图像,用来识别的人脸图像

label:人脸图像对于的标签,名字

5.通常需要将训练好的模型进行保存

face_recognizer.save('face_trained.yml')

6.函数face_recognizer.predict( ) 对一个待测人脸图像进行判断,寻找与当前图像距离最近的人脸图像。与哪个人脸图像最近,就将当前待测图像标注为其对应的标签。该函数有两个返回值,label和confidence。

label:返回的识别结果标签。

confidence:置信度,衡量识别结果与原有模型之间的距离。在LBPH识别方式中,0表示完全匹配。通常情况下,认为小于50的值是可以接受的,如果该值大于85则认为差别较大。

以下为代码,分为训练代码和识别代码两个部分

训练代码

  1. # face_train.py
  2. # by liangyu 2020.7.19
  3. # use opencv's built in face recognizer
  4. import os
  5. import cv2
  6. import numpy as np
  7. people = ['Avril Lavigne', 'Chris Martin', 'Emma Watson', 'Taylor Swift']
  8. # p = []
  9. # for i in os.listdir(r'E:\opencv_source'):
  10. # p.append(i)
  11. # print(p)
  12. DIR = r'E:\opencv_source'
  13. haar_cascade = cv2.CascadeClassifier('haar_face.xml') # .xml文件可从opencv官网下载
  14. features = []
  15. labels = []
  16. def create_train():
  17. # loop every folder
  18. for person in people:
  19. path = os.path.join(DIR, person)
  20. label = people.index(person)
  21. # loop every image in the folder
  22. for img in os.listdir(path):
  23. img_path = os.path.join(path, img)
  24. # read the image from the path
  25. img_array = cv2.imread(img_path)
  26. gary = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
  27. faces_rect = haar_cascade.detectMultiScale(gary, scaleFactor=1.1, minNeighbors=7)
  28. for (x, y, w, h) in faces_rect:
  29. faces_roi = gary[y:y + h, x:x + w]
  30. features.append(faces_roi)
  31. labels.append(label)
  32. create_train()
  33. # print(f'Length of the features = {len(features)}')
  34. # print(f'Length of the features = {len(labels)}')
  35. print('Training Done ------------------')
  36. features = np.array(features,dtype='object')
  37. labels = np.array(labels)
  38. face_recognizer = cv2.face.LBPHFaceRecognizer_create()
  39. # Train the Recognizer on the features list and the labels list
  40. face_recognizer.train(features,labels)
  41. # Pass two parameters: 1.scr--->the target image 2.label---->the corresponding label of the image
  42. face_recognizer.save('face_trained.yml')
  43. np.save('features.npy',features)
  44. np.save('labels.npy',labels)

识别代码

  1. # faces_recognition.py
  2. import numpy as np
  3. import cv2
  4. haar_cascade = cv2.CascadeClassifier('haar_face.xml')
  5. people = ['Avril Lavigne', 'Chris Martin', 'Emma Watson', 'Taylor Swift']
  6. features = np.load('features.npy', allow_pickle=True)
  7. labels = np.load('labels.npy', allow_pickle=True)
  8. face_recognizer = cv2.face.LBPHFaceRecognizer_create()
  9. face_recognizer.read('face_trained.yml')
  10. img = cv2.imread(r'E:\image\verify\Chris Martin\cm3.jpg')
  11. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. cv2.imshow('Person', gray)
  13. # Detect the face in the image
  14. face_rect = haar_cascade.detectMultiScale(gray, 1.1, 4)
  15. for (x, y, w, h) in face_rect:
  16. face_roi = gray[y:y + h, x:x + w]
  17. label, confidence = face_recognizer.predict(face_roi)
  18. # Return two values: label and confidence
  19. print(f'label = {people[label]} with a confidence of {confidence}')
  20. # confidence :'0' indicates an exact match,value less than 50 is considered acceptable
  21. # value greater than 85 means the difference is huge
  22. cv2.putText(img, str(people[label]), (10, 30), cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 255, 0), thickness=1)
  23. cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), thickness=2)
  24. cv2.imshow('Detected Face', img)
  25. cv2.waitKey(0)

3.识别结果展示

 对Coldplay主唱 Chris Martin进行识别可以看到与其相匹配且置信度为0表示完全匹配。

对霉霉进行识别,结果识别出来为艾玛沃特森!!置信度72表示和实际有较大的误差

4.结束语:

对于opencv内部自带的人脸检测(LBPH人脸识别)和其他两种方法(Eigen人脸识别,FisherFace人脸识别)方法大致相同,通常情况下这三种方法识别精度较低,且对噪声较为敏感,识别效果并不是十分精准,和预期还是有一定出入的。其他两种识别方法可参考这篇文章https://blog.csdn.net/weixin_43810267/article/details/106650055

Haar特征分类器介绍可以参考 (55条消息) opencv人脸检测--detectMultiScale函数_walker lee的博客-CSDN博客_detectmultiscale

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

闽ICP备14008679号