python3调用虹软3.0人脸识别

2023-10-28 17:30

本文主要是介绍python3调用虹软3.0人脸识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

python3调用虹软3.0人脸识别

    • 说明
    • 代码结构

说明

平台 windows,linux没试过应该也可以只需将虹软sdk换成linux版本即可。
软件依赖 cv2、flask、虹软sdk3版本(2版本的也可以)
main_flask是以图片的方式在浏览器上实时浏览,
main_client是一个窗口的形式展现实时监测视频数据。
代码结构
虹软的sdk怎么搞就不多说了,自己看官网。

代码结构

  1. flask程序主入口 main_flask.py
import face_dll
import face_class
from ctypes import *
import cv2
import face_function as fun
import face_feature_extract
import video_camera
from flask import Flask, abort, request, jsonify, Responseapp = Flask(__name__)Appkey = b''
SDKey = b'''''
存放人脸库的信息,key为对应的图片名即为1.jpg或者2.jpg
'''
faceInfos = {'1':{'name':'Ju Jingyi','gender':'girl','age':'25','image':'images/1.jpg'},'2':{'name':'Ju Jingyi','gender':'girl','age':'25','image':'images/2.jpg'}}'''
激活sdk,激活一次即可
'''def active():ret = fun.active(Appkey, SDKey)if ret == 0 or ret == 90114:print('激活成功:', ret)else:print('激活失败:', ret)passdef init():# 初始化 1 视频(0x00000000)或图片(0xFFFFFFFF)模式,ret = fun.init(0x00000000)if ret[0] == 0:print('初始化成功:', ret, '句柄', fun.Handle)else:print('初始化失败:', ret)def gen():videoCamera = video_camera.VideoCamera(faceFeatures, faceInfos)while True:ret, frame = videoCamera.get_frame()if ret:yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame.tobytes() + b'\r\n\r\n')'''
返回图片流
'''
@app.route('/video_feed/')
def video_feed():return Response(gen(),mimetype='multipart/x-mixed-replace; boundary=frame')if __name__ == "__main__":#active()# 加载人脸资源faceFeatures = face_feature_extract.load_face_feature(faceInfos)init()app.run(host="0.0.0.0", port=8080, debug=True, threaded=True, processes=True)
  1. 摄像头类 video_camera.py
import cv2
import face_function as fun
import face_feature_extract
import face_class'''
摄像头类
'''class VideoCamera(object):def __init__(self, faceFeatures, faceInfos):# 通过opencv获取实时视频流self.videoCapture = cv2.VideoCapture(0, cv2.CAP_DSHOW)self.frame_width = int(self.videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH))self.frame_height = int(self.videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))self.faceFeatures = faceFeaturesself.faceInfos = faceInfosdef __del__(self):self.videoCapture.release()'''将视频帧转换为字节流返回'''def get_frame(self):ret, frame = self.videoCapture.read()if ret:# 加载图片imageData = face_class.ImageData(frame, self.frame_width, self.frame_height)ret, faces = fun.detectFaces(fun.deal_image_data(imageData))if ret == 0:frame = fun.deal_frame(imageData, faces, self.faceFeatures, self.faceInfos)img_fps = 80img_param = [int(cv2.IMWRITE_JPEG_QUALITY), img_fps]# 转化ret, frame = cv2.imencode('.jpg', frame, img_param)return ret, frame
  1. 人脸识别相关函数 face_function.py
import face_dll
import 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 active(appkey, sdkey):ret = face_dll.active(appkey, sdkey)return ret# 初始化函数def init(model):'''1 视频(0x00000000)或图片(0xFFFFFFFF)模式,2 角度(),3 识别的最小人脸比例 = 图片长边 / 人脸框长边的比值 默认推荐值:VIDEO模式推荐16;IMAGE模式推荐324 最大需要检测的人脸个数,取值范围[1,50],5 需要启用的功能组合,可多选ASF_FACE_DETECT 0x00000001 //人脸检测 SF_FACERECOGNITION 0x00000004 //人脸特征 ASF_AGE 0x00000008 //年龄 ASF_GENDER 0x00000010 //性别ASF_FACE3DANGLE 0x00000020 //3D角度 ASF_LIVENESS 0x00000080 //RGB活体 ASF_IR_LIVENESS 0x00000400 //IR活体 这些属性均是以常量值进行定义,可通过 | 位运算符进行组合使用。例如 MInt32 combinedMask = ASF_FACE_DETECT | ASF_FACERECOGNITION | ASF_LIVENESS;6 返回激活句柄'''ret = face_dll.initEngine(model, 0x1, 16, 10, 5, byref(Handle))return ret, Handle# cv2记载图片并处理def LoadImg(imageData):img = cv2.imread(imageData.filepath)sp = img.shapeimg = cv2.resize(img, (sp[1]//4*4, sp[0]//4*4))sp = img.shapeimageData.image = imgimageData.width = sp[1]imageData.height = sp[0]return imageData'''
处理图片改变大小
'''def deal_image_data(imageData):shape = imageData.image.shapeimage = cv2.resize(imageData.image, (shape[1]//4*4, shape[0]//4*4))shape = image.shapeimageData.image = imageimageData.width = shape[1]imageData.height = shape[0]return imageDatadef detectFaces(imageData):faces = face_class.ASF_MultiFaceInfo()imgby = bytes(imageData.image)imgcuby = cast(imgby, c_ubyte_p)ret = face_dll.detectFaces(Handle, imageData.width, imageData.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.image, (ra.left, ra.top),(ra.right, ra.bottom), (255, 0, 0,), 2)cv2.imshow('faces', im.image)cv2.waitKey(0)# 显示人脸识别图片def showimg2(imageData, faces, faceFeatures, faceInfos):for i in range(0, faces.faceNum):# 画出人脸框ra = faces.faceRect[i]cv2.rectangle(imageData.image, (ra.left, ra.top),(ra.right, ra.bottom), (255, 0, 0,), 2)peopleName = 'unknown'res = 0.5# 提取单人1特征ft = getsingleface(faces, i)ret, faceFeature = faceFeatureExtract(imageData, ft)if ret == 0:for item in faceFeatures:ret, result = faceFeatureCompare(faceFeature, item['faceFeature'])if ret == 0:if result > res:res = resultpeopleName = faceInfos[item['id']]['name']cv2.putText(imageData.image, peopleName, (ra.left, ra.top - 5),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0,), 1, cv2.LINE_AA)cv2.imshow('faces', imageData.image)def deal_frame(imageData, faces, faceFeatures, faceInfos):for i in range(0, faces.faceNum):# 画出人脸框ra = faces.faceRect[i]cv2.rectangle(imageData.image, (ra.left, ra.top),(ra.right, ra.bottom), (255, 0, 0,), 2)peopleName = 'unknown'res = 0.5# 提取单人1特征ft = getsingleface(faces, i)ret, faceFeature = faceFeatureExtract(imageData, ft)if ret == 0:for item in faceFeatures:ret, result = faceFeatureCompare(faceFeature, item['faceFeature'])if ret == 0:if result > res:res = resultpeopleName = faceInfos[item['id']]['name']cv2.putText(imageData.image, peopleName, (ra.left, ra.top - 5),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0,), 1, cv2.LINE_AA)return imageData.image
# 提取人脸特征def faceFeatureExtract(im, ft):detectedFaces = face_class.ASF_FaceFeature()img = im.imageimgby = bytes(im.image)imgcuby = cast(imgby, c_ubyte_p)ret = face_dll.faceFeatureExtract(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)return ret, retzelse:return ret, None# 特征值比对,返回比对结果def faceFeatureCompare(faceFeature1, FaceFeature2):result = c_float()ret = face_dll.faceFeatureCompare(Handle, faceFeature1, FaceFeature2, byref(result))return ret, result.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.left = ra.leftft.faceRect.right = ra.rightft.faceRect.top = ra.topft.faceRect.bottom = ra.bottomft.faceOrient = singleface.faceOrient[index]return ft# 从文件获取特征值def ftfromfile(filepath):fas = face_class.ASF_FaceFeature()f = open(filepath, '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
  1. 人脸特征值提取face_feature_extract.py
import face_dll
import face_class
import cv2
import face_function as fun
import os
from ctypes import string_at'''
存放人脸特征值的集合
'''
faceFeatures = []'''
初始化sdk设置为图片模式以加载更为精确的特征值集合
'''def init():# 初始化ret = fun.init(0xFFFFFFFF)if ret[0] == 0:print('初始化成功:', ret, '句柄', fun.Handle)else:print('初始化失败:', ret)'''
提取图片文件里面的人脸特征值
'''def face_feature_extract(filepath):imageData = face_class.ImageLoadData(filepath)imageData = fun.LoadImg(imageData)ret, faces = fun.detectFaces(imageData)if ret == 0:# 提取单人1特征ft = fun.getsingleface(faces, 0)ret, faceFeature = fun.faceFeatureExtract(imageData, ft)return ret, faceFeature'''
读取人脸资源库所有的图片
'''def read_images(filePath):for i, j, files in os.walk(filePath):return filesdef load_face_feature(faceInfos):init()for info in faceInfos:imagePath = faceInfos[info]['image']if imagePath.find('.jpg'):ret, faceFeature = face_feature_extract(imagePath)if ret == 0:print("add faceFeature", info)faceFeatures.append({'id': info, 'faceFeature': faceFeature})return faceFeaturesif __name__ == "__main__":faceInfos = {'1':{'name':'Ju Jingyi','gender':'girl','age':'25','image':'images/1.jpg'},'2':{'name':'Ju Jingyi','gender':'girl','age':'25','image':'images/2.jpg'}}load_face_feature(faceInfos)
  1. 特征值对比 face_feature_compare.py
import face_dll
import face_class
import face_function as fun
import face_feature_extract'''
本地图片提取的特征值与内存的特征值对比
'''def face_feature_compare(faceFeature):# 结果比对faceFeatures = face_feature_extract.loadFaceFeature('images/')for item in faceFeatures:ret, result = fun.faceFeatureCompare(faceFeature, item['faceFeature'])if ret == 0:print('name %s similarity %s' % (item['name'], result))if __name__ == "__main__":ret, faceFeature = face_feature_extract.faceFeatureExtract('images/JuJingyi.jpg')if ret == 0:face_feature_compare(faceFeature)
  1. c++中的结构体python封装 face_class.py
from ctypes import c_int32, c_char_p, Structure, POINTER, c_void_p# 人脸框class MRECT(Structure):_fields_ = [(u'left', c_int32), (u'top', c_int32),(u'right', c_int32), (u'bottom', 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_ = [(u'faceRect', POINTER(MRECT)), (u'faceOrient',POINTER(c_int32)), (u'faceNum', c_int32)]# 人脸特征 人脸特征,人脸特征长度class ASF_FaceFeature(Structure):_fields_ = [('feature', c_void_p), ('featureSize', c_int32)]# 自定义图片类class ImageData:def __init__(self, image, width, height):self.image = imageself.width = widthself.height = height# 自定义图片类class ImageLoadData:def __init__(self, filepath):self.filepath = filepathself.image = Noneself.width = 0self.height = 0
  1. sdk库pyhton接口封装 face_dll.py
from ctypes import c_int32, c_char_p, c_void_p, c_float, c_size_t, c_ubyte, c_long, cdll, POINTER, CDLL
from face_class import ASF_MultiFaceInfo, ASF_SingleFaceInfo, ASF_FaceFeaturewuyongdll = CDLL('libarcsoft/libarcsoft_face.dll')
dll = CDLL('libarcsoft/libarcsoft_face_engine.dll')
dllc = cdll.msvcrt
ASF_DETECT_MODE_VIDEO = 0x00000000
ASF_DETECT_MODE_IMAGE = 0xFFFFFFFF
c_ubyte_p = POINTER(c_ubyte)# 激活
active = dll.ASFActivation
active.restype = c_int32
active.argtypes = (c_char_p, c_char_p)# 初始化
initEngine = dll.ASFInitEngine
initEngine.restype = c_int32
initEngine.argtypes = (c_long, c_int32, c_int32,c_int32, c_int32, POINTER(c_void_p))# 人脸识别
detectFaces = dll.ASFDetectFaces
detectFaces.restype = c_int32
detectFaces.argtypes = (c_void_p, c_int32, c_int32,c_int32, POINTER(c_ubyte), POINTER(ASF_MultiFaceInfo))# 特征提取
faceFeatureExtract = dll.ASFFaceFeatureExtract
faceFeatureExtract.restype = c_int32
faceFeatureExtract.argtypes = (c_void_p, c_int32, c_int32, c_int32, POINTER(c_ubyte), POINTER(ASF_SingleFaceInfo), POINTER(ASF_FaceFeature))# 特征比对
faceFeatureCompare = dll.ASFFaceFeatureCompare
faceFeatureCompare.restype = c_int32
faceFeatureCompare.argtypes = (c_void_p, POINTER(ASF_FaceFeature), POINTER(ASF_FaceFeature), POINTER(c_float))
malloc = dllc.malloc
free = dllc.free
memcpy = dllc.memcpymalloc.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)
  1. 窗口的形式展现 main_client.py
import face_dll
import face_class
from ctypes import *
import cv2
import face_function as fun
import face_feature_extractAppkey = b''
SDKey = b'''''
存放人脸库的信息,key为对应的图片名即为1.jpg或者2.jpg
'''
faceInfos = {'1':{'name':'Ju Jingyi','gender':'girl','age':'25','image':'images/1.jpg'},'2':{'name':'Ju Jingyi','gender':'girl','age':'25','image':'images/2.jpg'}}'''
激活sdk,激活一次即可
'''def active():ret = fun.active(Appkey, SDKey)if ret == 0 or ret == 90114:print('激活成功:', ret)else:print('激活失败:', ret)passdef init():# 初始化 1 视频(0x00000000)或图片(0xFFFFFFFF)模式,ret = fun.init(0x00000000)if ret[0] == 0:print('初始化成功:', ret, '句柄', fun.Handle)else:print('初始化失败:', ret)def start(faceFeatures):cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))while True:# get a frameret, frame = cap.read()if ret:# 加载图片imageData = face_class.ImageData(frame, frame_width, frame_height)ret, faces = fun.detectFaces(fun.deal_image_data(imageData))if ret == 0:fun.showimg2(imageData, faces, faceFeatures, faceInfos)else:pass# show a frameif cv2.waitKey(1) & 0xFF == ord('q'):breakcap.release()cv2.destroyAllWindows()if __name__ == "__main__":#active()# 加载人脸资源faceFeatures = face_feature_extract.load_face_feature(faceInfos)init()start(faceFeatures)

代码地址:码云仓库代码

这篇关于python3调用虹软3.0人脸识别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/294808

相关文章

Idea调用WebService的关键步骤和注意事项

《Idea调用WebService的关键步骤和注意事项》:本文主要介绍如何在Idea中调用WebService,包括理解WebService的基本概念、获取WSDL文件、阅读和理解WSDL文件、选... 目录前言一、理解WebService的基本概念二、获取WSDL文件三、阅读和理解WSDL文件四、选择对接

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

Python3中Sanic中间件的使用

《Python3中Sanic中间件的使用》Sanic框架中的中间件是一种强大的工具,本文就来介绍Python3中Sanic中间件的使用,具有一定的参考价值,感兴趣的可以了解一下... 目录Sanic 中间件的工作流程中间件的使用1. 全局中间件2. 路由中间件3. 异常处理中间件4. 异步中间件5. 优先级

java如何调用kettle设置变量和参数

《java如何调用kettle设置变量和参数》文章简要介绍了如何在Java中调用Kettle,并重点讨论了变量和参数的区别,以及在Java代码中如何正确设置和使用这些变量,避免覆盖Kettle中已设置... 目录Java调用kettle设置变量和参数java代码中变量会覆盖kettle里面设置的变量总结ja

如何在页面调用utility bar并传递参数至lwc组件

1.在app的utility item中添加lwc组件: 2.调用utility bar api的方式有两种: 方法一,通过lwc调用: import {LightningElement,api ,wire } from 'lwc';import { publish, MessageContext } from 'lightning/messageService';import Ca

4B参数秒杀GPT-3.5:MiniCPM 3.0惊艳登场!

​ 面壁智能 在 AI 的世界里,总有那么几个时刻让人惊叹不已。面壁智能推出的 MiniCPM 3.0,这个仅有4B参数的"小钢炮",正在以惊人的实力挑战着 GPT-3.5 这个曾经的AI巨人。 MiniCPM 3.0 MiniCPM 3.0 MiniCPM 3.0 目前的主要功能有: 长上下文功能:原生支持 32k 上下文长度,性能完美。我们引入了

Python3 BeautifulSoup爬虫 POJ自动提交

POJ 提交代码采用Base64加密方式 import http.cookiejarimport loggingimport urllib.parseimport urllib.requestimport base64from bs4 import BeautifulSoupfrom submitcode import SubmitCodeclass SubmitPoj():de

【LabVIEW学习篇 - 21】:DLL与API的调用

文章目录 DLL与API调用DLLAPIDLL的调用 DLL与API调用 LabVIEW虽然已经足够强大,但不同的语言在不同领域都有着自己的优势,为了强强联合,LabVIEW提供了强大的外部程序接口能力,包括DLL、CIN(C语言接口)、ActiveX、.NET、MATLAB等等。通过DLL可以使用户很方便地调用C、C++、C#、VB等编程语言写的程序以及windows自带的大

string字符会调用new分配堆内存吗

gcc的string默认大小是32个字节,字符串小于等于15直接保存在栈上,超过之后才会使用new分配。

京东物流查询|开发者调用API接口实现

快递聚合查询的优势 1、高效整合多种快递信息。2、实时动态更新。3、自动化管理流程。 聚合国内外1500家快递公司的物流信息查询服务,使用API接口查询京东物流的便捷步骤,首先选择专业的数据平台的快递API接口:物流快递查询API接口-单号查询API - 探数数据 以下示例是参考的示例代码: import requestsurl = "http://api.tanshuapi.com/a