赞
踩
import cv2 import os import numpy as np import sys from PIL import Image imgs=[] # 用于存储人脸数据 cl=[] # 用于存储标签 face_eneine = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml') # 获取人脸检测分类器 with open("D:/java_ceshi/python_wenjian/at.txt","r") as f: #打开预处理好的数据文件 for line in f.readlines(): str,bian=line.split(';') #通过逗号分割数据,str是人脸数据路径,bian是人脸标签 PIL_img = Image.open(str).convert('L') #打开人脸图片 img_numpy=np.array(PIL_img,'uint8') #把人脸数据转化为数组 faces = face_eneine.detectMultiScale(img_numpy,scaleFactor=1.3,minNeighbors=5) #检测人脸部分,并且把人脸坐标返回 for x,y,w,h in faces: imgs.append(img_numpy[y:y+h,x:x+w]) #把人脸数据进行分割,只保留人脸部分,并且存储到列表中 cl.append(int(bian)) #将标签存储进列表中 print(len(imgs)) print(len(cl)) recognizer = cv2.face.LBPHFaceRecognizer_create() recognizer.train(imgs,np.array(cl)) #进行模型训练 recognizer.write('renlianshib.xml') #把训练好的模型存储到这个文件中 cv2.destroyAllWindows() print("模型训练完成")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。