OpenCV训练人脸模型并生成XML文件

《OpenCV系列教程》
项目位置:OpenCV-Sample
代码位置:23-LearnEigenFace.py
同时所需要的资源也都在项目里面

import  cv2
import os
import numpy as np

imgs = []
cl = []
imgPath = './Actor/zxc/'
model = cv2.face.EigenFaceRecognizer_create()
dirs = os.listdir(imgPath)
for f in dirs:
    file = imgPath + f
    img = cv2.imread(file, 0)
    imgs.append(img)
    cl.append(101)

array = np.array(cl)
model.train(imgs, array)
model.save('./XML/actor_zxc.xml')
cv2.destroyAllWindows()

需要注意的一点是必须转成灰度图。
训练资源也都在项目里面。
训练的是周润发的图片。
OpenCV训练人脸模型并生成XML文件_第1张图片

生成好的数据都类似这样的:




  1.7976931348623157e+308
  11
  
    1
    40000
    
d
1.4918181818181819e+02 1.4790909090909091e+02 1.4709090909090909e+02 1.4836363636363637e+02 1.4809090909090909e+02 1.4672727272727272e+02 1.4654545454545456e+02 147. 147. 1.4627272727272728e+02 1.4690909090909091e+02 1.4745454545454547e+02 1.4672727272727272e+02 1.4654545454545456e+02 1.4681818181818181e+02 1.4690909090909091e+02 1.4709090909090909e+02 1.4681818181818181e+02 1.4654545454545456e+02 147. 1.4645454545454547e+02 1.4663636363636365e+02 1.4690909090909091e+02 1.4654545454545456e+02 1.4663636363636365e+02 1.4572727272727272e+02 1.4690909090909091e+02 1.4636363636363637e+02 1.4636363636363637e+02 1.4636363636363637e+02 1.4672727272727272e+02 1.4645454545454547e+02 1.4663636363636365e+02 1.4727272727272728e+02 1.4690909090909091e+02 1.4672727272727272e+02 1.4718181818181819e+02 1.4736363636363637e+02 1.4663636363636365e+02 1.4718181818181819e+02 1.4745454545454547e+02 147. 1.4763636363636365e+02 148. 1.4763636363636365e+02 1.4790909090909091e+02 1.4763636363636365e+02 1.4827272727272728e+02 1.4781818181818181e+02 1.4772727272727272e+02 1.4845454545454547e+02 148. 1.4790909090909091e+02 1.4818181818181819e+02

你可能感兴趣的:(OpenCV)