Graduate

Modify client GUI

......@@ -5,6 +5,7 @@
import tkinter as tk
import tkinter.font
import tkinter.messagebox
import tkinter.scrolledtext
import threading
import torch
import numpy as np
......@@ -48,9 +49,17 @@ class Client(tk.Frame):
self.cap.set(3, self.cam_width)
self.cap.set(4, self.cam_height)
# Application Function
# cam에서 MTCNN 적용하는 영역
self.detecting_square = (200, 200)
# 영상 위에 사각형 색상 지정
self.rectangle_color = (0, 0, 255)
# tkinter GUI
self.width = 740
self.height = 640
self.height = 700
self.parent = parent
self.parent.title("출석시스템")
self.parent.geometry("%dx%d+100+100" % (self.width, self.height))
......@@ -74,13 +83,19 @@ class Client(tk.Frame):
self.label = tk.Label(self, image=image)
self.label.grid(row=1, column=0, columnspan=20)
self.log = tk.Text(self)
self.log = tk.scrolledtext.ScrolledText(self, wrap = tk.WORD, state=tk.DISABLED, width = 96, height = 10)
self.log.grid(row=2, column=0, columnspan=20)
self.quit = tk.Button(self, text="나가기", fg="red", command=self.stop)
self.quit.grid(row=5, column=10)
self.quit.grid(row=3, column=10)
def logging(self, text):
self.log.config(state=tk.NORMAL)
self.log.insert(tkinter.CURRENT, text)
self.log.insert(tkinter.CURRENT, '\n')
self.log.config(state=tk.DISABLED)
def detect_face(self, frame):
results = self.mtcnn.detect(frame)
......@@ -97,8 +112,6 @@ class Client(tk.Frame):
x1, y1, x2, y2 = box
if (x2-x1) * (y2-y1) < 15000:
# 얼굴 해상도가 너무 낮으면 무시
self.alert.config(text= "카메라에 더 가까이 접근해주세요.", fg="red")
self.alert.update()
continue
image = frame
image_list.append(image)
......@@ -109,24 +122,35 @@ class Client(tk.Frame):
def mainthread(self):
t = threading.currentThread()
asyncio.set_event_loop(self.event_loop)
x1 = int(self.cam_width / 2 - self.detecting_square[0] / 2)
x2 = int(self.cam_width / 2 + self.detecting_square[0] / 2)
y1 = int(self.cam_height / 2 - self.detecting_square[1] / 2)
y2 = int(self.cam_height / 2 + self.detecting_square[1] / 2)
while getattr(t, "do_run", True):
ret, frame = self.cap.read()
# model에 이용하기 위해 convert
converted = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_list, image_list = self.detect_face(converted)
face_list, image_list = self.detect_face(converted[y1:y2, x1:x2])
# 얼굴이 인식되면 출석요청
self.event_loop.run_until_complete(self.send_face(face_list, image_list))
# show image
frame = cv2.rectangle(frame, (x1, y1), (x2, y2), self.rectangle_color, 3)
converted = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 거울상으로 보여준다
converted = cv2.flip(converted,1)
image = Image.fromarray(converted)
image = ImageTk.PhotoImage(image)
self.label.configure(image=image)
self.label.image = image # kind of double buffering
# 얼굴이 인식되면 요청
if face_list:
self.event_loop.run_until_complete(self.send_face(face_list, image_list))
@asyncio.coroutine
def set_rectangle(self):
self.rectangle_color = (255, 0, 0)
yield from asyncio.sleep(1)
self.rectangle_color = (0, 0, 255)
async def wait(self, n):
await asyncio.sleep(n)
......@@ -141,16 +165,16 @@ class Client(tk.Frame):
data = json.loads(recv)
if data['status'] == 'success':
# 성공
self.log.insert(tkinter.CURRENT, data['student_id'] + 'is attend')
self.log.insert(tkinter.CURRENT, '\n')
self.logging('출석확인: ' + data['student_id'])
asyncio.ensure_future(self.set_rectangle())
else:
self.log.insert(tkinter.CURRENT, 'verification failed:' + data['status'])
self.log.insert(tkinter.CURRENT, '\n')
if data['status'] == 'failed':
send = json.dumps({'action': 'save_image', 'image': image.tolist()})
await websocket.send(send)
elif data['status'] == 'already':
asyncio.ensure_future(self.set_rectangle())
except Exception as e:
self.log.insert(tkinter.CURRENT, e)
self.log.insert(tkinter.CURRENT, '\n')
self.logging(e)
def stop(self):
self.thread.do_run = False
......@@ -164,4 +188,4 @@ if __name__ == '__main__':
root = tk.Tk()
Client(root)
root.mainloop()
\ No newline at end of file
......
......@@ -164,6 +164,8 @@ class Register(tk.Frame):
# show image
converted = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# 유저에게 보여줄 땐 거울상으로 보여준다
converted = cv2.flip(converted,1)
image = Image.fromarray(converted)
image = ImageTk.PhotoImage(image)
self.label.configure(image=image)
......
......@@ -51,18 +51,18 @@ async def register(websocket):
global clients
async with lock:
clients.add(websocket)
remote_ip = websocket.remote_address[0]
msg='[{ip}] connected'.format(ip=remote_ip)
print(msg)
#remote_ip = websocket.remote_address[0]
#msg='[{ip}] connected'.format(ip=remote_ip)
#print(msg)
async def unregister(websocket):
global lock
global clients
async with lock:
clients.remove(websocket)
remote_ip = websocket.remote_address[0]
msg='[{ip}] disconnected'.format(ip=remote_ip)
print(msg)
#remote_ip = websocket.remote_address[0]
#msg='[{ip}] disconnected'.format(ip=remote_ip)
#print(msg)
async def thread(websocket, path):
await register(websocket)
......