Graduate

Change server IP

...@@ -24,7 +24,7 @@ print('Running on device: {}'.format(device)) ...@@ -24,7 +24,7 @@ print('Running on device: {}'.format(device))
24 24
25 mtcnn = MTCNN(keep_all=True, post_process=True, device=device) 25 mtcnn = MTCNN(keep_all=True, post_process=True, device=device)
26 26
27 -uri = 'ws://localhost:8765' 27 +uri = 'ws://169.56.95.131:8765'
28 28
29 async def send_face(face_list, image_list): 29 async def send_face(face_list, image_list):
30 async with websockets.connect(uri) as websocket: 30 async with websockets.connect(uri) as websocket:
......
1 -##################################################
2 -#1. webcam에서 얼굴을 인식합니다.
3 -#2. 얼굴일 확률이 97% 이상이고 영역이 15000 이상인 이미지를 서버에 전송
4 -##################################################
5 -import torch
6 -import numpy as np
7 -import cv2
8 -import asyncio
9 -import websockets
10 -import json
11 -import os
12 -import timeit
13 -import base64
14 -
15 -from PIL import Image
16 -from io import BytesIO
17 -import requests
18 -
19 -from models.mtcnn import MTCNN
20 -
21 -device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
22 -print('Running on device: {}'.format(device))
23 -
24 -mtcnn = MTCNN(keep_all=True, device=device)
25 -
26 -uri = 'ws://localhost:8765'
27 -
28 -async def send_face(face_list, image_list):
29 - async with websockets.connect(uri) as websocket:
30 - for face, image in zip(face_list, image_list):
31 - #type: np.float32
32 - send = json.dumps({'action': 'verify', 'MTCNN': face.tolist()})
33 - await websocket.send(send)
34 - recv = await websocket.recv()
35 - data = json.loads(recv)
36 - if data['status'] == 'success':
37 - # 성공
38 - print(data['student_id'], 'is attend')
39 - else:
40 - print('verification failed:', data['status'])
41 - if data['status'] == 'failed':
42 - send = json.dumps({'action': 'save_image', 'image': image.tolist()})
43 -
44 -def detect_face(frame):
45 - results = mtcnn.detect(frame)
46 - faces = mtcnn(frame, return_prob = False)
47 - image_list = []
48 - face_list = []
49 - if results[1][0] == None:
50 - return [], []
51 - for box, face, prob in zip(results[0], faces, results[1]):
52 - if prob < 0.97:
53 - continue
54 - print('face detected. prob:', prob)
55 - x1, y1, x2, y2 = box
56 - if (x2-x1) * (y2-y1) < 15000:
57 - # 얼굴 해상도가 너무 낮으면 무시
58 - continue
59 - # 얼굴 주변 ±3 영역 저장
60 - image = frame[int(y1-3):int(y2+3), int(x1-3):int(x2+3)]
61 - image_list.append(image)
62 - # MTCNN 데이터 저장
63 - face_list.append(face.numpy())
64 - return image_list, face_list
65 -
66 -def make_face_list(frame):
67 - results, prob = mtcnn(frame, return_prob = True)
68 - face_list = []
69 - if prob[0] == None:
70 - return []
71 - for result, prob in zip(results, prob):
72 - if prob < 0.97:
73 - continue
74 - #np.float32
75 - face_list.append(result.numpy())
76 - return face_list
77 -
78 -if __name__ == '__main__':
79 - cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
80 - cap.set(3, 720)
81 - cap.set(4, 480)
82 - cv2.namedWindow("img", cv2.WINDOW_NORMAL)
83 - while True:
84 - try:
85 - ret, frame = cap.read()
86 - cv2.imshow('img', frame)
87 - cv2.waitKey(10)
88 - frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
89 - image_list, face_list = detect_face(frame)
90 - if not face_list:
91 - continue;
92 - asyncio.get_event_loop().run_until_complete(send_face(face_list, image_list))
93 - except Exception as ex:
94 - print(ex)