利用百度智能云中的人脸识别。
调用方法以及代码↓
百度智能云人脸识别的注册以及代码
先介绍一下 需要安装的东西
先win+R打开
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/69b82ce34bef2ec002e636c202ec0b06.png)
直接输入就行 ,需要安装的有 pip install baidu-aip pip install opencv-python
利用百度的人脸识别来进行操作。
如果之前没有用过可以先去https://login.bce.baidu.com 创建一个账号
进入之后
点击这个人脸识别
图片中的三个东西是在后面调用的时候需要用到的
创建一个文件夹
完整的文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-24F546qx-1615551601821)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20210312195924535.png)]
check.py
#与百度人脸识别相连接,来对当前打卡的进行检测。
from aip import AipFace
import base64
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
groupIdList = "Matrix_studio"
print("初始化完成")
imageType = "BASE64"
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
filePath = (r"capture.jpg")
def run():
f = open(filePath, "rb")
data = base64.b64encode(f.read())
f.close()
image = str(data, 'UTF-8')
result = client.search(image, imageType, groupIdList)
print(result["result"]["user_list"][0]["group_id"]) # 用户组名称
print(result["result"]["user_list"][0]["user_id"]) # 用户ID
print(result["result"]["user_list"][0]["score"]) # 相似度
if result["error_msg"] == "SUCCESS":
score = result["result"]["user_list"][0]["score"]
user_id = result["result"]["user_list"][0]["user_id"]
if score > 85:
result_ = ['yes',user_id]
print(user_id, ":识别成功")
return result_
else:
result_ = 'no'
print("人脸库无此人")
return result_
else:
print("error:", result["error_msg"])
gengxin.py
#当匹配结果正确的时候,更改此人在数据库中的打卡次数的数据。
import pymysql.cursors
import time
import check
connection = pymysql.connect(host='localhost', port=3306,
user='root', passwd='123456', database='sunck', charset='utf8')
def get(sql):
#try:
cursor = connection.cursor()
cursor.execute(sql)
results = cursor.fetchall()
return results
#except Exception:
#print("查询失败")
#finally:
# cursor.close() # 关闭游标连接
# connection.close() # 关闭数据库连接
def update(sql):
try:
cursor = connection.cursor()
cursor.execute(sql)
connection.commit()
except Exception:
print("修改失败")
#finally:
# cursor.close() # 关闭游标连接
# connection.close() # 关闭数据库连接
def geng(name_):
a_ = get('select * from myApp_people')
#res = check.run()
#print(res)
for x in a_:
#print(x)
if name_ in x:
#uname = res[1]
result = x[-3]
break
#date_=time.strftime('%y-%m-%d')
#if date_ in date_dic:
# date_dic[date_]
try:
update('update myApp_people set count=' +str(result+1) +" where NAME = '"+name_+"'")
return result+1
except:
pass
GUI.py
#向百度人脸库注册人脸的界面code。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(803, 531)
MainWindow.setMinimumSize(QtCore.QSize(803, 531))
MainWindow.setMaximumSize(QtCore.QSize(803, 531))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(10, 500, 641, 23))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 10, 640, 480))
self.label.setMinimumSize(QtCore.QSize(640, 480))
self.label.setMaximumSize(QtCore.QSize(639, 480))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("C:/Users/wangxin/Desktop/未标题-1.jpg"))
self.label.setScaledContents(False)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(660, 30, 41, 16))