├── README.md ├── assets ├── database.png ├── login.png ├── login2.png └── register.png ├── faceid.py ├── model ├── db.py └── models.py └── web.py /README.md: -------------------------------------------------------------------------------- 1 | # python-web:face registration and login 2 | 3 | Building a personal website, realized face registration and login. 4 | 5 | This system uses Mysql as a database tool, developed using python3.6, connected to the database using the third-party package pymysql, the web framework used tornado5.0. 6 | 7 | #### PATH setting 8 | 9 | Place your own html file in the /templates, the css file in the /static/css, the image file in the /static/image, and the js file in the /static/js. 10 | #### DATABASE setting 11 | 12 | Just configure your own database and call pymysql.connect(), refer to the official documentation. The configuration dictionary format is as follows 13 | 14 | ```python 15 | PY_MYSQL_CONN_DICT = { 16 | 'host' : 'localhost', # This database is in the same server as the web. 17 | 'user' : 'user_name', 18 | 'password' : 'password', 19 | 'database' : 'db_name', 20 | 'port' : port, 21 | 'charset' : 'utf8'} 22 | ``` 23 | 24 | #### Face API setting 25 | 26 | You need to create a new AipFace, because this part is to call Baidu's API, the code is as follows: 27 | ```python 28 | from aip import AipFace 29 | 30 | APP_ID = 'your APP ID' 31 | API_KEY = 'your Api Key' 32 | SECRET_KEY = 'your Secret Key' 33 | 34 | client = AipFace(APP_ID, API_KEY, SECRET_KEY) 35 | ``` 36 | In the above code, the constant APP_ID is created in the Baidu cloud console. The constant API_KEY and SECRET_KEY are the strings assigned to the user after the application is created. They are used to identify the user and perform signature verification for the access. View in the list of apps in the service console 37 | 38 | #### server 39 | 40 | - os:CentOS 6 x64 41 | - language:Python3.6 42 | - database:Mysql-server 5.1.73 43 | - First we need to install Mysql and Python3.6, then install tornado and pymysql. you can use `pip install {packagename}==version 44 | ` 45 | - run `python {filename.py}`. In order to keep running, you can use the screen command or the nohup command. 46 | 47 | 48 | 49 | register 50 | 51 | ![register](assets/register.png) 52 | 53 | login 54 | 55 | ![login](assets/login.png) 56 | ![login2](assets/login2.png) 57 | 58 | database 59 | 60 | ![database](assets/database.png) 61 | 62 | -------------------------------------------------------------------------------- /assets/database.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongnianwang/python-web-face-recognition/3123ecacfe0a87785592a69c4d85eceeba915b82/assets/database.png -------------------------------------------------------------------------------- /assets/login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongnianwang/python-web-face-recognition/3123ecacfe0a87785592a69c4d85eceeba915b82/assets/login.png -------------------------------------------------------------------------------- /assets/login2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongnianwang/python-web-face-recognition/3123ecacfe0a87785592a69c4d85eceeba915b82/assets/login2.png -------------------------------------------------------------------------------- /assets/register.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongnianwang/python-web-face-recognition/3123ecacfe0a87785592a69c4d85eceeba915b82/assets/register.png -------------------------------------------------------------------------------- /faceid.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from aip import AipFace 4 | 5 | APP_ID = 'your APP ID' 6 | API_KEY = 'your Api Key' 7 | SECRET_KEY = 'your Secret Key' 8 | 9 | client = AipFace(APP_ID, API_KEY, SECRET_KEY) 10 | 11 | def face_register(image, group_id, user_id, image_type = 'BASE64',options = None): 12 | # 注册 13 | # image = image_to_base64(image) 14 | res = client.addUser(image, image_type, group_id, user_id, options) 15 | print(res) 16 | if res['error_msg'] != 'SUCCESS': 17 | return False 18 | return True 19 | 20 | # 验证 21 | """ 22 | TODO: 23 | 活体验证 24 | """ 25 | def face_valid(image,group_id_list, imageType = 'BASE64',options=None): 26 | 27 | res = client.search(image, imageType, group_id_list, options) 28 | try: 29 | if [item[key] for item in res['result']['user_list'] for key in item][-1] > 80: # score>80 30 | return {'result': True, 'info': res['result']['user_list']} 31 | except Exception as e: 32 | print(e) 33 | return {'result': False, 'info': '检测失败!'} 34 | 35 | 36 | if __name__ == '__main__': 37 | 38 | image_type = "BASE64" 39 | group_id = "group11" 40 | user_id = "user11" 41 | 42 | res = client.addUser(image, image_type, group_id, user_id, options=None) 43 | print (res) 44 | -------------------------------------------------------------------------------- /model/db.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import pymysql 4 | 5 | PY_MYSQL_CONN_DICT = { 6 | 'host': 'localhost', # This database is in the same server as the web. 7 | 'user': 'user_name', 8 | 'password': 'password', 9 | 'database': 'db_name', 10 | 'port': port, 11 | 'charset': 'utf8' 12 | } 13 | 14 | class DbConnection: 15 | 16 | def __init__(self): 17 | self.__conn_dict = PY_MYSQL_CONN_DICT 18 | self.conn = None 19 | self.cursor = None 20 | 21 | def connect(self, cursor=pymysql.cursors.DictCursor): 22 | self.conn = pymysql.connect(**self.__conn_dict) 23 | self.cursor = self.conn.cursor(cursor=cursor) 24 | return self.cursor 25 | 26 | def close(self): 27 | self.conn.commit() 28 | self.cursor.close() 29 | self.conn.close() 30 | 31 | -------------------------------------------------------------------------------- /model/models.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from .db import DbConnection 4 | import time 5 | 6 | class UserModel(DbConnection): 7 | 8 | def add_user(self, **kwargs): 9 | # 往数据里添加用户信息 10 | try: 11 | self.connect() 12 | sql = "insert into user (username, groupname , c_time) VALUES (%s, %s, %s)" 13 | self.cursor.execute(sql, ( 14 | kwargs['username'], kwargs['groupname'], time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))) 15 | self.close() 16 | return self.cursor.lastrowid 17 | except Exception as e: 18 | print(e) 19 | 20 | def add_user_login(self, **kwargs): 21 | # 往login表里添加用户信息 22 | try: 23 | self.connect() 24 | sql = "insert into user_login (username, groupname , c_time) VALUES (%s, %s, %s)" 25 | self.cursor.execute(sql, ( 26 | kwargs['username'], kwargs['groupname'], time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))) 27 | self.close() 28 | return self.cursor.lastrowid 29 | except Exception as e: 30 | print(e) 31 | 32 | def get_id_by_name(self, username): 33 | # group by username 34 | try: 35 | self.connect() 36 | sql = "select count(*) count,username from user_login where username=%s group by username;" 37 | print(sql) 38 | self.cursor.execute(sql, (username, )) 39 | id = self.cursor.fetchone() 40 | print(id) 41 | if id: 42 | return id 43 | except Exception as e: 44 | print(e) -------------------------------------------------------------------------------- /web.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from tornado import web, ioloop, httpserver, locale 4 | from model.models import UserModel 5 | import base64 6 | from faceid import face_register, face_valid 7 | import datetime 8 | 9 | # 10 | class MainPageHandler(web.RequestHandler): 11 | def get(self): 12 | # self.write("Hello, world") 13 | # self.render('week2.html') 14 | self.redirect('/login') 15 | 16 | # 首页 17 | class HomePageHandler(web.RequestHandler): 18 | def get(self): 19 | self.render('week2.html') 20 | 21 | # 照片墙 22 | class PhotoPageHandler(web.RequestHandler): 23 | def get(self): 24 | self.render('week3.html') 25 | 26 | # 注册 27 | class RegisterHandler(web.RequestHandler): 28 | def get(self): 29 | self.render('register.html') 30 | def post(self): 31 | global list 32 | username = self.get_argument('username') 33 | groupname = self.get_argument('groupname') 34 | img = self.get_argument('face') 35 | img = str(img[22:]) 36 | # 人脸注册 37 | res = face_register(img, groupname, username) 38 | if res: 39 | # 添加数据库 40 | mode1 = UserModel() 41 | user_id = mode1.add_user(username=username, groupname=groupname) 42 | if user_id != None: 43 | self.write("注册成功!") 44 | list = list + ',' + groupname 45 | print(list) 46 | # group_name = list(groupname) 47 | # group_id_list.append(group_name) 48 | print(res) 49 | self.render('login.html') 50 | return 51 | print(res) 52 | 53 | self.write("注册失败!") 54 | 55 | # 登入 56 | class LoginPageHandler(web.RequestHandler): 57 | def get(self): 58 | # self.write("Hello, world") 59 | self.render('login.html') 60 | def post(self): 61 | 62 | img = self.get_argument('face') 63 | img = str(img[22:]) 64 | res = face_valid(img, list) 65 | 66 | if res['result']: 67 | # 获取数据 68 | username = [item[key] for item in res['info'] for key in item][1] 69 | groupname = [item[key] for item in res['info'] for key in item][0] 70 | # 添加数据库 71 | mode1 = UserModel() 72 | all_id = mode1.add_user_login(username=username, groupname=groupname) 73 | id = mode1.get_id_by_name(username) 74 | print(id) 75 | all_id = str(all_id) 76 | id_n = str(id['count']) 77 | print(id_n) 78 | times = str(datetime.datetime.now()) 79 | self.write([item[key] for item in res['info'] for key in item][0]+',' 80 | +[item[key] for item in res['info'] for key in item][1]+','+ 81 | times[:19]+"\n 登入成功!") 82 | self.write(" 您一共访问"+id_n+"次! 您是第"+all_id+"位访客!") 83 | self.render('week2.html') 84 | # time.sleep(5) 85 | # self.redirect('/') 86 | print(res) 87 | return 88 | print(res) 89 | self.write("登入失败,请多试一次!") 90 | 91 | settings = { 92 | # 模板的路径 93 | 'template_path' : 'baiduai/templates', 94 | # 静态资源的路径 95 | 'static_path' : 'baiduai/static', 96 | } 97 | application = web.Application([ 98 | (r"/", MainPageHandler), 99 | (r"/register", RegisterHandler), 100 | (r"/login", LoginPageHandler), 101 | (r"/week2", HomePageHandler), 102 | (r"/week3", PhotoPageHandler)], **settings) 103 | 104 | if __name__ == "__main__": 105 | global list 106 | # init 107 | list = 'cs' 108 | http_server = httpserver.HTTPServer(application) 109 | http_server.listen(80) 110 | ioloop.IOLoop.current().start() 111 | --------------------------------------------------------------------------------