python3虹软离线人脸识别 demo

布满荆棘的人生 2022-10-02 01:53 341阅读 0赞

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

python3+虹软2.0的所有功能整合测试完成,并对虹软所有功能进行了封装,现提供demo 主要功能, 1.人脸识别 2.人脸特征提取 3.特征比对 4.特征数据存储与比对 其他特征没有添加

虹软SDK下载戳这里https://ai.arcsoft.com.cn/index.htm?utm_source=jianshu&utm_medium=referral

  1. face_class.py
  2. from ctypes import *
  3. #人脸框
  4. class MRECT(Structure):
  5. _fields_=[(u'left1',c_int32),(u'top1',c_int32),(u'right1',c_int32),(u'bottom1',c_int32)]
  6. #版本信息 版本号,构建日期,版权说明
  7. class ASF_VERSION(Structure):
  8. _fields_=[('Version',c_char_p),('BuildDate',c_char_p),('CopyRight',c_char_p)]
  9. #单人人脸信息 人脸狂,人脸角度
  10. class ASF_SingleFaceInfo(Structure):
  11. _fields_=[('faceRect',MRECT),('faceOrient',c_int32)]
  12. #多人人脸信息 人脸框数组,人脸角度数组,人脸数
  13. class ASF_MultiFaceInfo(Structure):
  14. # _fields_=[('faceRect',POINTER(MRECT)),('faceOrient',POINTER( c_int32)),('faceNum',c_int32)]
  15. _fields_=[(u'faceRect',POINTER(MRECT)),(u'faceOrient',POINTER(c_int32)),(u'faceNum', c_int32)]
  16. # _fields_=[(u'faceRect',MRECT*50),(u'faceOrient',c_int32*50),(u'faceNum',c_int32)]
  17. #人脸特征 人脸特征,人脸特征长度
  18. class ASF_FaceFeature(Structure):
  19. _fields_=[('feature',c_void_p),('featureSize',c_int32)]
  20. #自定义图片类
  21. class IM:
  22. def __init__(self):
  23. self.filepath=None
  24. self.date=None
  25. self.width=0
  26. self.height=0
  27. face_dll.py
  28. from ctypes import *
  29. from face_class import *
  30. wuyongdll=CDLL('d:\python\Test\Face\lib\X64\libarcsoft_face.dll')
  31. dll=CDLL('d:\python\Test\Face\lib\X64\libarcsoft_face_engine.dll')
  32. dllc=cdll.msvcrt
  33. ASF_DETECT_MODE_VIDEO = 0x00000000
  34. ASF_DETECT_MODE_IMAGE = 0xFFFFFFFF
  35. c_ubyte_p = POINTER(c_ubyte)
  36. #激活
  37. jihuo=dll.ASFActivation
  38. jihuo.restype = c_int32
  39. jihuo.argtypes = (c_char_p,c_char_p)
  40. #初始化
  41. chushihua=dll.ASFInitEngine
  42. chushihua.restype=c_int32
  43. chushihua.argtypes=(c_long,c_int32,c_int32,c_int32,c_int32,POINTER(c_void_p))
  44. #人脸识别
  45. shibie=dll.ASFDetectFaces
  46. shibie.restype=c_int32
  47. shibie.argtypes=(c_void_p,c_int32,c_int32,c_int32,POINTER(c_ubyte),POINTER(ASF_MultiFaceInfo))
  48. #特征提取
  49. tezheng=dll.ASFFaceFeatureExtract
  50. tezheng.restype=c_int32
  51. tezheng.argtypes=(c_void_p,c_int32,c_int32,c_int32,POINTER(c_ubyte),POINTER(ASF_SingleFaceInfo),POINTER(ASF_FaceFeature))
  52. #特征比对
  53. bidui=dll.ASFFaceFeatureCompare
  54. bidui.restype=c_int32
  55. bidui.argtypes=(c_void_p,POINTER(ASF_FaceFeature),POINTER(ASF_FaceFeature),POINTER(c_float))
  56. malloc = dllc.malloc
  57. free = dllc.free
  58. memcpy = dllc.memcpy
  59. malloc.restype = c_void_p
  60. malloc.argtypes = (c_size_t, )
  61. free.restype = None
  62. free.argtypes = (c_void_p, )
  63. memcpy.restype = c_void_p
  64. memcpy.argtypes = (c_void_p, c_void_p, c_size_t)
  65. face_function.py
  66. import face_dll,face_class
  67. from ctypes import *
  68. import cv2
  69. from io import BytesIO
  70. # from Main import *
  71. Handle=c_void_p()
  72. c_ubyte_p = POINTER(c_ubyte)
  73. # 激活函数
  74. def JH(appkey,sdkey):
  75. ret=face_dll.jihuo(appkey,sdkey)
  76. return ret
  77. # 初始化函数
  78. def CSH():# 1:视频或图片模式,2角度,3最小人脸尺寸推荐16,4最多人脸数最大50,5功能,6返回激活句柄
  79. ret=face_dll.chushihua(0xFFFFFFFF,0x1,16,50,5,byref(Handle))
  80. # Main.Handle=Handle
  81. return ret,Handle
  82. # cv2记载图片并处理
  83. def LoadImg(im):
  84. img=cv2.imread(im.filepath)
  85. sp=img.shape
  86. img=cv2.resize(img,(sp[1]//4*4,sp[0]//4*4))
  87. sp=img.shape
  88. im.data=img
  89. im.width=sp[1]
  90. im.height=sp[0]
  91. return im
  92. def RLSB(im):
  93. faces=face_class.ASF_MultiFaceInfo()
  94. img=im.data
  95. imgby=bytes(im.data)
  96. imgcuby=cast(imgby,c_ubyte_p)
  97. ret=face_dll.shibie(Handle,im.width,im.height,0x201,imgcuby,byref(faces))
  98. return ret,faces
  99. # 显示人脸识别图片
  100. def showimg(im,faces):
  101. for i in range(0,faces.faceNum):
  102. ra=faces.faceRect[i]
  103. cv2.rectangle(im.data,(ra.left1,ra.top1),(ra.right1,ra.bottom1),(255,0,0,),2)
  104. cv2.imshow('faces',im.data)
  105. cv2.waitKey(0)
  106. #提取人脸特征
  107. def RLTZ(im,ft):
  108. detectedFaces=face_class.ASF_FaceFeature()
  109. img=im.data
  110. imgby=bytes(im.data)
  111. imgcuby=cast(imgby,c_ubyte_p)
  112. ret=face_dll.tezheng(Handle,im.width,im.height,0x201,imgcuby,ft,byref(detectedFaces))
  113. if ret==0:
  114. retz=face_class.ASF_FaceFeature()
  115. retz.featureSize=detectedFaces.featureSize
  116. #必须操作内存来保留特征值,因为c++会在过程结束后自动释放内存
  117. retz.feature=face_dll.malloc(detectedFaces.featureSize)
  118. face_dll.memcpy(retz.feature,detectedFaces.feature,detectedFaces.featureSize)
  119. # print('提取特征成功:',detectedFaces.featureSize,mem)
  120. return ret,retz
  121. else:
  122. return ret
  123. #特征值比对,返回比对结果
  124. def BD(tz1,tz2):
  125. jg=c_float()
  126. ret=face_dll.bidui(Handle,tz1,tz2,byref(jg))
  127. return ret,jg.value
  128. #单人特征写入文件
  129. def writeFTFile(feature,filepath):
  130. f = BytesIO(string_at(feature.feature,feature.featureSize))
  131. a=open(filepath,'wb')
  132. a.write(f.getvalue())
  133. a.close()
  134. #从多人中提取单人数据
  135. def getsingleface(singleface,index):
  136. ft=face_class.ASF_SingleFaceInfo()
  137. ra=singleface.faceRect[index]
  138. ft.faceRect.left1=ra.left1
  139. ft.faceRect.right1=ra.right1
  140. ft.faceRect.top1=ra.top1
  141. ft.faceRect.bottom1=ra.bottom1
  142. ft.faceOrient=singleface.faceOrient[index]
  143. return ft
  144. #从文件获取特征值
  145. def ftfromfile(filepath):
  146. fas=face_class.ASF_FaceFeature()
  147. f=open('d:/1.dat','rb')
  148. b=f.read()
  149. f.close()
  150. fas.featureSize=b.__len__()
  151. fas.feature=face_dll.malloc(fas.featureSize)
  152. face_dll.memcpy(fas.feature,b,fas.featureSize)
  153. return fas
  154. Main1.py
  155. import face_dll,face_class
  156. from ctypes import *
  157. import cv2
  158. import face_function as fun
  159. Appkey=b''
  160. SDKey=b''
  161. # 激活
  162. ret=fun.JH(Appkey,SDKey)
  163. if ret==0 or ret==90114:
  164. print('激活成功:',ret)
  165. else:
  166. print('激活失败:',ret)
  167. pass
  168. # 初始化
  169. ret=fun.CSH()
  170. if ret[0]==0:
  171. print('初始化成功:',ret,'句柄',fun.Handle)
  172. else:
  173. print('初始化失败:',ret)
  174. # 加载图片
  175. im=face_class.IM()
  176. im.filepath='e:/2.jpg'
  177. im=fun.LoadImg(im)
  178. print(im.filepath,im.width,im.height)
  179. # cv2.imshow('im',im.data)
  180. # cv2.waitKey(0)
  181. print('加载图片完成:',im)
  182. ret=fun.RLSB(im)
  183. if ret[0]==-1:
  184. print('人脸识别失败:',ret)
  185. pass
  186. else:
  187. print('人脸识别成功:',ret)
  188. # 显示人脸照片
  189. # showimg(im,ret)
  190. #提取单人1特征
  191. ft=fun.getsingleface(ret[1],0)
  192. tz1=fun.RLTZ(im,ft)[1]
  193. #提取单人2特征
  194. ft=fun.getsingleface(ret[1],1)
  195. tz2=fun.RLTZ(im,ft)[1]
  196. #特征保存到文件
  197. # fun.writeFTFile(tz1,'d:/1.dat')
  198. # fun.writeFTFile(tz2,'d:/2.dat')
  199. #文件获取特征
  200. tz=fun.ftfromfile('d:/1.dat')
  201. jg=fun.BD(tz1,tz)
  202. print(jg[1])
  203. #结果比对
  204. # jg=fun.BD(tz1,tz2)
  205. # print(jg[1])

转载于:https://my.oschina.net/bangbangda/blog/3019631

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读