ginipick commited on
Commit
c050c45
·
verified ·
1 Parent(s): 3709d93

Update src/main.py

Browse files
Files changed (1) hide show
  1. src/main.py +96 -87
src/main.py CHANGED
@@ -25,104 +25,61 @@ def spell_out_word(word):
25
  """단어를 개별 알파벳으로 분리하는 함수"""
26
  return ' '.join(list(word.lower()))
27
 
28
- def translate_korean_to_english(text):
 
29
  try:
30
- # 작은따옴표로 묶인 단어들 찾기
31
- quoted_words = find_quoted_words(text)
32
- translated_quoted_words = []
33
-
34
- # 각 따옴표 단어를 먼저 번역
35
  url = "https://translate.googleapis.com/translate_a/single"
36
- for word in quoted_words:
37
- params = {
38
- "client": "gtx",
39
- "sl": "ko",
40
- "tl": "en",
41
- "dt": "t",
42
- "q": word
43
- }
44
- response = requests.get(url, params=params)
45
- if response.status_code == 200:
46
- translated_word = response.json()[0][0][0].upper()
47
- translated_quoted_words.append(translated_word)
48
- else:
49
- translated_quoted_words.append(word)
50
-
51
- # 원본 텍스트에서 따옴표 부분을 임시 마커로 대체
52
- temp_text = text
53
- for i, word in enumerate(quoted_words):
54
- temp_text = temp_text.replace(f"'{word}'", f"QUOTED_WORD_{i}")
55
-
56
- # 전체 문장 번역
57
  params = {
58
  "client": "gtx",
59
  "sl": "ko",
60
  "tl": "en",
61
  "dt": "t",
62
- "q": temp_text.strip()
63
  }
64
  response = requests.get(url, params=params)
65
-
66
  if response.status_code == 200:
67
- translated_text = ' '.join(item[0] for item in response.json()[0] if item[0])
68
-
69
- # 번역된 텍스트에서 마커를 번역된 따옴표 단어로 대체
70
- for i, translated_word in enumerate(translated_quoted_words):
71
- translated_text = translated_text.replace(f"QUOTED_WORD_{i}", f"'{translated_word}'")
72
-
73
- return translated_text
74
- else:
75
- raise Exception(f"Translation API returned status code: {response.status_code}")
76
  except Exception as e:
77
- print(f"Translation error: {e}")
78
- return text
79
 
80
- def generate_complete_video(gloss_list, dataset, list_2000_tokens):
 
81
  try:
82
- frames = []
83
- is_spelling = False
84
-
85
- for gloss in gloss_list:
86
- if gloss == 'FINGERSPELL-START':
87
- is_spelling = True
88
- continue
89
- elif gloss == 'FINGERSPELL-END':
90
- is_spelling = False
91
- continue
92
-
93
- for frame in dg.generate_video([gloss], dataset, list_2000_tokens):
94
- frame_data = frame.split(b'\r\n\r\n')[1]
95
- nparr = np.frombuffer(frame_data, np.uint8)
96
- img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
97
- frames.append(img)
98
 
99
- if not frames:
100
- raise Exception("No frames generated")
101
-
102
- height, width = frames[0].shape[:2]
103
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
104
-
105
- with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
106
- temp_path = temp_file.name
107
-
108
- out = cv2.VideoWriter(temp_path, fourcc, 25, (width, height))
109
-
110
- for frame in frames:
111
- out.write(frame)
112
- out.release()
113
-
114
- with open(temp_path, 'rb') as f:
115
- video_bytes = f.read()
 
 
 
116
 
117
- os.remove(temp_path)
118
- return video_bytes
 
119
  except Exception as e:
120
- print(f"Error generating video: {str(e)}")
121
- raise
122
-
123
- @app.route('/')
124
- def index():
125
- return render_template('index.html', title=app.config['TITLE'])
126
 
127
  @app.route('/translate/', methods=['POST'])
128
  def result():
@@ -132,14 +89,17 @@ def result():
132
  return render_template('error.html', error="Please enter text to translate")
133
 
134
  try:
 
135
  english_text = translate_korean_to_english(input_text)
136
  if not english_text:
137
  raise Exception("Translation failed")
138
-
139
- # 작은따옴표로 묶인 단어들 찾기
140
- quoted_words = find_quoted_words(english_text)
141
 
142
- eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=english_text.replace("'", ""))
 
 
 
 
 
143
  generated_gloss = eng_to_asl_translator.translate_to_gloss()
144
 
145
  # 단어 처리
@@ -148,7 +108,7 @@ def result():
148
 
149
  for word in words:
150
  word_upper = word.upper()
151
- if any(quoted_word.upper() == word_upper for quoted_word in quoted_words):
152
  # 고유명사인 경우 철자를 하나씩 분리
153
  spelled_word = spell_out_word(word)
154
  processed_gloss.extend(['FINGERSPELL-START'] + spelled_word.split() + ['FINGERSPELL-END'])
@@ -190,6 +150,55 @@ def result():
190
  except Exception as e:
191
  return render_template('error.html', error=f"Translation error: {str(e)}")
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  @app.route('/video_feed')
194
  def video_feed():
195
  sentence = request.args.get('gloss_sentence_to_display', '')
 
25
  """단어를 개별 알파벳으로 분리하는 함수"""
