python调用虹软2.0目前没有任何demo可以参考,自己研究了2个晚上终于把第一步做出来了,使用了opencv来加载和显示图片,龟速更新中
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‘,MRECT*50),(u‘faceOrient‘,c_int32*50),(u‘faceNum‘,c_int32)] 15 #人脸特征 人脸特征,人脸特征长度 16 class ASF_FaceFeature(Structure): 17 _fields_=[(‘feature‘,c_byte),(‘featureSize‘,c_int32)] 18 #年龄信息 0=未知 >0则检测出年龄 ,人脸数 19 class ASF_AgeInfo(Structure): 20 _fields_=[(‘ageArray‘,c_int32),(‘num‘,c_int32)] 21 #性别 0=男 1=女 -1未知,人脸数 22 class ASF_GenderInfo(Structure): 23 _fields_=[(‘genderArray‘,c_int32),(‘num‘,c_int32)]
1 #调用dll需要引入ctypes 2 from ctypes import * 3 from face_class import * 4 import io 5 from PIL import Image 6 import cv2 7 dll=CDLL(‘d:\python\Test\Face\lib\X64\libarcsoft_face.dll‘) 8 Facedll=CDLL(‘d:\python\Test\Face\lib\X64\libarcsoft_face_engine.dll‘) 9 #由于dll是c,所以字符串要做类型转换,否则,激活失败,APPID无效 10 Appkey=c_char_p(b‘自己去注册‘) 11 SDKey=c_char_p(b‘自己去注册‘) 12 # ASF_VERSION a=Facedll.ASFActiviation(Appkey,SDKey) 13 vs=Facedll.ASFActivation 14 #激活函数返回0,初始化成功,返回90114已激活,其他则为失败 ,重新激活需删除asf_install.dat 文件 15 ret=Facedll.ASFActivation(Appkey,SDKey) 16 print(‘激活:‘,ret) 17 #初始化引擎 18 ASF_OP_0_ONLY = 0x1 19 ASF_DETECT_MODE_VIDEO = 0x00000000 20 ASF_DETECT_MODE_IMAGE = 0xFFFFFFFF 21 r=c_void_p() 22 #初始化引擎 23 a=Facedll.ASFInitEngine(c_long(ASF_DETECT_MODE_IMAGE),c_int32(ASF_OP_0_ONLY),c_int32(16),c_int32(50),c_int8(ASF_OP_0_ONLY),byref(r)) 24 print(‘初始化‘,a) 25 print(‘初始化返回‘,r) 26 c_ubyte_p = POINTER(c_ubyte) 27 ASVL_PAF_I420 = 0x601 28 ASVL_PAF_RGB24_B8G8R8 = 0x201 29 img1=cv2.imread(‘e:/5.jpg‘) 30 sp=img1.shape 31 #调整图片大小,正好是4的倍数,否则会报错 32 img=cv2.resize(img1,(sp[1]//4*4,sp[0]//4*4)) 33 # img=cv2.cvtColor(img,cv2.COLOR_BGR2YUV_I420) 34 # img=cv2.cvtColor(img,cv2.COLOR_BGRA2YUV_I420) 35 # img=cv2.cvtColor(img,cv2.COLOR_BGR2YUV_IYUV) 36 sp=img.shape 37 wd=sp[1] 38 he=sp[0] 39 print(‘宽高‘,wd,he) 40 # cv2.imshow(‘1‘,img) 41 # cv2.waitKey(0) 42 #内存指针返回ASF_MultiFaceInfo类型--回调函数 43 tz=POINTER(ASF_MultiFaceInfo)() 44 #图片转换成字字节 45 b=bytes(img) 46 #强转为C++的byte类型 47 d=cast(b,c_ubyte_p) 48 #调用多人人脸识别 49 a=Facedll.ASFDetectFaces(r,c_int32(wd),c_int32(he),c_int32(ASVL_PAF_RGB24_B8G8R8),d,byref(tz)) 50 print(‘返回特征‘,tz) 51 print(‘返回值‘,a) 52 if a==0: 53 tezheng=tz.contents 54 print(tezheng.faceNum) 55 for i in range(0,50): 56 # cv2.rectangle(img,(tezheng.faceRect[1].left1,tezheng.faceRect[1].top1),(tezheng.faceRect[1].right1,tezheng.faceRect[1].bottom1),(255,0,0),2) 57 cv2.rectangle(img,(tezheng.faceRect[i].left1,tezheng.faceRect[i].top1),(tezheng.faceRect[i].right1,tezheng.faceRect[i].bottom1),(255,0,0),2) 58 cv2.imshow(‘1‘,img) 59 cv2.waitKey(0)
原文地址:https://www.cnblogs.com/wxt51/p/10111536.html
时间: 2024-10-06 15:45:31