公司須要在項目中使用人臉識別SDK,而且對信息安全的要求很是高,在詳細瞭解市場上幾個主流人臉識別SDK後,綜合來看虹軟的Arcface SDK比較符合咱們的需求,它提供了免費版本,而且能夠在離線環境下使用,這一點很是符合咱們對安全性的要求。python
但有個遺憾的事情,咱們的項目主要使用了Python語言,虹軟官方並無提供Python版本的SDK,所以我本身使用Python封裝了Arcface C++ SDK,便於在項目中使用,這裏將主要過程寫出來供你們探討下。 數組
a. 注意Win64環境的Python必須使用ArcFace C++(Win64) SDK,若是平臺不一致, 不然可能會出現如下錯誤。 安全
OSError: [WinError 193] %1 不是有效的 Win32 應用程序複製代碼
b. 因爲SDK中涉及到內存操做,本文使用了ctypes包和cdll包提供的如下幾種方式 bash
c_ubyte_p = POINTER(c_ubyte) memcpy = cdll.msvcrt.memcpy malloc = cdll.msvcrt.malloc malloc.restype = c_void_p free = cdll.msvcrt.free複製代碼
在封裝數據結構時,必定要注意參數類型,不然可能會致使程序出錯。數據結構
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)] 複製代碼
a. 接口封裝以前須要加載dll庫,Arcface SDK 提供的dll都須要加載。 app
b. 本文中圖片格式使用了ASVL_PAF_RGB24_B8G8R8。 ui
c. 每一個接口都須要定義返回值以及參數類型,某些參數類型依賴前文所述的基本數據結構。 spa
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):
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.ASFFaceFeature
Compare 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)) 複製代碼
接下來按照下面的流程圖介紹接口調用(此圖使用 Microsoft Visio 2016自動生成)。3d
下圖是按照此流程處理獲得的效果圖,因爲畫面有限,只顯示了年齡、性別、活體信息。rest
須要注意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) 複製代碼
初始化須要將全部須要的功能參數一次性傳入,本文使用了人臉檢測、特徵提取等功能。
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)複製代碼
本文使用了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)複製代碼
特徵提取只支持單人臉,所以作了人臉處理操做,而且須要及時將提取的人臉特徵拷貝一份,不然會被覆蓋。
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_feature.featureSize
feature1.feature = malloc(feature1.featureSize)
memcpy(c_void_p(feature1.feature),
c_void_p(face_feature.feature),
feature1.featureSize)複製代碼
按照前文所述再提取一張人臉的特徵,便可以進行下面的人臉特徵比對操做
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)複製代碼
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)複製代碼
在活體檢測以前建議按照實際場景設置活體閾值,不設置即便用默認閾值,這裏設置了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("process失敗:", 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)複製代碼
最後,歡迎你們指教哦~
推薦大家瞭解虹軟人臉識別SDK~