26
  return ' '.join(list(word.lower()))
27
 
28
+ def translate_quoted_word(word):
29
+ """따옴표 안의 단어를 개별적으로 번역"""
30
  try:
 
 
 
 
 
31
  url = "https://translate.googleapis.com/translate_a/single"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  params = {
33
  "client": "gtx",
34
  "sl": "ko",
35
  "tl": "en",
36
  "dt": "t",
37
+ "q": word
38
  }
39
  response = requests.get(url, params=params)
 
40
  if response.status_code == 200:
41
+ translated = response.json()[0][0][0].upper()
42
+ return translated
43
+ return word
 
 
 
 
 
 
44
  except Exception as e:
45
+ print(f"Word translation error: {e}")
46
+ return word
47
 
48
+ def translate_korean_to_english(text):
49
+ """전체 텍스트 번역 함수"""
50
  try:
51
+ # 1. 따옴표로 묶인 부분을 찾아서 따로 번역
52
+ pattern = r"'([^']*)'|([^']+)"
53
+ parts = re.findall(pattern, text)
54
+ translated_parts = []
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ for quoted, unquoted in parts:
57
+ if quoted: # 따옴표로 묶인 부분
58
+ translated_word = translate_quoted_word(quoted)
59
+ translated_parts.append(f"'{translated_word}'")
60
+ elif unquoted: # 일반 텍스트
61
+ # 일반 텍스트 번역
62
+ url = "https://translate.googleapis.com/translate_a/single"
63
+ params = {
64
+ "client": "gtx",
65
+ "sl": "ko",
66
+ "tl": "en",
67
+ "dt": "t",
68
+ "q": unquoted.strip()
69
+ }
70
+ response = requests.get(url, params=params)
71
+ if response.status_code == 200:
72
+ translated = ' '.join(item[0] for item in response.json()[0] if item[0])
73
+ translated_parts.append(translated)
74
+ else:
75
+ translated_parts.append(unquoted)
76
 
77
+ # 번역된 부분들을 합치기
78
+ result = ''.join(translated_parts).strip()
79
+ return result
80
  except Exception as e:
81
+ print(f"Translation error: {e}")
82
+ return text
 
 
 
 
83
 
84
  @app.route('/translate/', methods=['POST'])
85
  def result():
 
89
  return render_template('error.html', error="Please enter text to translate")
90
 
91
  try:
92
+ # 번역 수행
93
  english_text = translate_korean_to_english(input_text)
94
  if not english_text:
95
  raise Exception("Translation failed")
 
 
 
96
 
97
+ # 따옴표로 묶인 단어 추출 (번역된 영어 텍스트에서)
98
+ quoted_words = re.findall(r"'([^']*)'", english_text)
99
+
100
+ # 번역된 텍스트에서 따옴표 제거하고 ASL 변환
101
+ clean_english = re.sub(r"'([^']*)'", r"\1", english_text)
102
+ eng_to_asl_translator = NlpSpacyBaseTranslator(sentence=clean_english)
103
  generated_gloss = eng_to_asl_translator.translate_to_gloss()
104
 
105
  # 단어 처리
 
108
 
109
  for word in words:
110
  word_upper = word.upper()
111
+ if any(quoted.upper() == word_upper for quoted in quoted_words):
112
  # 고유명사인 경우 철자를 하나씩 분리
113
  spelled_word = spell_out_word(word)
114
  processed_gloss.extend(['FINGERSPELL-START'] + spelled_word.split() + ['FINGERSPELL-END'])
 
150
  except Exception as e:
151
  return render_template('error.html', error=f"Translation error: {str(e)}")
152
 
153
+ def generate_complete_video(gloss_list, dataset, list_2000_tokens):
154
+ try:
155
+ frames = []
156
+ is_spelling = False
157
+
158
+ for gloss in gloss_list:
159
+ if gloss == 'FINGERSPELL-START':
160
+ is_spelling = True
161
+ continue
162
+ elif gloss == 'FINGERSPELL-END':
163
+ is_spelling = False
164
+ continue
165
+
166
+ for frame in dg.generate_video([gloss], dataset, list_2000_tokens):
167
+ frame_data = frame.split(b'\r\n\r\n')[1]
168
+ nparr = np.frombuffer(frame_data, np.uint8)
169
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
170
+ frames.append(img)
171
+
172
+ if not frames:
173
+ raise Exception("No frames generated")
174
+
175
+ height, width = frames[0].shape[:2]
176
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
177
+
178
+ with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_file:
179
+ temp_path = temp_file.name
180
+
181
+ out = cv2.VideoWriter(temp_path, fourcc, 25, (width, height))
182
+
183
+ for frame in frames:
184
+ out.write(frame)
185
+ out.release()
186
+
187
+ with open(temp_path, 'rb') as f:
188
+ video_bytes = f.read()
189
+
190
+ os.remove(temp_path)
191
+ return video_bytes
192
+ except Exception as e:
193
+ print(f"Error generating video: {str(e)}")
194
+ raise
195
+
196
+ @app.route('/')
197
+ def index():
198
+ return render_template('index.html', title=app.config['TITLE'])
199
+
200
+
201
+
202
  @app.route('/video_feed')
203
  def video_feed():
204
  sentence = request.args.get('gloss_sentence_to_display', '')