Graduate

flask

from flask import Flask, render_template, Response, request, jsonify
import os
import io
import json
import cv2
import numpy as np
import time
import datetime
from datetime import datetime
import sys
import tensorflow as tf
import base64
import pymysql
import configparser
from PIL import Image
config = configparser.ConfigParser()
config.read('./config.cnf')
......@@ -46,8 +48,11 @@ def index():
"""Video streaming page"""
return render_template('index.html')
@app.route('/register', methods=['POST'])
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'GET':
return render_template('register.html')
attendance_db = pymysql.connect(read_default_file="./DB.cnf")
cursor = attendance_db.cursor(pymysql.cursors.DictCursor)
send = {'form':'json'}
......@@ -66,11 +71,14 @@ def register():
msg='[{id}] is registered'.format(id=student_id)
print(msg)
# image to input tensor
image = base64.b64decode(request.form['image'])
image_np = np.frombuffer(image, dtype=np.uint8)
image_np = cv2.imdecode(image_np, flags=1)
file = request.files['file']
image_bytes = file.read()
image_np = np.fromstring(image_bytes, dtype=np.uint8)
image_np = cv2.imdecode(image_np, cv2.IMREAD_UNCHANGED)
cv2.imwrite('./test.jpg', image_np)
image_np = resize(image_np)
image_np = prewhiten(image_np)
cv2.imwrite('./test2.jpg', image_np)
image_np = image_np.reshape(-1, image_size, image_size, 3)
# get embedding
feed_dict = {input_placeholder:image_np, phase_train_placeholder:False }
......@@ -94,7 +102,7 @@ def verify():
send = {'form':'json'}
image = base64.b64decode(request.form['image'])
image_np = np.frombuffer(image, dtype=np.uint8)
image_np = cv2.imdecode(image_np, flags=1)
image_np = cv2.imdecode(image_np, cv2.IMREAD_UNCHANGED)
image_np = resize(image_np)
image_np = prewhiten(image_np)
image_np = image_np.reshape(-1, image_size, image_size, 3)
......@@ -110,6 +118,7 @@ def verify():
db_embedding = np.frombuffer(row_data['embedding'], dtype=np.float32)
db_embedding = db_embedding.reshape((1,512))
distance = get_distance(embedding, db_embedding)
print(distance)
if (distance < threshold):
verified_id = row_data['student_id']
new_embedding = db_embedding * 0.8 + embedding * 0.2
......
[verification_server]
model=models/20200816-080621
threshold=0.75
threshold=0.8
image_size=160
......
<!doctype html>
<title>Web Attendance System Register</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Nanum+Gothic:400,700,800&amp;subset=korean">
<script type='text/javascript' src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<style>
body,h1,h2,h3,h4,h5 {font-family: "Nanum+Gothic", sans-serif}
</style>
<body class="w3-light-grey">
<!-- w3-content defines a container for fixed size centered content,
and is wrapped around the whole page content, except for the footer in this example -->
<div class="w3-content" style="max-width:1400px">
<!-- Header -->
<header class="w3-container w3-center w3-padding-32">
<h1><b>얼굴 등록</b></h1>
<p>Made by <span class="w3-tag">정해갑</span></p>
</header>
<div class="w3-row", style='text-align:center'>
<h2><b>얼굴 파일을 등록해주세요 (jpeg only)</b></h2>
<form method="post" action="/register" enctype="multipart/form-data">
학번: <input type="text" name="student_id"><br>
이름: <input type="text" name="student_name"><br><br>
<input type="file" name="file" onchange="loadFile(event)" autocomplete="off" accept="image/jpeg" required>
<div>
<img id="preview">
</div>
<input type="submit" value="등록">
</form>
<script>
var loadFile = function(event) {
var output = document.getElementById('preview');
var reader = new FileReader();
reader.readAsDataURL(event.target.files[0]);
reader.result;
output.src = URL.createObjectURL(event.target.files[0]);
output.onload = function() {
URL.revokeObjectURL(output.src) // free memory
}
};
</script>
</div>