Graduate

flask

1 from flask import Flask, render_template, Response, request, jsonify 1 from flask import Flask, render_template, Response, request, jsonify
2 import os 2 import os
3 +import io
3 import json 4 import json
4 import cv2 5 import cv2
5 import numpy as np 6 import numpy as np
6 import time 7 import time
7 -import datetime 8 +from datetime import datetime
8 import sys 9 import sys
9 import tensorflow as tf 10 import tensorflow as tf
10 import base64 11 import base64
11 import pymysql 12 import pymysql
12 import configparser 13 import configparser
14 +from PIL import Image
13 15
14 config = configparser.ConfigParser() 16 config = configparser.ConfigParser()
15 config.read('./config.cnf') 17 config.read('./config.cnf')
...@@ -46,8 +48,11 @@ def index(): ...@@ -46,8 +48,11 @@ def index():
46 """Video streaming page""" 48 """Video streaming page"""
47 return render_template('index.html') 49 return render_template('index.html')
48 50
49 -@app.route('/register', methods=['POST']) 51 +@app.route('/register', methods=['GET', 'POST'])
50 def register(): 52 def register():
53 + if request.method == 'GET':
54 + return render_template('register.html')
55 +
51 attendance_db = pymysql.connect(read_default_file="./DB.cnf") 56 attendance_db = pymysql.connect(read_default_file="./DB.cnf")
52 cursor = attendance_db.cursor(pymysql.cursors.DictCursor) 57 cursor = attendance_db.cursor(pymysql.cursors.DictCursor)
53 send = {'form':'json'} 58 send = {'form':'json'}
...@@ -66,11 +71,14 @@ def register(): ...@@ -66,11 +71,14 @@ def register():
66 msg='[{id}] is registered'.format(id=student_id) 71 msg='[{id}] is registered'.format(id=student_id)
67 print(msg) 72 print(msg)
68 # image to input tensor 73 # image to input tensor
69 - image = base64.b64decode(request.form['image']) 74 + file = request.files['file']
70 - image_np = np.frombuffer(image, dtype=np.uint8) 75 + image_bytes = file.read()
71 - image_np = cv2.imdecode(image_np, flags=1) 76 + image_np = np.fromstring(image_bytes, dtype=np.uint8)
77 + image_np = cv2.imdecode(image_np, cv2.IMREAD_UNCHANGED)
78 + cv2.imwrite('./test.jpg', image_np)
72 image_np = resize(image_np) 79 image_np = resize(image_np)
73 image_np = prewhiten(image_np) 80 image_np = prewhiten(image_np)
81 + cv2.imwrite('./test2.jpg', image_np)
74 image_np = image_np.reshape(-1, image_size, image_size, 3) 82 image_np = image_np.reshape(-1, image_size, image_size, 3)
75 # get embedding 83 # get embedding
76 feed_dict = {input_placeholder:image_np, phase_train_placeholder:False } 84 feed_dict = {input_placeholder:image_np, phase_train_placeholder:False }
...@@ -94,7 +102,7 @@ def verify(): ...@@ -94,7 +102,7 @@ def verify():
94 send = {'form':'json'} 102 send = {'form':'json'}
95 image = base64.b64decode(request.form['image']) 103 image = base64.b64decode(request.form['image'])
96 image_np = np.frombuffer(image, dtype=np.uint8) 104 image_np = np.frombuffer(image, dtype=np.uint8)
97 - image_np = cv2.imdecode(image_np, flags=1) 105 + image_np = cv2.imdecode(image_np, cv2.IMREAD_UNCHANGED)
98 image_np = resize(image_np) 106 image_np = resize(image_np)
99 image_np = prewhiten(image_np) 107 image_np = prewhiten(image_np)
100 image_np = image_np.reshape(-1, image_size, image_size, 3) 108 image_np = image_np.reshape(-1, image_size, image_size, 3)
...@@ -110,6 +118,7 @@ def verify(): ...@@ -110,6 +118,7 @@ def verify():
110 db_embedding = np.frombuffer(row_data['embedding'], dtype=np.float32) 118 db_embedding = np.frombuffer(row_data['embedding'], dtype=np.float32)
111 db_embedding = db_embedding.reshape((1,512)) 119 db_embedding = db_embedding.reshape((1,512))
112 distance = get_distance(embedding, db_embedding) 120 distance = get_distance(embedding, db_embedding)
121 + print(distance)
113 if (distance < threshold): 122 if (distance < threshold):
114 verified_id = row_data['student_id'] 123 verified_id = row_data['student_id']
115 new_embedding = db_embedding * 0.8 + embedding * 0.2 124 new_embedding = db_embedding * 0.8 + embedding * 0.2
......
1 [verification_server] 1 [verification_server]
2 model=models/20200816-080621 2 model=models/20200816-080621
3 -threshold=0.75 3 +threshold=0.8
4 image_size=160 4 image_size=160
......
1 +<!doctype html>
2 +<title>Web Attendance System Register</title>
3 +<meta charset="UTF-8">
4 +<meta name="viewport" content="width=device-width, initial-scale=1">
5 +<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
6 +<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nanum+Gothic:400,700,800&amp;subset=korean">
7 +<script type='text/javascript' src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
8 +<style>
9 +body,h1,h2,h3,h4,h5 {font-family: "Nanum+Gothic", sans-serif}
10 +</style>
11 +<body class="w3-light-grey">
12 +
13 +<!-- w3-content defines a container for fixed size centered content,
14 +and is wrapped around the whole page content, except for the footer in this example -->
15 +<div class="w3-content" style="max-width:1400px">
16 +
17 +<!-- Header -->
18 +<header class="w3-container w3-center w3-padding-32">
19 + <h1><b>얼굴 등록</b></h1>
20 + <p>Made by <span class="w3-tag">정해갑</span></p>
21 +</header>
22 +
23 +<div class="w3-row", style='text-align:center'>
24 + <h2><b>얼굴 파일을 등록해주세요 (jpeg only)</b></h2>
25 +<form method="post" action="/register" enctype="multipart/form-data">
26 +학번: <input type="text" name="student_id"><br>
27 +이름: <input type="text" name="student_name"><br><br>
28 +<input type="file" name="file" onchange="loadFile(event)" autocomplete="off" accept="image/jpeg" required>
29 +<div>
30 +<img id="preview">
31 +</div>
32 +<input type="submit" value="등록">
33 +</form>
34 +<script>
35 + var loadFile = function(event) {
36 + var output = document.getElementById('preview');
37 + var reader = new FileReader();
38 + reader.readAsDataURL(event.target.files[0]);
39 + reader.result;
40 +
41 + output.src = URL.createObjectURL(event.target.files[0]);
42 + output.onload = function() {
43 + URL.revokeObjectURL(output.src) // free memory
44 + }
45 + };
46 +</script>
47 +</div>
48 +