python调用虹软2.0

第一版踩了无数的坑,终于第二版把坑全添了,这次更新可以正常获取人脸数,角度,代码可读性更高,继续更新中

第三版已发出 https://www.cnblogs.com/wxt51/p/10125460.html

face_class.py
 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:pythonTestFacelibX64libarcsoft_face.dll')
 4 dll=CDLL('d:pythonTestFacelibX64libarcsoft_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)

  

原文地址:https://www.cnblogs.com/Zzz-/p/10677001.html