侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

Python  /  管理员 发布于 7年前   223

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

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.msvcrtASF_DETECT_MODE_VIDEO = 0x00000000ASF_DETECT_MODE_IMAGE = 0xFFFFFFFFc_ubyte_p = POINTER(c_ubyte) #激活jihuo=dll.ASFActivationjihuo.restype = c_int32jihuo.argtypes = (c_char_p,c_char_p)#初始化chushihua=dll.ASFInitEnginechushihua.restype=c_int32chushihua.argtypes=(c_long,c_int32,c_int32,c_int32,c_int32,POINTER(c_void_p))#人脸识别shibie=dll.ASFDetectFacesshibie.restype=c_int32shibie.argtypes=(c_void_p,c_int32,c_int32,c_int32,POINTER(c_ubyte),POINTER(ASF_MultiFaceInfo))#特征提取tezheng=dll.ASFFaceFeatureExtracttezheng.restype=c_int32tezheng.argtypes=(c_void_p,c_int32,c_int32,c_int32,POINTER(c_ubyte),POINTER(ASF_SingleFaceInfo),POINTER(ASF_FaceFeature))#特征比对bidui=dll.ASFFaceFeatureComparebidui.restype=c_int32bidui.argtypes=(c_void_p,POINTER(ASF_FaceFeature),POINTER(ASF_FaceFeature),POINTER(c_float))malloc = dllc.mallocfree = dllc.freememcpy = dllc.memcpymalloc.restype = c_void_pmalloc.argtypes = (c_size_t, )free.restype = Nonefree.argtypes = (c_void_p, )memcpy.restype = c_void_pmemcpy.argtypes = (c_void_p, c_void_p, c_size_t)

face_function.py

import face_dll,face_classfrom ctypes import *import cv2from 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 imdef 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_classfrom ctypes import *import cv2import face_function as funAppkey=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)  passelse:  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使用pymongo库操作MongoDB数据库的方法实例
    下一条:
    Python实现图片转字符画的代码实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客