-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
164 lines (132 loc) · 4.97 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from fastapi import BackgroundTasks, FastAPI, Request, WebSocket, WebSocketDisconnect
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import cv2
from websockets.exceptions import ConnectionClosedError
from gaze_tracking.gaze_tracker import GazeTracker
from emotion_detector import EmotionDetector
import asyncio
from datetime import datetime
from io import BytesIO
from fastapi import Response, HTTPException, status
from pydantic import BaseModel
from fastapi.responses import StreamingResponse
import os
import matplotlib.pyplot as plt
os.environ['OPENCV_VIDEOIO_PRIORITY_MSMF'] = '0'
app = FastAPI()
origins = [
"http://localhost",
]
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# 크기 변환
# camera.set(3, 1640)
# camera.set(4, 1480)
templates = Jinja2Templates(directory="templates")
showing_emotion = 'false'
emotion_detector = EmotionDetector()
gaze_tracker = GazeTracker()
EMOTIONS = ["angry", "disgust", "scared", "happy", "sad", "surprised", "neutral"]
# async def receive_message(websocket: WebSocket):
# global show
# show = await websocket.receive_text()
@app.get('/')
def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
# 감정 분석 결과 화면에 보여준다
@app.get('/emotion')
async def show_emotion(show: str):
global showing_emotion
showing_emotion = show
# 면접 시작한다 (감정 초기화)
@app.get('/start-interview')
async def start_analysis():
emotion_detector.start_emotion_analysis()
class EmotionList(BaseModel):
emotions: list[str] = []
values: list[float] = []
x_data: list[float] = []
y_data: list[float] = []
@app.get('/stop-interview', response_model=EmotionList)
async def show_result():
emotion_detector.end_emotion_analysis()
# try:
# plt, happy_per = emotion_detector.get_happy_result()
# except ZeroDivisionError:
# raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
# return str(happy_per)
print(gaze_tracker.x_data, gaze_tracker.y_data)
plt.scatter(gaze_tracker.x_data, gaze_tracker.y_data, c='red', alpha=0.5, s=100)
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.savefig('save.png')
# x_data: 오른쪽 -> 왼쪽
# y_data: 위 -> 아래
print({'emotions': EMOTIONS, 'values': emotion_detector.emotion_cal, 'x_data': gaze_tracker.x_data, 'y_data': gaze_tracker.y_data})
return {'emotions': EMOTIONS, 'values': emotion_detector.emotion_cal, 'x_data': gaze_tracker.x_data, 'y_data': gaze_tracker.y_data}
@app.websocket("/emotion-cam")
async def get_stream_with_emotion(websocket: WebSocket, background_tasks: BackgroundTasks):
# 소켓 연결 -> 카메라 읽기 시작한 후에 소켓으로 이미지를 보내준다
await websocket.accept()
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
print('emotion cam started...')
# emotion_detector.start_emotion_analysis()
global showing_emotion
showing_emotion = 'false'
try:
while True:
# data = await websocket.receive_text()
success, frame = camera.read()
# 반전
frame = cv2.flip(frame, 2)
if not success:
print('fail...')
break
else:
# frame = emotion_detector.add_frame(frame)
# 소켓에 이미지 보낸다
# if emotion_detector.detecting:
# frame = await emotion_detector.add_frame(frame, 'true')
if showing_emotion == 'true':
frame = await emotion_detector.add_frame(frame, 'true')
else:
# emotion_detector.add_frame(frame)
asyncio.create_task(emotion_detector.add_frame(frame))
asyncio.create_task(gaze_tracker.add_frame(frame))
ret, buffer = cv2.imencode('.jpg', frame)
await websocket.send_bytes(buffer.tobytes())
except ConnectionClosedError:
print("Client disconnected")
camera.release()
@app.websocket("/test-cam")
async def get_stream(websocket: WebSocket):
# 소켓 연결 -> 카메라 읽기 시작한 후에 소켓으로 이미지를 보내준다
await websocket.accept()
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
print('test cam started...')
try:
while True:
success, frame = camera.read()
# 반전
frame = cv2.flip(frame, 2)
if not success:
break
else:
try:
ret, buffer = cv2.imencode('.jpg', frame)
except:
print(buffer)
break
await websocket.send_bytes(buffer.tobytes())
except ConnectionClosedError:
print("Client disconnected")
camera.release()
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000)