当前位置: 移动技术网 > IT编程>脚本编程>Python > python调用虹软2.0第三版的具体使用

python调用虹软2.0第三版的具体使用

2019年03月19日  | 移动技术网IT编程  | 我要评论

真情无限继母,杨曼,新平二手房网

这一版,对虹软的功能进行了一些封装,添加了人脸特征比对,比对结果保存到文件,和从文件提取特征进行比对,大体功能基本都已经实现,可以进行下一步的应用开发了

face_class.py

from ctypes import *
#人脸框
class mrect(structure):
  _fields_=[(u'left1',c_int32),(u'top1',c_int32),(u'right1',c_int32),(u'bottom1',c_int32)]
#版本信息   版本号,构建日期,版权说明
class asf_version(structure):
  _fields_=[('version',c_char_p),('builddate',c_char_p),('copyright',c_char_p)]
#单人人脸信息 人脸狂,人脸角度
class asf_singlefaceinfo(structure):
  _fields_=[('facerect',mrect),('faceorient',c_int32)]
#多人人脸信息 人脸框数组,人脸角度数组,人脸数
class asf_multifaceinfo(structure):
  # _fields_=[('facerect',pointer(mrect)),('faceorient',pointer( c_int32)),('facenum',c_int32)]
  _fields_=[(u'facerect',pointer(mrect)),(u'faceorient',pointer(c_int32)),(u'facenum', c_int32)]
  # _fields_=[(u'facerect',mrect*50),(u'faceorient',c_int32*50),(u'facenum',c_int32)]
#人脸特征 人脸特征,人脸特征长度
class asf_facefeature(structure):
  _fields_=[('feature',c_void_p),('featuresize',c_int32)]
#自定义图片类
class im:
  def __init__(self):
    self.filepath=none
    self.date=none
    self.width=0
    self.height=0

face_dll.py

from ctypes import *
from face_class import *
wuyongdll=cdll('d:\python\test\face\lib\x64\libarcsoft_face.dll')
dll=cdll('d:\python\test\face\lib\x64\libarcsoft_face_engine.dll')
dllc=cdll.msvcrt
asf_detect_mode_video = 0x00000000
asf_detect_mode_image = 0xffffffff
c_ubyte_p = pointer(c_ubyte) 
#激活
jihuo=dll.asfactivation
jihuo.restype = c_int32
jihuo.argtypes = (c_char_p,c_char_p)
#初始化
chushihua=dll.asfinitengine
chushihua.restype=c_int32
chushihua.argtypes=(c_long,c_int32,c_int32,c_int32,c_int32,pointer(c_void_p))
#人脸识别
shibie=dll.asfdetectfaces
shibie.restype=c_int32
shibie.argtypes=(c_void_p,c_int32,c_int32,c_int32,pointer(c_ubyte),pointer(asf_multifaceinfo))
#特征提取
tezheng=dll.asffacefeatureextract
tezheng.restype=c_int32
tezheng.argtypes=(c_void_p,c_int32,c_int32,c_int32,pointer(c_ubyte),pointer(asf_singlefaceinfo),pointer(asf_facefeature))

#特征比对
bidui=dll.asffacefeaturecompare
bidui.restype=c_int32
bidui.argtypes=(c_void_p,pointer(asf_facefeature),pointer(asf_facefeature),pointer(c_float))
malloc = dllc.malloc
free = dllc.free
memcpy = dllc.memcpy

malloc.restype = c_void_p
malloc.argtypes = (c_size_t, )
free.restype = none
free.argtypes = (c_void_p, )
memcpy.restype = c_void_p
memcpy.argtypes = (c_void_p, c_void_p, c_size_t)

face_function.py

import face_dll,face_class
from ctypes import *
import cv2
from io import bytesio
# from main import *
handle=c_void_p()
c_ubyte_p = pointer(c_ubyte) 
# 激活函数
def jh(appkey,sdkey):
  ret=face_dll.jihuo(appkey,sdkey)
  return ret
# 初始化函数
def csh():# 1:视频或图片模式,2角度,3最小人脸尺寸推荐16,4最多人脸数最大50,5功能,6返回激活句柄
  ret=face_dll.chushihua(0xffffffff,0x1,16,50,5,byref(handle))
  # main.handle=handle
  return ret,handle
