调用WosGetFace模块
注:为更清楚的讲解,没有对方法进行封装,在生产环境中请按策略进行封装
import WosGetFace
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)
path = config.__FaceStore__path + '/' + str(userId) + '/'
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
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)
return True
专栏文章目录
|