3 OpenCV获取人脸并保存

调用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

专栏文章目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wos.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值