-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
181 lines (160 loc) · 4.81 KB
/
app.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from flask import Flask, render_template, Response,request,redirect, url_for
import cv2
import os
import time
from camera import Camera
import shutil
from Detect import seesunObjectDetector,seesunTextDetector
from Speech import seesunSpeech
app = Flask(__name__)
camera = None
ssod = None
ssot = None
speech = None
guide=False
# camera.py
def get_camera():
global camera
if not camera:
camera = Camera()
return camera
def get_yolo():
global ssod
if not ssod:
ssod = seesunObjectDetector()
return ssod
def get_text():
global ssot
if not ssot:
ssot = seesunTextDetector()
return ssot
def get_speech():
global speech
if not speech:
speech = seesunSpeech()
return speech
# 되돌아갈경우
@app.route('/')
def root():
speech = get_speech()
path = speech.MP3_DIR
for del_file_name in os.listdir(path):
file_path = path + del_file_name
temp = os.path.splitext(file_path)[-1]
if temp == ".mp3":
os.remove(file_path)
return redirect(url_for('index'))
# mainpage
@app.route('/index/')
def index():
global guide
if not guide:
guide=True
speech = get_speech()
time.sleep(2.5)
speech.play_audio('guide_voice',guide=True,wait=False)
return render_template('index.html')
# 이미지 캡처 camera.py에서 frame
def gen(camera):
while True:
frame = camera.get_feed()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
# 이미지 캡쳐 된 것을 보여줌..?
@app.route('/video_feed/')
def video_feed():
camera = get_camera()
return Response(gen(camera),
mimetype='multipart/x-mixed-replace; boundary=frame')
# 캡처된 이미지 저장
@app.route('/img/')
def img_capture():
camera = get_camera()
stamp = camera.capture()
return redirect(url_for('show_capture', timestamp=stamp))
def stamp_file(timestamp):
return 'temp/' + timestamp +".jpg"
# 이미지
@app.route('/img/image/<timestamp>', methods=['POST', 'GET'])
def show_capture(timestamp):
path = stamp_file(timestamp)
file_name = path.split('/')[1]
file_path = camera.CAPTURES_DIR + file_name
detector = get_yolo()
image = cv2.imread(file_path)
speech = get_speech()
speech.play_audio('wait_for_it',guide=True,wait=True)
flag,new_image,label=detector.detect(image)
if flag:
cv2.imwrite(file_path, new_image)
speech.tts(label)
# 저장된이미지를 모델에서 보여줌
return render_template('image.html', path=path,label=label)
# 캡처된 텍스트 이미지 저장
@app.route('/text/')
def text_capture():
camera = get_camera()
stamp = camera.capture()
return redirect(url_for('show_text', name=stamp))
# 텍스트
@app.route('/text/image/<name>', methods=['POST', 'GET'])
def show_text(name):
path = stamp_file(name)
file_name = path.split('/')[1]
file_path = camera.CAPTURES_DIR + file_name
detector = get_text()
image = cv2.imread(file_path)
speech = get_speech()
speech.play_audio('wait_for_it',guide=True,wait=True)
flag,new_image,label=detector.recognize(image)
if flag:
cv2.imwrite(file_path, new_image)
speech.tts(label)
# 저장된이미지를 모델에서 보여줌
return render_template('text.html', path=path,label=label)
# speak
@app.route('/speak/')
def start_speak():
speech = get_speech()
speech.play_audio('voice_on',guide=True,wait=False)
return_text = speech.stt()
if '보여' in return_text:
return redirect(url_for('img_capture'))
elif '읽어' in return_text:
return redirect(url_for('text_capture'))
elif '아빠' in return_text:
return redirect(url_for('dad'))
elif '엄마' in return_text:
return redirect(url_for('mom'))
else:
return redirect(url_for('error'))
# 예외처리 페이지
@app.route('/error')
def error():
speech = get_speech()
speech.play_audio('unknown_command',guide=True,wait=False)
return render_template('return.html')
# no 캐싱
@app.after_request
def set_response_headers(r):
r.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
r.headers['Pragma'] = 'no-cache'
r.headers['Expires'] = '0'
return r
#팀원들,,,
@app.route('/teams')
def teams():
return render_template('user.html')
@app.route('/dad')
def dad():
speech = get_speech()
speech.tts('절 만드신 아빠는 김진원, 이동재 두분이에요! 하.하.하.')
return redirect(url_for('index'))
@app.route('/mom')
def mom():
speech = get_speech()
speech.tts('절 만드신 엄마는 민채정, 박희원, 이찬주 세분이에요! 호.호.호.')
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True)
# app.run(host='192.168.0.57',port=5001,debug=True)