登陆

怎么运用 python 接入虹软 ArcFace SDK

admin 2019-10-31 283人围观 ,发现0个评论

公司需求在项目中运用人脸辨认SDK,而且对信息安全的要求十分高,在具体了解市场上几个干流人脸辨认SDK后,归纳来看虹软的Arcface SDK比较契合咱们的需求,它供给了免费版别,而且能够在离线环境下运用,这一点十分契合咱们对安全性的要求。但有个惋惜的工作,咱们的项目首要运用了Python言语,虹软官方并没有供给Python版别的SDK,因而我自己运用Python封装了Arcface C++ SDK,便于在项目中运用,这儿将首要进程写出来供我们讨论下。

Python资源共享群:626017123

1.环境阐明

a.留意Win64环境的Python有必要运用ArcFace C++(Win64) SDK,假如渠道不一致, 不然可能会呈现以下过错。

OSError: [WinError 193] %1 不是有用的 Win32 应用程序

b.因为SDK中涉及到内存操作,本文运用了ctypes包和cdll包供给的以下几种方法

c_ubyte_p = POINTER(c_ubyte)
memcpy = cdll.msvcrt.memcpy
malloc = cdll.msvcrt.malloc
malloc.restype = c_void_p
free = cdll.msvcrt.free

2.Arcface SDK根本数据结构封装

在封装数据结构时,必定要留意参数类型,不然可能会导致程序犯错。

class MRECT(Structure): # 人脸框
_fields_ = [(u'left', c_int32),
(u'top', c_int32),
(u'right', c_int32),
(u'bottom', c_int32)]
class ASFVersion(Structure): # 版别信息 版别号 构建日期 版权阐明
_fields_ = [
('Version', c_char_p),
('BuildDate', c_char_p),
('CopyRight', c_char_p)]
class ASFSingleFaceInfo(Structure): # 单人脸信息 人脸框 人脸视点
_fields_ = [
('faceRect', MRECT),
('faceOrient', c_int32)]
class ASFMultiFaceInfo(Structure): # 多人脸信息 人脸框数组 人脸视点数组 人脸数
_fields_ = [
(u'faceRect', POINTER(MRECT)),
(u'faceOrient', POINTER(c_int32)),
(u'faceNum', c_int32)]
class ASFFaceFeature(Structure): # 人脸特征 人脸特征 人脸特征长度
_fields_ = [
('feature', c_void_p),
('featureSize', c_int32)]
class ASFFace3DAngle(Structure): # 人脸视点信息
_fields_ = [
('roll', c_void_p),
('yaw', c_void_p),
('pitch', c_void_p),
('status', c_void_p),
('num', c_int32)]
class ASFAgeInfo(Structure): # 年纪
_fields_ = [
(u'ageArray', c_void_p),
(u'num', c_int32)]
class ASFGenderInfo(Structure): # 性别
_fields_ = [
(u'genderArray', c_void_p),
(u'num', c_int32)]
class ASFLivenessThreshold(Structure): # 活体阈值
_fields_ = [
(u'thresholdmodel_BGR', c_float),
(u'thresholdmodel_IR', c_int32)]
class ASFLivenessInfo(Structure): # 活体信息
_fields_ = [
(u'isLive', c_void_p),
(u'num', c_int32)]

3.Arcface SDK接口封装

a.接口封装之前需求加载dll库,Arcface SDK 供给的dll都需求加载。

b.本文中图片格式运用了ASVL_PAF_RGB24_B8G8R8。

c.每个接口都需求界说返回值以及参数类型,某些参数类型依靠前文所述的根本数据结构。

