虹软建立本地人脸库


前言

由于工作要求,找一款好用高效的人脸识别,最后看了一圈选定了虹软的免费版SDK3.0;但是由于他家使用C++开发的,我主要开发语言是python,网上找了一些虹软python接口.but,问题又来了,我们目前的人脸库是保存在本地的,网上大佬大部分都是存在数据库里;所以花了一些时间解决这个问题.记录一下

一、虹软Python接口

  1. https://blog.csdn.net/Nirvana_6174/article/details/113357484 这位大佬分享的很详细,博客里面有代码git地址.
  2. https://ai.arcsoft.com.cn/manual/docs#/222 虹软官网技术文档

二、实现方法

1.人脸引擎+人脸检测

import cv2
from arcface.engine import *

def face_engine():
    """
    功能:人脸识别引擎;
    params:
        face:检测的最大人脸数,注册时传入为1,最大可检测数量为50.
        mask:初始化引擎启用的功能
    返回:人脸引擎
    """
    # 获取人脸识别引擎
    face_engine = ArcFace()

    # 需要引擎开启的功能
    # | ASF_AGE | ASF_GENDER | ASF_FACE3DANGLE | ASF_LIVENESS | ASF_IR_LIVENESS #年龄,性别,旋转角度,两种活体检测
    mask = ASF_FACE_DETECT | ASF_FACERECOGNITION  # 人脸检测,人脸识别

    # 初始化接口
    res = face_engine.ASFInitEngine(
        ASF_DETECT_MODE_IMAGE, ASF_OP_0_ONLY, 32, 50, mask)
    # 引擎:图片,没有旋转,图片长变与人脸长边比值[2,32],最大检测人脸个数10(最大为50),开启的功能;

    if (res != MOK):
        log.logger.error(
            "User login's ASFInitEngine fail: {}".format(res))
        return None
    else:
        print(
            "User login's ASFInitEngine sucess: {}".format(res))
        return face_engine

def face_detection(img, face_engine):
    """
    多人脸检测,返回以序号为键,[人脸框,人脸特征]为值的字典
    """
    result = []
    # 人脸检测,res状态码,detectedFaces人脸检测结果.
    res, detectedFaces = face_engine.ASFDetectFaces(img)
    if res == MOK:
        # 获取读取的人脸数量
        num = detectedFaces.faceNum
        for i in range(num):
            # 将人脸信息赋值个单人脸信息数据结构体
            single_detected_face = ASF_SingleFaceInfo()
            rect = single_detected_face.faceRect = detectedFaces.faceRect[i]
            x = rect.left, rect.top, rect.right, rect.bottom
            single_detected_face.faceOrient = detectedFaces.faceOrient[i]
            res, face_feature = face_engine.ASFFaceFeatureExtract(
                img, single_detected_face)
            if (res != MOK): 
                log.logger.error(
                    "ASFFaceFeatureExtract fail: {}".format(res))
            else:  # 提取成功,赋值
                result.append([x, face_feature])
    else: 
        log.logger.error("ASFDetectFaces fail: {}".format(res))
    return result

2.人脸本地库建立

def login_(img_path, face_features_dir, res_dir, face_engine):
    '''
    功能: 将pic_path中需要注册的人脸进过人脸检测+人脸特征提取,将人脸特征写入文件入库;并在原图上绘制人脸检测的结果以供验证.
    params:
        img_dir:需要注册的人脸保存路径
        pic_path:列表,需要注册的人脸名称(不是路径)
        face_features_dir:保存的人脸库的路径
        res_dir: 人脸检测结果展示路径
        face_engine:人脸识别引擎
    '''
    # 读取图片
    img = cv2.imread(img_path,1)
    # 获取注册名称
    name = os.path.split(img_path)[1].split('.')[0]
    f_name = name+'.txt'

    # 检测图中的人脸
    faces = face_detection(img, face_engine)
    if faces:
        if len(faces) > 1:
            print(
                'There are %s faces rather than one face in image' % (len(faces)))
        # 仅提取第一张人脸注册
        rect, face_feature = faces[0]
        # 提取人脸位置信息,备用
        x1, y1, x2, y2 = rect
        # 将人脸特征写入文件,保存在人脸库中
        with open(os.path.join(face_features_dir, f_name), 'wb') as f:
            f.write(face_feature.get_feature_bytes())
        print(
            'The face feature of %s is stored into face libary (%s) successfully' % (name, face_features_dir))
        # 注册图片中展示人脸检测信息,以用来校验.
        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)  # 画框
        cv2.imwrite(os.path.join(res_dir, name+'_login.jpg'), img)
    else:
        print('There could not detect any face in image')

3. 从本地人脸库读取人脸特征

def libary_(face_libary):
    '''
    功能:读取人脸特征库中的保存的人脸特征文件,输出文件名称为健,人脸特征为值的字典;
    params: 
        face_libary:人脸特征保存路径
    return:字典
    '''
    libarys = {}
    # 遍历人脸库文件
    for i in os.listdir(face_libary):
        # 获取文件名称作为该人脸特征名称
        name = i.split('.')[0]
        # 读取文件
        with open(os.path.join(face_libary, i), 'rb') as f:
            # 实例化
            Feature = ASF_FaceFeature()
            # 将人脸特征送入
            Feature.set_feature(f.read())
            # 构建字典
            libarys[name] = Feature
    print(
        'Loading %d face features successsfully' % (len(libarys)))
    return libarys

总结

请配合大佬的python接口食用.

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值