Graduate

Modify server.py

......@@ -8,7 +8,7 @@ import websockets
from io import BytesIO
import pymysql
import datetime
from datetime import datetime
from PIL import Image, ImageDraw
from IPython import display
......@@ -23,7 +23,7 @@ model = InceptionResnetV1().eval().to(device)
attendance_db = pymysql.connect(
user='root',
passwd='5978',
host='localhost',
host='127.0.0.1',
db='attendance',
charset='utf8'
)
......@@ -39,7 +39,7 @@ async def get_embeddings(face_list):
return yhat
async def get_distance(arr1, arr2):
distance = (arr1 - arr2).norm().item()
distance = np.linalg.norm(arr1 - arr2)
return distance
def get_argmin(someone, database):
......@@ -67,10 +67,8 @@ async def unregister(websocket):
print(msg)
async def thread(websocket, path):
# register(websocket) sends user_event() to websocket
await register(websocket)
try:
# await websocket.send(state_event())
async for message in websocket:
data = json.loads(message)
remote_ip = websocket.remote_address[0]
......@@ -89,79 +87,92 @@ async def thread(websocket, path):
cursor = attendance_db.cursor(pymysql.cursors.DictCursor)
# 학생을 찾음
sql = "SELECT student_id FROM student WHERE student_id = %s;"
sql = "SELECT student_id FROM student WHERE student_id = %s;"
cursor.execute(sql, (student_id))
# DB에 학생이 없으면 등록
if not cursor.fetchone():
sql = "insert into student(student_id, student_name) values (%s, %s)"
sql = "INSERT INTO student(student_id, student_name) VALUES (%s, %s)"
cursor.execute(sql, (student_id, student_name))
attendance_db.commit()
msg='[{ip}] {id} is registered'.format(ip=remote_ip, id=student_id)
print(msg)
# student_embedding Table에 등록
embedding = await get_embeddings(face)
embedding = embedding.detach().numpy().tobytes()
embedding_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
sql = "insert into student_embedding(student_id, embedding_date, embedding) values (%s, %s, %s)"
embedding_date = datetime.now().strftime('%Y-%m-%d')
sql = "insert into student_embedding(student_id, embedding_date, embedding) values (%s, %s, _binary %s)"
cursor.execute(sql, (student_id, embedding_date, embedding))
attendance_db.commit()
await websocket.send('{id} registered'.format(id=student_id))
elif data['action'] == "verify":
send = json.dumps({'status': 'success', 'student_id': student_id})
await websocket.send(send)
elif data['action'] == 'verify':
# log
msg='[{ip}] verify face'.format(ip=remote_ip)
print(msg)
###############
# load json
face = np.asarray(data['MTCNN'], dtype = np.float32)
face = face.reshape((1,3,160,160))
# embedding 구하기
embedding = await get_embeddings(face)
embedding = embedding.detach().numpy()
# embedding.numpy()
# [1, 512] numpy()임
# np.frombuffer()로 불러오는 것이 좋을 듯.
# DB에 연결
cursor = attendance_db.cursor(pymysql.cursors.DictCursor)
# 학생을 찾음
# 가장 비슷한 Embedding을 찾는 SQL
cursor = attendance_db.cursor(pymysql.cursors.DictCursor)
sql = "SELECT student_id, embedding FROM student_embedding;"
cursor.execute(sql)
result = cursor.fetchall()
verified_id = '0000000000'
distance_min = 1
verified_id = '0'
distance_min = 1.0
for row_data in result:
db_embedding = np.frombuffer(row_data['embedding'], dtype=np.float32)
db_embedding = db_embedding.reshape((1,512))
distance = get_distance(embedding, db_embedding)
distance = await get_distance(embedding, db_embedding)
if (distance < distance_min):
verified_id = row_data['student_id']
distance_min = distance
# 출석 데이터 전송
data = ''
send = ''
print('[debug] distance:', distance_min)
if distance_min >= 0.6:
# 해당하는 사람 DB에 없음
print('verification failed: not in DB')
data = json.dumps({'state': 'fail'})
msg='[{ip}] verification failed'.format(ip=remote_ip)
print(msg)
send = json.dumps({'status': 'fail'})
else:
# 해당하는 사람 DB에 있음
print('verification success:', verified_id)
data = json.dumps({'state': 'success', 'id': verified_id})
await websocket.send(data)
# logging
msg='[{ip}] verification success {id}'.format(ip=remote_ip, id=verified_id)
print(msg)
# TODO: lecture DB에 tuple 삽입해야 아래 코드가 돌아감
# 해당하는 사람이 DB에 있으면 출석 처리
#date = datetime.now().strftime('%Y-%m-%d')
#sql = "INSERT INTO student_attendance(lecture_id, student_id, date) VALUES (%s, %s, %s)"
#cursor.execute(sql, ('0', verified_id, date))
# client에 전달
send = json.dumps({'status': 'success', 'student_id': verified_id})
await websocket.send(send)
elif data['action'] == "save_image":
# 출석이 제대로 이뤄지지 않으면 이미지를 저장하여
# 나중에 교강사가 출석을 확인할 수 있도록 한다
arr = np.asarray(data['image'], dtype = np.uint8)
# 이 데이터는 데이터베이스(과목명/일자/undefined)에 저장하는 것이 좋을듯
# image = Image.fromarray(arr)
# image.save('face.jpg')# storage에 데이터 저장
remote_ip = websocket.remote_address[0]
msg='[{ip}] save image'.format(ip=remote_ip)
print(msg)
###
await websocket.send('정해갑')
arr = np.asarray(data['image'], dtype = np.uint8)
blob = arr.tobytes()
date = datetime.now().strftime('%Y-%m-%d')
# TODO: lecture DB에 tuple 삽입해야 아래 코드가 돌아감
#cursor = attendance_db.cursor(pymysql.cursors.DictCursor)
#sql = "INSERT INTO undefined_image(lecture_id, date, image, width, height) VALUES (%s, %s, _binary %s, %s, %s)"
#cursor.execute(sql, ('0', date, blob, arr.shape[0], arr.shape[1]))
#attendance_db.commit()
else:
print("unsupported event: {}", data)
finally:
......