# cv2记载图片并处理
def loadimg(im):
  img=cv2.imread(im.filepath)
  sp=img.shape
  img=cv2.resize(img,(sp[1]//4*4,sp[0]//4*4))
  sp=img.shape
  im.data=img
  im.width=sp[1]
  im.height=sp[0]
  return im
def rlsb(im):
  faces=face_class.asf_multifaceinfo()
  img=im.data
  imgby=bytes(im.data)
  imgcuby=cast(imgby,c_ubyte_p)
  ret=face_dll.shibie(handle,im.width,im.height,0x201,imgcuby,byref(faces))
  return ret,faces
# 显示人脸识别图片
def showimg(im,faces):
  for i in range(0,faces.facenum):
    ra=faces.facerect[i]
    cv2.rectangle(im.data,(ra.left1,ra.top1),(ra.right1,ra.bottom1),(255,0,0,),2)
  cv2.imshow('faces',im.data)
  cv2.waitkey(0)
#提取人脸特征
def rltz(im,ft):
  detectedfaces=face_class.asf_facefeature()
  img=im.data
  imgby=bytes(im.data)
  imgcuby=cast(imgby,c_ubyte_p)
  ret=face_dll.tezheng(handle,im.width,im.height,0x201,imgcuby,ft,byref(detectedfaces))
  if ret==0:
    retz=face_class.asf_facefeature()
    retz.featuresize=detectedfaces.featuresize
    #必须操作内存来保留特征值,因为c++会在过程结束后自动释放内存
    retz.feature=face_dll.malloc(detectedfaces.featuresize)
    face_dll.memcpy(retz.feature,detectedfaces.feature,detectedfaces.featuresize)
    # print('提取特征成功:',detectedfaces.featuresize,mem)
    return ret,retz
  else:
    return ret
#特征值比对,返回比对结果
def bd(tz1,tz2):
  jg=c_float()
  ret=face_dll.bidui(handle,tz1,tz2,byref(jg))
  return ret,jg.value
#单人特征写入文件
def writeftfile(feature,filepath):
  f = bytesio(string_at(feature.feature,feature.featuresize))
  a=open(filepath,'wb')
  a.write(f.getvalue())
  a.close()
#从多人中提取单人数据
def getsingleface(singleface,index):
  ft=face_class.asf_singlefaceinfo()
  ra=singleface.facerect[index]
  ft.facerect.left1=ra.left1
  ft.facerect.right1=ra.right1
  ft.facerect.top1=ra.top1
  ft.facerect.bottom1=ra.bottom1
  ft.faceorient=singleface.faceorient[index]
  return ft
#从文件获取特征值
def ftfromfile(filepath):
  fas=face_class.asf_facefeature()
  f=open('d:/1.dat','rb')
  b=f.read()
  f.close()
  fas.featuresize=b.__len__()
  fas.feature=face_dll.malloc(fas.featuresize)
  face_dll.memcpy(fas.feature,b,fas.featuresize)
  return fas

main1.py

import face_dll,face_class
from ctypes import *
import cv2
import face_function as fun
appkey=b''
sdkey=b''
# 激活
ret=fun.jh(appkey,sdkey)
if ret==0 or ret==90114:
  print('激活成功:',ret)
else:
  print('激活失败:',ret)
  pass
# 初始化
ret=fun.csh()
if ret[0]==0:
  print('初始化成功:',ret,'句柄',fun.handle)
else:
  print('初始化失败:',ret)
# 加载图片
im=face_class.im()
im.filepath='e:/2.jpg'
im=fun.loadimg(im)
print(im.filepath,im.width,im.height)
# cv2.imshow('im',im.data)
# cv2.waitkey(0)
print('加载图片完成:',im)

ret=fun.rlsb(im)
if ret[0]==-1:
  print('人脸识别失败:',ret)
  pass
else:
  print('人脸识别成功:',ret)
# 显示人脸照片
# showimg(im,ret)
#提取单人1特征
ft=fun.getsingleface(ret[1],0)
tz1=fun.rltz(im,ft)[1]
#提取单人2特征
ft=fun.getsingleface(ret[1],1)
tz2=fun.rltz(im,ft)[1]
#特征保存到文件
# fun.writeftfile(tz1,'d:/1.dat')
# fun.writeftfile(tz2,'d:/2.dat')
#文件获取特征
tz=fun.ftfromfile('d:/1.dat')
jg=fun.bd(tz1,tz)
print(jg[1])
#结果比对
# jg=fun.bd(tz1,tz2)
# print(jg[1])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网