from arcsoft_face_struct import *
from ctypes import *
from enum import Enum
face_dll = CDLL("libarcsoft_face.dll")
face_engine_dll = CDLL("libarcsoft_face_engine.dll")
ASF_DETECT_MODE_VIDEO = 0x00000000
ASF_DETECT_MODE_IMAGE = 0xFFFFFFFF
ASF_NONE = 0x00000000
ASF_FACE_DETECT = 0x00000001
ASF_FACE_RECOGNITION = 0x00000004
ASF_AGE = 0x00000008
ASF_GENDER = 0x00000010
ASF_FACE3DANGLE = 0x00000020
ASF_LIVENESS = 0x00000080
ASF_IR_LIVENESS = 0x00000400
ASVL_PAF_RGB24_B8G8R8 = 0x201
class ArcSoftFaceOrientPriority(Enum):怎么运用 python 接入虹软 ArcFace SDK
ASF_OP_0_ONLY = 0x1,
ASF_OP_90_ONLY = 0x2,
ASF_OP_270_ONLY = 0x3,
ASF_OP_180_ONLY = 0x4,
ASF_OP_0_HIGHER_EXT = 0x5,
activate = face_engine_dll.ASFActivation
activate.restype = c_int32
activate.argtypes = (c_char_p, c_char_p)
init_engine = face_engine_dll.ASFInitEngine
init_engine.restype = c_int32
init_engine.argtypes = (c_long, c_int32, c_int32, c_int32, c_int32, POINTER(c_void_p))
detect_face = face_engine_dll.ASFDetectFaces
detect_face.restype = c_int32
detect_face.argtypes = (c_void_p, c_int32, c_int32, c_int32, POINTER(c_ubyte), POINTER(ASFMultiFaceInfo))
extract_feature = face_engine_dll.ASFFaceFeatureExtract
extract_feature.restype = c_int32
extract_feature.argtypes = (c_void_p, c_int32, c_int32, c_int32, POINTER(c_ubyte),
POINTER(ASFSingleFaceInfo), POINTER(ASFFaceFeature))
compare_feature = face_engine_dll.ASFFaceFeatureCompare
compare_feature.restype = c_int32
compare_feature.argtypes = (c_void_p, POINTER(ASFFaceFeature),
POINTER(ASFFaceFeature), POINTER(c_float))
set_liveness_param = face_engine_dll.ASFSetLivenessParam
set_liveness_param.restype = c_int32
set_liveness_param.argtypes = (c_void_p, POINTER(ASFLivenessThreshold))
process = face_engine_dll.ASFProcess
process.restype = c_int32
process.argtypes = (c_void_p, c_int32, c_int32, c_int32, POINTER(c_ubyte),
POINTER(ASFMultiFaceInfo), c_int32)
get_age = face_engine_dll.ASFGetAge
get_age.restype = c_int32
get_age.argtypes = (c_void_p, POINTER(ASFAgeInfo))
get_gender = face_engine_dll.ASFGetGender
get_gender.restype = c_int32
get_gender.argtypes = (c_void_p, POINTER(ASFGenderInfo))
get_3d_angle = face_engine_dll.ASFGetFace3DAngle
get_3d_angle.restype = c_int32
get_3d_angle.argtypes = (c_void_p, POINTER(ASFFace3DAngle))
get_liveness_info = face_engine_dll.ASFGetLivenessScore
get_liveness_info.restype = c_int32
get_liveness_info.argtypes = (c_void_p, POINTER(ASFLivenessInfo))

4.封装接口调用

接下来依照下面的流程图介绍接口调用(此图运用 Microsoft Visio 2016主动生成)。

下图是依照此流程处理得到的效果图,因为画面有限,只显示了年纪、性别、活体信息。

a.激活

需求留意app_id和sdk_key需求运用字节类型。

app_id = b""
sdk_key = b""
ret = arcsoft_face_func.activate(app_id, sdk_key) # 激活
if ret == 0 or ret == 90114:
print("激活成功")
else:
print("激活失利:", ret)

b.初始化

初始化需求将一切需求的功用参数一次性传入,本文运用了人脸检测、特征提取等功用。

