初版踩了無數的坑,終於第二版把坑全添了,此次更新能夠正常獲取人臉數,角度,代碼可讀性更高,繼續更新中html
第三版已發出 https://www.cnblogs.com/wxt51/p/10125460.html face_class.pypython
1 from ctypes import * 2 #人臉框 3 class MRECT(Structure): 4 _fields_=[(u'left1',c_int32),(u'top1',c_int32),(u'right1',c_int32),(u'bottom1',c_int32)] 5 #版本信息 版本號,構建日期,版權說明 6 class ASF_VERSION(Structure): 7 _fields_=[('Version',c_char_p),('BuildDate',c_char_p),('CopyRight',c_char_p)] 8 #單人人臉信息 人臉狂,人臉角度 9 class ASF_SingleFaceInfo(Structure): 10 _fields_=[('faceRect',MRECT),('faceOrient',c_int32)] 11 #多人人臉信息 人臉框數組,人臉角度數組,人臉數 12 class ASF_MultiFaceInfo(Structure): 13 # _fields_=[('faceRect',POINTER(MRECT)),('faceOrient',POINTER( c_int32)),('faceNum',c_int32)] 14 _fields_=[(u'faceRect',POINTER(MRECT)),(u'faceOrient',POINTER(c_int32)),(u'faceNum', c_int32)] 15 # _fields_=[(u'faceRect',MRECT*50),(u'faceOrient',c_int32*50),(u'faceNum',c_int32)] 16 #人臉特徵 人臉特徵,人臉特徵長度 17 class ASF_FaceFeature(Structure): 18 _fields_=[('feature',c_void_p),('featureSize',c_int32)] 19 #自定義圖片類 20 class IM: 21 def __init__(self): 22 self.filepath=None 23 self.date=None 24 self.width=0 25 self.height=0
face_dll.py數組
1 from ctypes import * 2 from face_class import * 3 wuyongdll=CDLL('d:\python\Test\Face\lib\X64\libarcsoft_face.dll') 4 dll=CDLL('d:\python\Test\Face\lib\X64\libarcsoft_face_engine.dll') 5 ASF_DETECT_MODE_VIDEO = 0x00000000 6 ASF_DETECT_MODE_IMAGE = 0xFFFFFFFF 7 c_ubyte_p = POINTER(c_ubyte) 8 #激活 9 jihuo=dll.ASFActivation 10 jihuo.restype = c_int32 11 jihuo.argtypes = (c_char_p,c_char_p) 12 #初始化 13 chushihua=dll.ASFInitEngine 14 chushihua.restype=c_int32 15 chushihua.argtypes=(c_long,c_int32,c_int32,c_int32,c_int32,POINTER(c_void_p)) 16 #人臉識別 17 shibie=dll.ASFDetectFaces 18 shibie.restype=c_int32 19 shibie.argtypes=(c_void_p,c_int32,c_int32,c_int32,POINTER(c_ubyte),POINTER(ASF_MultiFaceInfo))
main.py函數
1 import face_dll,face_class 2 from ctypes import * 3 import cv2 4 Appkey=b'' 5 SDKey=b'' 6 Handle=c_void_p() #全局句柄 7 c_ubyte_p = POINTER(c_ubyte) 8 # 激活函數 9 def JH(): 10 ret=face_dll.jihuo(Appkey,SDKey) 11 return ret 12 # 初始化函數 13 def CSH():# 1:視頻或圖片模式,2角度,3最小人臉尺寸推薦16,4最多人臉數最大50,5功能,6返回激活句柄 14 ret=face_dll.chushihua(0xFFFFFFFF,0x1,16,50,5,byref(Handle)) 15 return ret 16 # cv2記載圖片並處理 17 def LoadImg(im): 18 img=cv2.imread(im.filepath) 19 sp=img.shape 20 img=cv2.resize(img,(sp[1]//4*4,sp[0]//4*4)) 21 sp=img.shape 22 im.data=img 23 im.width=sp[1] 24 im.height=sp[0] 25 return im 26 def RLSB(im): 27 faces=face_class.ASF_MultiFaceInfo() 28 img=im.data 29 imgby=bytes(im.data) 30 imgcuby=cast(imgby,c_ubyte_p) 31 ret=face_dll.shibie(Handle,im.width,im.height,0x201,imgcuby,byref(faces)) 32 # print('ret',faces.faceNum) 33 # for i in range(0,faces.faceNum): 34 # rr=faces.faceRect[i] 35 # print('range',rr.left1) 36 # print('jd',faces.faceOrient[i]) 37 if ret==0: 38 return faces 39 else: 40 return ret 41 # 激活 42 ret=JH() 43 if ret==0 or ret==90114: 44 print('激活成功:',ret) 45 else: 46 print('激活失敗:',ret) 47 pass 48 # 初始化 49 ret=CSH() 50 if ret==0: 51 print('初始化成功:',ret,'句柄',Handle) 52 else: 53 print('初始化失敗:',ret) 54 # 顯示人臉識別圖片 55 def showimg(im,faces): 56 for i in range(0,faces.faceNum): 57 ra=faces.faceRect[i] 58 cv2.rectangle(im.data,(ra.left1,ra.top1),(ra.right1,ra.bottom1),(255,0,0,),2) 59 cv2.imshow('faces',im.data) 60 cv2.waitKey(0) 61 # 加載圖片 62 im=face_class.IM() 63 im.filepath='e:/4.jpg' 64 im=LoadImg(im) 65 print(im.filepath,im.width,im.height) 66 # cv2.imshow('im',im.data) 67 # cv2.waitKey(0) 68 print('加載圖片完成:',im) 69 70 ret=RLSB(im) 71 if ret==-1: 72 print('人臉識別失敗:',ret) 73 pass 74 else: 75 print('人臉識別成功:',ret) 76 # 顯示人臉照片 77 showimg(im,ret)