马梦航,男,西安工程大学电子信息学院,2021级硕士研究生
研究方向:医学影像大数据分析
电子邮件:[email protected]
孟莉苹,女,西安工程大学电子信息学院,2021级硕士研究生,张宏伟人工智能课题组
研究方向:机器视觉与人工智能
电子邮件:[email protected]
人脸识别作为一种“以貌取人”的生物智能识别技术,与其他生物识别技术相比,具有易获取、易识别、准确率高、直观性好等优点。人脸比对在人们的生活中应用广泛。通过比对两张图片中的人脸相似度并返回两两比对的得分,判断两张脸是否是同一人的可能性大小,人脸比对这一技术将辅助人们更加快捷有效地完成身份验证。目前,人脸对比检测技术主要包括:基于肤色、基于特征、基于模板匹配和基于统计学习。
华为云提供的人脸识别集合了人脸检测、人脸比对、人脸搜索等技术,广泛应用于人脸实名认证、城市公共交通、手机刷脸登陆、人脸采集等场景,无需人工干预,机器自动搜索人脸特征并完成分析,全面提升识别效率。其中,人脸比对功能根据用户选择的两张照片自动进行相似度分析,得出可能性大小,判断是否为同一人。
人脸识别提供了Web化的服务管理平台,即管理控制台,以及基于HTTPS请求的API管理方式。人脸识别以开放API的方式提供给用户,用户需要将人脸识别集成到第三方系统后才可使用。
①申请服务
②获取请求认证:①Token认证:通过Token认证调用请求。②AK/SK认证:通过AK/SK加密调用请求。AK/SK认证安全性更高。
③调用API。
在HTTP协议中,请求可以使用多种请求方法例如GET、 PUT、 POST、 DELETE、PATCH,用于指明以何种方式来访问指定的资源。在调用人脸比对API之前,需要先获取TOKEN值。访问TOKEN的协议为:url=https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens。
参数设置如图:
根据算法要求设置调用API,并上传两张人脸图片,将会返回人脸的相似度。
为了验证模型是否是根据脸部的全局特征进行人脸对比,接下来的实验上传了一个遮挡部分脸部的实验,最终结果报错。由此可见,该算法是根据人脸的全局特征,并不是根据局部特征,例如虹膜等其它生物特征。
# _*_ coding: utf-8 _*_
# @Email :[email protected]
import sys
import os
basedir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(basedir)
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import requests
import base64
from urllib3.exceptions import InsecureRequestWarning
import urllib3
urllib3.disable_warnings(InsecureRequestWarning)
class picture(QWidget):
def __init__(self):
super(picture, self).__init__()
self.resize(1200, 800)
self.setWindowTitle("人脸比对识别")
self.setWindowIcon(QIcon('logo.ico'))
self.setWindowFlags(Qt.FramelessWindowHint) # 去边框
# self.setAttribute(Qt.WA_TranslucentBackground) # 设置窗口背景透明
window_pale = QtGui.QPalette()
window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("background.png")))
self.setPalette(window_pale)
button_red = QPushButton(self)
button_red.move(20, 20)
button_red.setFixedSize(20, 20)
button_red.setStyleSheet("QPushButton{\n"
" background:#CE0000;\n"
" color:white;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"QPushButton:hover{ \n"
" background:red;\n"
"}\n"
"QPushButton:pressed{\n"
" border: 1px solid #3C3C3C!important;\n"
" background:black;\n"
"}")
button_red.clicked.connect(self.quit_button)
button_orange = QPushButton(self)
button_orange.move(50, 20)
button_orange.setFixedSize(20, 20)
button_orange.setStyleSheet("QPushButton{\n"
" background:orange;\n"
" color:white;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"QPushButton:hover{ \n"
" background:yellow;\n"
"}\n"
"QPushButton:pressed{\n"
" border: 1px solid #3C3C3C!important;\n"
" background:black;\n"
"}")
button_green = QPushButton(self)
button_green.move(80, 20)
button_green.setFixedSize(20, 20)
button_green.setStyleSheet("QPushButton{\n"
" background:green;\n"
" color:white;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"QPushButton:hover{ \n"
" background:#08BF14;\n"
"}\n"
"QPushButton:pressed{\n"
" border: 1px solid #3C3C3C!important;\n"
" background:black;\n"
"}")
button_green.clicked.connect(self.min_button)
self.label1 = QLabel(self)
self.label1.setText(" 图片1显示")
self.label1.setFixedSize(500, 400)
# self.label1.move(350, 75)
self.label1.move(80, 75)
# self.label1.setStyleSheet("QLabel{background:white;}"
# "QLabel{color:rgb(300,300,300,120);font-size:48px;font-weight:bold;font-family:微软雅黑;}"
# )
self.label2 = QLabel(self)
self.label2.setText(" 图片2显示")
self.label2.setFixedSize(500, 400)
self.label2.move(620, 75)
# self.label1.setStyleSheet("QLabel{background:white;}"
# "QLabel{color:rgb(300,300,300,120);font-size:48px;font-weight:bold;font-family:微软雅黑;}"
# )
self.label1.setStyleSheet("QLabel{\n"
" background:white;\n"
" font-family:微软雅黑;\n"
" color:black;\n"
"font-size:48px;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"}")
self.label2.setStyleSheet("QLabel{\n"
" background:white;\n"
" font-family:微软雅黑;\n"
" color:black;\n"
"font-size:48px;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"}")
self.text1 = QLineEdit(self)
self.text1.setText("人脸对比识别")
self.text1.setFixedSize(1140, 50)
self.text1.move(30, 600)
#self.text1.setFixedWidth(800);
self.text1.setStyleSheet("QLineEdit{\n"
" background:white;\n"
" font-family:微软雅黑;\n"
" color:black;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"QPushButton:hover{ \n"
" background:#00BFFF;\n"
"}\n"
"QPushButton:pressed{\n"
" border: 1px solid #3C3C3C!important;\n"
" background:black;\n"
"}")
btn1 = QPushButton(self)
btn1.setText("第一张图片")
btn1.resize(120, 80)
btn1.move(270, 500)
btn1.setStyleSheet("QPushButton{\n"
" background:#6495ED;\n"
" font-family:微软雅黑;\n"
" color:white;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"QPushButton:hover{ \n"
" background:#00BFFF;\n"
"}\n"
"QPushButton:pressed{\n"
" border: 1px solid #3C3C3C!important;\n"
" background:black;\n"
"}")
btn1.clicked.connect(self.openimage_1)
btn2 = QPushButton(self)
btn2.setText("第二张图片")
btn2.resize(120, 80)
btn2.move(810, 500)
btn2.setStyleSheet("QPushButton{\n"
" background:#6495ED;\n"
" font-family:微软雅黑;\n"
" color:white;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"QPushButton:hover{ \n"
" background:#00BFFF;\n"
"}\n"
"QPushButton:pressed{\n"
" border: 1px solid #3C3C3C!important;\n"
" background:black;\n"
"}")
btn2.clicked.connect(self.openimage_2)
btn3 = QPushButton(self)
btn3.setText("开始识别")
btn3.resize(150, 50)
btn3.move(525, 670)
btn3.setStyleSheet("QPushButton{\n"
" background:#6495ED;\n"
" font-family:微软雅黑;\n"
" color:white;\n"
" box-shadow: 1px 1px 3px;border-radius: 10px;\n"
"}\n"
"QPushButton:hover{ \n"
" background:#00BFFF;\n"
"}\n"
"QPushButton:pressed{\n"
" border: 1px solid #3C3C3C!important;\n"
" background:black;\n"
"}")
btn3.clicked.connect(self.checkimage)
def quit_button(self,event):
reply = QtWidgets.QMessageBox.question(self,
'人脸对比',
"是否要退出程序?",
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,
QtWidgets.QMessageBox.No)
if reply == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
#quit()
def full_button(self):
self.showMaximized()
def mid_button(self):
self.showNormal()
def min_button(self):
self.showMinimized( )
def openimage_1(self):
global imgName1
imgName1, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
# print(imgName1)
jpg = QtGui.QPixmap(imgName1).scaled(self.label1.width(), self.label1.height())
self.label1.setPixmap(jpg)
def openimage_2(self):
global imgName2
imgName2, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
jpg = QtGui.QPixmap(imgName2).scaled(self.label2.width(), self.label2.height())
self.label2.setPixmap(jpg)
def checkimage(self):
url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
headers = {'Content-Type': 'application/json'}
request_body = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"name": "*********",
"password": "**********",
"domain": {"name": "********"}}}},
"scope": {
"project": {
"name": "cn-north-4"}}}}
r = requests.post(url, json=request_body, headers=headers)
token = r.headers.get("X-Subject-Token")
endpoint = 'face.cn-north-4.myhuaweicloud.com' # 华北 北京4 人脸比对V2,
project_id = '0f55615fa680f5fa2fcdc01d0653a04a'
headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}
url = "https://{endpoint}/v2/{project_id}/face-compare".format(endpoint=endpoint, project_id=project_id)
with open(imgName1, "rb") as bin_data:
image1_data = bin_data.read()
with open(imgName2, "rb") as bin_data:
image2_data = bin_data.read()
image1_base64 = base64.b64encode(image1_data).decode("utf-8")
image2_base64 = base64.b64encode(image2_data).decode("utf-8")
body = {"image1_base64": image1_base64, "image2_base64": image2_base64}
response = requests.post(url, headers=headers, json=body, verify=False).json()
# print(response)
if 'similarity' not in response.keys():
response = str(response['error_msg'])
self.text1.setText('错误原因:'+response)
# print('failure', response)
else:
response = str(response['similarity'])
self.text1.setText('相似度:'+response)
# print('success', response)
def mousePressEvent(self, e):
if e.button() == Qt.LeftButton:
self.m_drag = True
self.m_DragPosition = e.globalPos() - self.pos()
e.accept()
def mouseReleaseEvent(self, e):
if e.button() == Qt.LeftButton:
self.m_drag = False
def mouseMoveEvent(self, e):
try:
if Qt.LeftButton and self.m_drag:
self.move(e.globalPos() - self.m_DragPosition)
e.accept()
except:
print("错误代码:000x0")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
my = picture()
my.show()
sys.exit(app.exec_())