Spaces:
Running
Running
Update src/main.py
Browse files- src/main.py +59 -11
src/main.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import display_gloss as dg
|
| 3 |
import synonyms_preprocess as sp
|
| 4 |
from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
|
@@ -17,6 +16,14 @@ app.config['TITLE'] = 'Sign Language Translate'
|
|
| 17 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
| 18 |
dataset, list_2000_tokens = dg.load_data()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def translate_korean_to_english(text):
|
| 21 |
try:
|
| 22 |
url = "https://translate.googleapis.com/translate_a/single"
|
|
@@ -40,11 +47,21 @@ def translate_korean_to_english(text):
|
|
| 40 |
def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
| 41 |
try:
|
| 42 |
frames = []
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if not frames:
|
| 50 |
raise Exception("No frames generated")
|
|
@@ -89,12 +106,43 @@ def result():
|
|
| 89 |
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text)
|
| 90 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
for gloss in gloss_list_lower]
|
| 97 |
-
gloss_sentence_after_synonym = " ".join(gloss_list)
|
| 98 |
|
| 99 |
return render_template('result.html',
|
| 100 |
title=app.config['TITLE'],
|
|
|
|
|
|
|
| 1 |
import display_gloss as dg
|
| 2 |
import synonyms_preprocess as sp
|
| 3 |
from NLP_Spacy_base_translator import NlpSpacyBaseTranslator
|
|
|
|
| 16 |
nlp, dict_docs_spacy = sp.load_spacy_values()
|
| 17 |
dataset, list_2000_tokens = dg.load_data()
|
| 18 |
|
| 19 |
+
def is_proper_noun(word):
|
| 20 |
+
"""고유명사 여부를 확인하는 함수"""
|
| 21 |
+
return word[0].isupper() if word else False
|
| 22 |
+
|
| 23 |
+
def spell_out_word(word):
|
| 24 |
+
"""단어를 개별 알파벳으로 분리하는 함수"""
|
| 25 |
+
return ' '.join(list(word.lower()))
|
| 26 |
+
|
| 27 |
def translate_korean_to_english(text):
|
| 28 |
try:
|
| 29 |
url = "https://translate.googleapis.com/translate_a/single"
|
|
|
|
| 47 |
def generate_complete_video(gloss_list, dataset, list_2000_tokens):
|
| 48 |
try:
|
| 49 |
frames = []
|
| 50 |
+
is_spelling = False
|
| 51 |
+
|
| 52 |
+
for gloss in gloss_list:
|
| 53 |
+
if gloss == 'FINGERSPELL-START':
|
| 54 |
+
is_spelling = True
|
| 55 |
+
continue
|
| 56 |
+
elif gloss == 'FINGERSPELL-END':
|
| 57 |
+
is_spelling = False
|
| 58 |
+
continue
|
| 59 |
+
|
| 60 |
+
for frame in dg.generate_video([gloss], dataset, list_2000_tokens):
|
| 61 |
+
frame_data = frame.split(b'\r\n\r\n')[1]
|
| 62 |
+
nparr = np.frombuffer(frame_data, np.uint8)
|
| 63 |
+
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 64 |
+
frames.append(img)
|
| 65 |
|
| 66 |
if not frames:
|
| 67 |
raise Exception("No frames generated")
|
|
|
|
| 106 |
eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text)
|
| 107 |
generated_gloss = eng_to_asl_translator.translate_to_gloss()
|
| 108 |
|
| 109 |
+
# 단어 처리
|
| 110 |
+
processed_gloss = []
|
| 111 |
+
words = generated_gloss.split()
|
| 112 |
+
|
| 113 |
+
for word in words:
|
| 114 |
+
if is_proper_noun(word):
|
| 115 |
+
# 고유명사인 경우 철자를 하나씩 분리
|
| 116 |
+
spelled_word = spell_out_word(word)
|
| 117 |
+
processed_gloss.extend(['FINGERSPELL-START'] + spelled_word.split() + ['FINGERSPELL-END'])
|
| 118 |
+
else:
|
| 119 |
+
# 일반 단어는 기존 방식대로 처리
|
| 120 |
+
word_lower = word.lower()
|
| 121 |
+
if word_lower.isalnum():
|
| 122 |
+
processed_gloss.append(word_lower)
|
| 123 |
+
|
| 124 |
+
gloss_sentence_before_synonym = " ".join(processed_gloss)
|
| 125 |
+
|
| 126 |
+
# 고유명사가 아닌 단어들만 동의어 처리
|
| 127 |
+
final_gloss = []
|
| 128 |
+
i = 0
|
| 129 |
+
while i < len(processed_gloss):
|
| 130 |
+
if processed_gloss[i] == 'FINGERSPELL-START':
|
| 131 |
+
# 철자 처리 부분을 그대로 유지
|
| 132 |
+
final_gloss.append(processed_gloss[i])
|
| 133 |
+
i += 1
|
| 134 |
+
while i < len(processed_gloss) and processed_gloss[i] != 'FINGERSPELL-END':
|
| 135 |
+
final_gloss.append(processed_gloss[i])
|
| 136 |
+
i += 1
|
| 137 |
+
final_gloss.append('FINGERSPELL-END')
|
| 138 |
+
i += 1
|
| 139 |
+
else:
|
| 140 |
+
# 일반 단어는 동의어 처리
|
| 141 |
+
word = processed_gloss[i]
|
| 142 |
+
final_gloss.append(sp.find_synonyms(word, nlp, dict_docs_spacy, list_2000_tokens))
|
| 143 |
+
i += 1
|
| 144 |
|
| 145 |
+
gloss_sentence_after_synonym = " ".join(final_gloss)
|
|
|
|
|
|
|
| 146 |
|
| 147 |
return render_template('result.html',
|
| 148 |
title=app.config['TITLE'],
|