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


Posted in Python onFebruary 22, 2019

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

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])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python 过滤字符串的技巧,map与itertools.imap
Sep 06 Python
在Gnumeric下使用Python脚本操作表格的教程
Apr 14 Python
实例探究Python以并发方式编写高性能端口扫描器的方法
Jun 14 Python
使用pyecharts无法import Bar的解决方案
Apr 23 Python
Django中利用filter与simple_tag为前端自定义函数的实现方法
Jun 15 Python
神经网络(BP)算法Python实现及应用
Apr 16 Python
解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题
Oct 17 Python
自定义django admin model表单提交的例子
Aug 23 Python
PyQt使用QPropertyAnimation开发简单动画
Apr 02 Python
Python yield生成器和return对比代码实例
Apr 20 Python
Python使用sqlite3模块内置数据库
May 07 Python
Python3与fastdfs分布式文件系统如何实现交互
Jun 23 Python
Python实现图片转字符画的代码实例
Feb 22 #Python
Python中正则表达式的用法总结
Feb 22 #Python
python ddt数据驱动最简实例代码
Feb 22 #Python
Flask框架踩坑之ajax跨域请求实现
Feb 22 #Python
Scrapy框架爬取Boss直聘网Python职位信息的源码
Feb 22 #Python
pandas修改DataFrame列名的实现方法
Feb 22 #Python
pyhanlp安装介绍和简单应用
Feb 22 #Python
You might like
PHP+redis实现的限制抢购防止商品超发功能详解
2019/09/19 PHP
关于laravel后台模板laravel-admin select框的使用详解
2019/10/03 PHP
Laravel 5.1 框架Blade模板引擎用法实例分析
2020/01/04 PHP
用JavaScript调用WebService的示例
2008/04/07 Javascript
详解JavaScript函数绑定
2013/08/18 Javascript
取得元素的左和上偏移量的方法
2014/09/17 Javascript
jQuery中removeProp()方法用法实例
2015/01/05 Javascript
js获取Html元素的实际宽度高度的方法
2016/05/19 Javascript
jQuery基于xml格式数据实现模糊查询及分页功能的方法
2016/12/25 Javascript
js 递归和定时器的实例解析
2017/02/03 Javascript
nodejs根据ip数组在百度地图中进行定位
2017/03/06 NodeJs
AngularJS动态绑定ng-options的ng-model实例代码
2017/06/21 Javascript
浅谈 vue 中的 watcher
2017/12/04 Javascript
浅析vue深复制
2018/01/29 Javascript
VUE组件中的 Drawer 抽屉实现代码
2019/08/06 Javascript
vue实现表格过滤功能
2019/09/27 Javascript
openlayers实现地图测距测面
2020/09/25 Javascript
[01:31:03]DOTA2完美盛典全回顾 见证十五项大奖花落谁家
2017/11/28 DOTA
[01:23:59]2018DOTA2亚洲邀请赛 4.1 小组赛 B组 VP vs Secret
2018/04/03 DOTA
Python的Flask框架标配模板引擎Jinja2的使用教程
2016/07/12 Python
Python2和Python3之间的str处理方式导致乱码的讲解
2019/01/03 Python
VS2019+python3.7+opencv4.1+tensorflow1.13配置详解
2020/04/16 Python
StubHub美国:购买或出售您的门票
2019/07/09 全球购物
毕业生自我推荐
2013/11/04 职场文书
简历的自我评价
2014/02/03 职场文书
护士求职自荐信范文
2014/03/19 职场文书
司仪主持词两篇
2014/03/22 职场文书
小学数学课题方案
2014/06/15 职场文书
化工工艺设计求职信
2014/06/25 职场文书
个人工作总结范文2014
2014/11/07 职场文书
违反工作规定检讨书范文
2014/12/14 职场文书
教师节感谢信
2015/01/22 职场文书
给女朋友的道歉短信
2015/05/12 职场文书
现实表现证明材料
2015/06/19 职场文书
世界名著读书笔记
2015/06/25 职场文书
竞选稿之小学班干部
2019/10/31 职场文书