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

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调用虹软2.0第三版的更多相关文章

  1. python调用虹软2.0(全网首发)-更新中

    python调用虹软2.0目前没有任何demo可以参考,自己研究了2个晚上终于把第一步做出来了,使用了opencv来加载和显示图片,龟速更新中 这一版作废,新版已发出:https://www.cnbl ...

  2. python调用虹软2.0第二版

    第一版踩了无数的坑,终于第二版把坑全添了,这次更新可以正常获取人脸数,角度,代码可读性更高,继续更新中 第三版已发出 https://www.cnblogs.com/wxt51/p/10125460. ...

  3. python调用虹软2.0

    第一版踩了无数的坑,终于第二版把坑全添了,这次更新可以正常获取人脸数,角度,代码可读性更高,继续更新中 第三版已发出 https://www.cnblogs.com/wxt51/p/10125460. ...

  4. Python调用ansible API系列(三)带有callback的执行adhoc和playbook

    在第二篇文章中虽然可以执行adhoc和playbook但是执行结果的输出并不是特别直观,虽然没有报错但是到底什么结果其实你是不知道的尤其是在执行adhoc的时候,这时候我们要利用callback来设置 ...

  5. python全栈开放实践第三版第一章的练习题完成情况

    练习题: 1.简述编译型与解释型语言的区别,且分别列出你知道哪些语言属于编译型,哪些数以解释型.1 编译型:只须编译一次就可以把源代码编译成机器语言,后面的执行无须重新编译,直接使用之前的编译结果就可 ...

  6. Python调用C/C++程序

    编程中会遇到调用其他语言到库,这里记录一下Python调用C++. Python底层是C, 所以调用C还是比较方便.调用C++有些麻烦. Python提供了ctypes, 方便将Python类型转为C ...

  7. Python黑帽编程3.0 第三章 网络接口层攻击基础知识

    3.0 第三章 网络接口层攻击基础知识 首先还是要提醒各位同学,在学习本章之前,请认真的学习TCP/IP体系结构的相关知识,本系列教程在这方面只会浅尝辄止. 本节简单概述下OSI七层模型和TCP/IP ...

  8. selenium webdriver (python) 第三版

    感谢 感谢购买第二版的同学,谢谢你们对本人劳动成果的支持!也正是你们时常问我还出不出第三版了,也是你们的鼓励,让我继续学习整理本文档. 感谢乙醇前辈,第二版的文档是放在他的淘宝网站上卖的,感谢他的帮忙 ...

  9. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

随机推荐

  1. Fuzzy and fun on Air Jordan 12 Doernbecher design

    Carissa Navarro keeps it warm, fuzzy and fun on her 2017 Air Jordan 12 Doernbecher design. Nike's 20 ...

  2. thinkphp 隐藏表单验证原理

    function savetoken() //创建session('hash') ,然后在魔板中表单中加入隐藏域 getsession('hash'),提交表单验证值是否一样,如果一样验证通过,同时重 ...

  3. 如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?

    如何在SQL Server查询语句(Select)中检索存储过程(Store Procedure)的结果集?(2006-12-14 09:25:36) 与这个问题具有相同性质的其他描述还包括:如何 ...

  4. JavaScript循环练习2

    折纸:折多少次和珠穆朗玛峰一样高1.一张纸的厚度是0.0001米,将纸对折,对折多少次厚度超过珠峰高度8848米 var hou = 0.0001; var cishu = 0; for(var i= ...

  5. 安卓备份 To Do(待办事项)的数据库

    真正路径:/data/data/com.mediatek.todos/databases/todos.db 使用过链接的路径:/data/user/0/com.mediatek.todos/datab ...

  6. java常用类总结

    0.jar包下载地点 http://mvnrepository.org/ 1.序列化反序列化Object代码 百度云:http://pan.baidu.com/disk/home#list/path= ...

  7. Intermediate Python for Data Science learning 3 - Customization

    Customization from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotl ...

  8. Linux中顿号

    ``的作用是运行``之间的命令,并且将命令运行的结果返回.一般shell脚本应该是这样:result=`ls -l` (用你的命令替换ls -l,这里只是举例)这样,result就有``里面的运行结果 ...

  9. 论文笔记:语音情感识别(三)手工特征+CRNN

    一:Emotion Recognition from Human Speech Using Temporal Information and Deep Learning(2018 InterSpeec ...

  10. python pip list 命令列出所有安装包和版本信息

    c:\Python27\Scripts>pip listDEPRECATION: The default format will switch to columns in the future. ...