用Python写一个简单的脸部识别项目(3)|训练数据并测试

代码实现

makeClassifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import cv2
import numpy as np
from PIL import Image


recognizer =cv2.face.LBPHFaceRecognizer_create()
path='dataSet'

def getImagesWithID(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
faces=[]
IDs=[]
usernames=[]
for imagePath in imagePaths:
faceImg=Image.open(imagePath).convert('L')
faceNp=np.array(faceImg,'uint8')

ID=int(os.path.split(imagePath)[-1].split('.')[1])
username=os.path.split(imagePath)[-1].split('.')[2]

faces.append(faceNp)
IDs.append(ID)
usernames.append(username)

cv2.imshow("training",faceNp)
cv2.waitKey(10)

return np.array(IDs),faces,usernames

Ids,faces,usernames = getImagesWithID(path)

recognizer.train(faces, Ids)
recognizer.write('trainingData.yml')
cv2.destroyAllWindows()

本文标题:用Python写一个简单的脸部识别项目(3)|训练数据并测试

文章作者:caili-zhang

发布时间:2017年12月24日 - 05:12

最后更新:2017年12月24日 - 06:12

原始链接:https://caili-zhang.github.io/2017/12/24/python-face-03/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

据说喜欢打赏的人运气都不会太差