mask = arcsoft_face_func.ASF_FACE_DETECT | \
arcsoft_face_func.ASF_FACE_RECOGNITION | \
arcsoft_face_func.ASF_AGE | \
arcsoft_face_func.ASF_GENDER | \
arcsoft_face_func.ASF_FACE3DANGLE |\
arcsoft_face_func.ASF_LIVENESS
engine = c_void_p()
ret = arcsoft_face_func.init_engine(arcsoft_face_func.ASF_DETECT_MODE_IMAGE,
arcsoft_face_func.ArcSoftFaceOrientPriority.ASF_OP_0_ONLY.value[0],
30, 10, mask, byref(engine))
if ret == 0:
print("初始化成功")
else:
print("初始化失利:", ret)

c.人脸检测

本文运用了opencv读图,兼容性更好,而且自界说的数据结构记载图片信息,留意 ArcFace C++ SDK 要求传入的图画宽度需求是4的倍数,下面做了裁剪。

class Image:
def __init__(self):
self.width = 0
self.height = 0
self.imageData = None
def load_image(file_path):
img = cv2.imread(file_path)
sp = img.shape
img = cv2.resize(img, (sp[1]//4*4, sp[0]))# 四字节对齐
image = Image()
image.width = img.shape[1]
image.height = img.shape[0]
image.imageData = img
return image
###################### 人脸检测 ##################################
image1 = load_image(r"1.jpg")
image_bytes = bytes(image1.imageData)
image_ubytes = cast(image_bytes, c_ubyte_p)
detect_faces = ASFMultiFaceInfo()
ret = arcsoft_face_func.detect_face(
engine,
image1.width,
image1.height,
arcsoft_face_func.ASVL_PAF_RGB24_B8G8R8,
image_ubytes,
byref(detect_faces)
)
if ret == 0:
print("检测人脸成功")
else:
print("检测人脸失利:", ret)

d.特征提取

特征提取只支撑单人脸,因而做了人脸处理操作,而且需求及时将提取的人脸特征复制一份,不然会被掩盖。

single_face1 = ASFSingleFaceInfo()
single_face1.faceRect = detect_faces.faceRect[0]
single_face1.faceOrient = detect_faces.faceOrient[0]
face_feature = ASFFaceFeature()
ret = arcsoft_face_func.extract_feature(
engine,
image1.width,
image1.height,
arcsoft_face_func.ASVL_PAF_RGB24_B8G8R8,
image_ubytes,
single_face1,
byref(face_feature)
)
if ret == 0:
print("提取特征1成功")
else:
print("提取特征1失利:", ret)
feature1 = ASFFaceFeature()
feature1.featureSize = face_怎么运用 python 接入虹软 ArcFace SDKfeature.featureSize
feature1.feature = malloc(feature1.featureSize)
memcpy(c_void_p(feature1.feature),
c_void_p(face_feature.feature),
feature1.featureSize)

e.特征比对

依照前文所述再提取一张人脸的特征,即能够进行下面的人脸特征比对操作

compare_threshold = c_float()
ret = arcsoft_face_func.compare_feature(
engine, feature1, feature2, compare_threshold
)
free(c_void_p(feature1.feature))
free(c_void_p(feature2.feature))
if ret == 0:
print("特征比对成功,类似度:", compare_threshold.value)
else:
print("特征比对失利:", ret)

f.年纪、性别、3D Angle

process接口现在供给了 年纪、性别、3D Angle、活体检测, 但年纪、性别、3D Angle支撑多人脸,而活体只支撑单人脸,因而下面别离处理。

process_mask = arcsoft_face_func.ASF_AGE | \
arcsoft_face_func.ASF_GENDER | \
arcsoft_face_func.ASF_FACE3DANGLE
ret = arcsoft_face_func.process(
engine,
image1.width,
image1.height,
arcsoft_face_func.ASVL_PAF_RGB24_B8G8R8,
image_ubytes,
byref(detect_faces),
c_int32(process_mask)
)
if ret == 0:
print("process成功")
else:
print("process失利:", ret)
######################## Age ################################
age_info = ASFAgeInfo()
ret = arcsoft_face_func.get_age(engine, byref(age_info))
if ret == 0:
print("get_age 成功")
age_ptr = cast(age_info.ageArray, POINTER(c_int))
for i in range(age_info.num):
print("face", i, "age:", age_ptr[i])
else:
print("get_age 失利:", ret)
####################### Gender #################################
gender_info = ASFGenderInfo()
ret = arcsoft_face_func.get_gender(engine, byref(gender_info))
if ret == 0:
print("get_gender 成功")
gender_ptr = cast(gender_info.genderArray, POINTER(c_int))
for i in range(gender_info.num):
print("face", i, "gender:",
"女人" if (gender_ptr[i] == 1) else (
"男性" if (gender_ptr[i] == 0) else "不知道"
))
else:
print("get_gender 失利:", ret)
####################### 3D Angle #################################
angle_info = ASFFace3DAngle()
ret = arcsoft_face_func.get_3d_angle(engine, byref(angle_info))
if ret == 0:
print("get_3d_angle 成功")
roll_ptr = cast(angle_info.roll, POINTER(c_float))
yaw_ptr = 金牛座男生cast(angle_info.yaw, POINTER(c_float))
pitch_ptr = cast(angle_info.pitch, POINTER(c_float))
status_ptr = cast(angle_info.status, POINTER(c_int32))
for i in range(angle_info.num):
print("face", i,
"roll:", roll_ptr[i],
"yaw:", yaw_ptr[i],
"pitch:", pitch_ptr[i],
"status:", "正常" if status_ptr[i] == 0 else "犯错")
else:
print("get_3d_angle 失利:", ret)

g.RGB活体

在活体检测之前主张依照实践场景设置活体阈值,不设置即运用默许阈值,这儿设置了RGB活体的阈值为0.75。并将检测的多人脸别离转为单张人脸的参数传到接口中。

######################### 活体阈值设置 ###############################
threshold_param = ASFLivenessThreshold()
threshold_param.thresholdmodel_BGR = 0.75
ret = arcsoft_face_func.set_liveness_param(engine,threshold_param)
if ret == 0:
print("set_liveness_param成功")
else:
print("set_liveness_param 失利:", ret)
temp_face_info = ASFMultiFaceInfo()
temp_face_info.faceNum = 1
LP_MRECT = POINTER(MRECT)
temp_face_info.faceRect = LP_MRECT(MRECT(malloc(sizeof(MRECT))))
LP_c_long = POINTER(c_long)
temp_face_info.faceOrient = LP_c_long(c_long(malloc(sizeof(c_long))))
for i in range(detect_faces.faceNum):
temp_face_info.faceRect[0] = detect_faces.faceRect[i]
temp_face_info.faceOrient[0] = detect_faces.faceOrient[i]
ret = arcsoft_face_func.process(
engine,
image1.width,
image1.height,
arcsoft_face_func.ASVL_PAF_RGB24_B8G8R8,
image_ubytes,
byref(temp_face_info),
c_int32(arcsoft_face_func.ASF_LIVENESS)
)
if ret == 0:
print("process成功")
else:
print("process失利:", ret)
## RGB活体检测
ret = arcsoft_face_func.process(
engine,
image1.width,
image1.height,
arcsoft_face_func.ASVL_PAF_RGB24_B8G8R8,
image_ubytes,
byref(temp_face_info),
c_int32(arcsoft_face_func.ASF_LIVENESS)
)
if ret == 0:
print("process成功")
else:
print("pr怎么运用 python 接入虹软 ArcFace SDKocess失利:", ret)
liveness_info = ASFLivenessInfo()
ret = arcsoft_face_func.get_liveness_info(engine, byref(liveness_info))
if ret == 0:
print("get_liveness_info 成功")
liveness_ptr = cast(liveness_info.isLive, POINTER(c_int))
print("face", i, "liveness:",
"非真人" if (liveness_ptr[0] == 0) else (
"真人" if (liveness_ptr[0] == 1) else (
"不确定" if (liveness_ptr[0] == -1) else (
"传入人脸数>1" if (liveness_ptr[0] == -2) else
(liveness_ptr[0])
)
)
))
else:
print("get_liveness_info 失利:", ret)
请关注微信公众号
微信二维码
不容错过
Powered By Z-BlogPHP