Spaces:
Building
Building
Update src/main.py
Browse files- src/main.py +83 -72
src/main.py
CHANGED
@@ -6,7 +6,7 @@ import io
|
|
6 |
import cv2
|
7 |
import numpy as np
|
8 |
import os
|
9 |
-
import requests
|
10 |
|
11 |
app = Flask(__name__, static_folder='static')
|
12 |
app.config['TITLE'] = 'Sign Language Translate'
|
@@ -15,90 +15,101 @@ nlp, dict_docs_spacy = sp.load_spacy_values()
|
|
15 |
dataset, list_2000_tokens = dg.load_data()
|
16 |
|
17 |
def translate_korean_to_english(text):
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
|
56 |
@app.route('/')
|
57 |
def index():
|
58 |
-
|
59 |
|
60 |
@app.route('/translate/', methods=['POST'])
|
61 |
def result():
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
@app.route('/video_feed')
|
86 |
def video_feed():
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
|
92 |
@app.route('/download_video/<gloss_sentence>')
|
93 |
def download_video(gloss_sentence):
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
|
103 |
if __name__ == "__main__":
|
104 |
-
|
|
|
6 |
import cv2
|
7 |
import numpy as np
|
8 |
import os
|
9 |
+
import requests
|
10 |
|
11 |
app = Flask(__name__, static_folder='static')
|
12 |
app.config['TITLE'] = 'Sign Language Translate'
|
|
|
15 |
dataset, list_2000_tokens = dg.load_data()
|
16 |
|
17 |
def translate_korean_to_english(text):
|
18 |
+
try:
|
19 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
20 |
+
params = {
|
21 |
+
"client": "gtx",
|
22 |
+
"sl": "ko",
|
23 |
+
"tl": "en",
|
24 |
+
"dt": "t",
|
25 |
+
"q": text.strip() # 입력 텍스트의 앞뒤 공백 제거
|
26 |
+
}
|
27 |
+
response = requests.get(url, params=params)
|
28 |
+
if response.status_code == 200:
|
29 |
+
# 전체 번역 결과를 하나의 문자열로 결합
|
30 |
+
translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
|
31 |
+
return translated_text
|
32 |
+
else:
|
33 |
+
raise Exception(f"Translation API returned status code: {response.status_code}")
|
34 |
+
except Exception as e:
|
35 |
+
print(f"Translation error: {e}")
|
36 |
+
return text
|
37 |
|
38 |
def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
39 |
+
frames = []
|
40 |
+
for frame in dg.generate_video(gloss_list, dataset, list_2000_tokens):
|
41 |
+
frame_data = frame.split(b'\r\n\r\n')[1]
|
42 |
+
nparr = np.frombuffer(frame_data, np.uint8)
|
43 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
44 |
+
frames.append(img)
|
45 |
+
|
46 |
+
height, width = frames[0].shape[:2]
|
47 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
48 |
+
temp_path = os.path.join('/tmp', 'temp.mp4')
|
49 |
+
out = cv2.VideoWriter(temp_path, fourcc, 25, (width, height))
|
50 |
+
|
51 |
+
for frame in frames:
|
52 |
+
out.write(frame)
|
53 |
+
out.release()
|
54 |
+
|
55 |
+
with open(temp_path, 'rb') as f:
|
56 |
+
video_bytes = f.read()
|
57 |
+
|
58 |
+
os.remove(temp_path)
|
59 |
+
return video_bytes
|
60 |
|
61 |
@app.route('/')
|
62 |
def index():
|
63 |
+
return render_template('index.html', title=app.config['TITLE'])
|
64 |
|
65 |
@app.route('/translate/', methods=['POST'])
|
66 |
def result():
|
67 |
+
if request.method == 'POST':
|
68 |
+
input_text = request.form['inputSentence'].strip()
|
69 |
+
if not input_text:
|
70 |
+
return render_template('error.html', error="Please enter text to translate")
|
71 |
+
|
72 |
+
try:
|
73 |
+
english_text = translate_korean_to_english(input_text)
|
74 |
+
if not english_text:
|
75 |
+
raise Exception("Translation failed")
|
76 |
+
|
77 |
+
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text)
|
78 |
+
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
79 |
+
|
80 |
+
gloss_list_lower = [gloss.lower() for gloss in generated_gloss.split() if gloss.isalnum()]
|
81 |
+
gloss_sentence_before_synonym = " ".join(gloss_list_lower)
|
82 |
+
|
83 |
+
gloss_list = [sp.find_synonyms(gloss, nlp, dict_docs_spacy, list_2000_tokens)
|
84 |
+
for gloss in gloss_list_lower]
|
85 |
+
gloss_sentence_after_synonym = " ".join(gloss_list)
|
86 |
+
|
87 |
+
return render_template('result.html',
|
88 |
+
title=app.config['TITLE'],
|
89 |
+
original_sentence=input_text,
|
90 |
+
english_translation=english_text,
|
91 |
+
gloss_sentence_before_synonym=gloss_sentence_before_synonym,
|
92 |
+
gloss_sentence_after_synonym=gloss_sentence_after_synonym)
|
93 |
+
except Exception as e:
|
94 |
+
return render_template('error.html', error=f"Translation error: {str(e)}")
|
95 |
|
96 |
@app.route('/video_feed')
|
97 |
def video_feed():
|
98 |
+
sentence = request.args.get('gloss_sentence_to_display', '')
|
99 |
+
gloss_list = sentence.split()
|
100 |
+
return Response(dg.generate_video(gloss_list, dataset, list_2000_tokens),
|
101 |
+
mimetype='multipart/x-mixed-replace; boundary=frame')
|
102 |
|
103 |
@app.route('/download_video/<gloss_sentence>')
|
104 |
def download_video(gloss_sentence):
|
105 |
+
gloss_list = gloss_sentence.split()
|
106 |
+
video_bytes = generate_complete_video(gloss_list, dataset, list_2000_tokens)
|
107 |
+
return send_file(
|
108 |
+
io.BytesIO(video_bytes),
|
109 |
+
mimetype='video/mp4',
|
110 |
+
as_attachment=True,
|
111 |
+
download_name='sign_language.mp4'
|
112 |
+
)
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|