调用WosGetFace模块
注:为更清楚的讲解,没有对方法进行封装,在生产环境中请按策略进行封装
# 1 - 创建人脸库
import WosGetFace
# userId 为人脸库全局Id
ifTrainer = WosGetFace.main(1308002****, [
'https://****.oss-cn-beijing.aliyuncs.com/temp/faceTest/liu/1.jpeg',
'https://****.oss-cn-beijing.aliyuncs.com/temp/faceTest/liu/2.jpeg',
'https://****.oss-cn-beijing.aliyuncs.com/temp/faceTest/liu/3.jpeg',
'https://****.oss-cn-beijing.aliyuncs.com/temp/faceTest/liu/4.jpeg',
'https:/****.oss-cn-beijing.aliyuncs.com/temp/faceTest/liu/5.jpeg'
])
print(ifTrainer)
WosGetFace.py
import config
import os
import cv2
import WosDownload
import time
import random
import WosTrain
def main(userId, images):
# 参数校正
if userId == '':
print("ERR: userId is null", userId)
return False
if images == '':
print("ERR: face is null", userId)
return False
# 下载人脸
faces = WosDownload.main(images)
# 查看user是否已经存在
path = config.__FaceStore__path + '/' + str(userId) + '/'
isExists = os.path.exists(path)
# 创建
if not isExists:
os.makedirs(path)
# opencv 模块全局路径
src = cv2.__file__
src = src.replace('__init__.py', '', 1) + 'data/'
# 搜索人脸 保存人脸
userFaces = []
for faceLocal in faces:
face_detector = cv2.CascadeClassifier(src + 'haarcascade_frontalface_default.xml')
image = cv2.imread(faceLocal)
# 图片转换成灰度图片
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
# 图片中查询人脸
faces = face_detector.detectMultiScale(gray, 1.3, 10)
# 保存这些人脸
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + w), (255, 0, 0))
# 保存图像
facePath = path + str(int(time.time())) + '-' + str(random.randint(10000, 99999)) + '.jpg'
cv2.imwrite(facePath, gray[y: y + h, x: x + w])
userFaces.append(facePath)
# 开始训练, 生产环境请自行设置
# WosTrain,main(userId)
return True