image文件是因为这没摄像头用的照片替换的
首先要到百度云去找到人脸登录的东西
还要导入js
在manage.py文件中
import os
import tornado.web # 生成应用
import tornado.ioloop # 启动
from tornado.options import define, parse_command_line, options
from face.views import RegisterHandler, InitDbHandler, LoginHandler
define('port', default=8080, type=int)
def make_app():
return tornado.web.Application(handlers=[
(r'/register/', RegisterHandler),
(r'/init_db/', InitDbHandler),
(r'/login/', LoginHandler)
],
template_path =os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates'),
static_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
)
if __name__ == '__main__':
parse_command_line()
app = make_app()
app.listen(options.port)
# 启动
tornado.ioloop.IOLoop.current().start()
face文件夹
模型文件 models.py
from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime
from utils.conn import Base
def init_db():
# 模型映射成表,定义的
Base.metadata.create_all()
class User(Base):
id = Column(Integer, primary_key=True, autoincrement=True)
username = Column(String(10), unique=True, nullable=False)
realname = Column(String(10), unique=True, nullable=False)
create_time = Column(DateTime, default=datetime.now)
__tablename__ = 'user'
views.py文件
import tornado.web
from face.models import User
from utils.conn import Base, session
from utils.faceId import face_register, face_login
class RegisterHandler(tornado.web.RequestHandler):
def get(self):
error = ''
self.render('register.html', error=error)
# 提交照片,保存
def post(self):
# 注册功能
# 获取数据
face = self.get_argument('face_img')
# print(face.split(',')[-1]) 取图片, 它是编码值
img = face.split(',')[-1]
username = self.get_argument('username')
realname = self.get_argument('realname')
if face and username and realname:
# 人脸注册进去 - 用户模型
user = User()
user.username = username
user.realname = realname
session.add(user)
session.commit()
# 调用百度的人脸注册接口
res = face_register(img, user.id)
if res:
# 注册成功,跳转登录
self.redirect('/login/')
else:
session.delete(user)
session.commit()
self.redirect('/register/')
# self.write('测试注册')
else:
error = '请填写完整的参数'
self.render('register.html', error=error)
class InitDbHandler(tornado.web.RequestHandler):
def get(self):
# 将模型映射到数据库中
# Base.metadata.create_all()
self.write('创建表成功')
class LoginHandler(tornado.web.RequestHandler):
def get(self):
self.render('login,html')
def post(self):
face = self.get_argument('face_img')
img = face.split(',')[-1]
res = face_login(img)
if res:
self.write('登录成功')
else:
self.write('登录失败')
在templates文件中
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
<script src="{{ static_url('js/ai.js') }}"></script>
<script src="https://code.jquery.com/jquery-3.0.0.min.js
"></script>
</head>
<body>
<input type="button" value="摄像" onchange="getMedia()">
<video height="120px" autoplay="autoplay"></video>
<!--画布-->
<canvas id="canvas1" height="220px" width="200px"></canvas>
<input type="button" value="拍照" onclick="getPhoto();">
{{ error }}
<form action="" method="post">
<!--hidden是隐藏 获取拍照图片的类型值-->
<input type="hidden" id="face" name="face_img">
<p><input type="submit" name="提交"></p>
</form>
</body>
</html>
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
<script src="{{ static_url('js/ai.js') }}"></script>
<script src="https://code.jquery.com/jquery-3.0.0.min.js
"></script>
</head>
<body>
<input type="button" value="摄像" onchange="getMedia()">
<img src="{{ static_url('image/qq1.jpg') }}" height="120px">
<video height="120px" autoplay="autoplay"></video>
<!--画布-->
<canvas id="canvas1" height="220px" width="200px"></canvas>
<input type="button" value="拍照" onclick="getPhoto();">
{{ error }}
<form action="" method="post">
<!--hidden是隐藏 获取拍照图片的类型值-->
<input type="hidden" id="face" name="face_img">
<p>账号: <input type="text" name="username"></p>
<p>真实姓名:<input type="text" name="realname"></p>
<p><input type="submit" name="提交"></p>
</form>
</body>
</html>
然后我们要连接到数据库,将图片保存到数据库中,以后登录时与数据库中的照片做对比
utils文件包中
conn.py文件中
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base # 生成base
from sqlalchemy.orm import sessionmaker
db_url = 'mysql+pymysql://root:root@127.0.0.1:3306/tornado9'
engine = create_engine(db_url)
Base = declarative_base(bind=engine)
DbSession = sessionmaker(bind=engine)
session = DbSession()
faceId.py文件中
from aip import AipFace
""" 你的 APPID AK SK """
APP_ID = '15750965'
API_KEY = 'R8vFwISg2pARS8SjFtPKZElB'
SECRET_KEY = 'kd3amWfp3Ywap02Fx3PElwKI2Fhmxdwn'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
def face_register(image, imageType='BASE64', groupId='user', userId=None):
"""
调用百度的人脸注册接口
:return:
"""
""" 调用人脸检测 """
res = client.addUser(image, imageType, groupId, userId)
# print(res)
if res['error_code']:
# 注册失败
return False
# 注册成功
return True
def face_login(image, imageType='BASE64', groupId='user'):
res = client.search(image, imageType, groupId)
# print(res)
if res['error_code']:
return False
return True