aiqcamp commited on
Commit
854614c
Β·
verified Β·
1 Parent(s): 90e3ed7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -36
app.py CHANGED
@@ -76,16 +76,19 @@ def visualize(pred_mask, image_path, work_dir):
76
  cv2.imwrite(output_path, visual_result)
77
  return output_path
78
 
79
- from googletrans import Translator
80
 
81
- # λ²ˆμ—­ ν•¨μˆ˜ μΆ”κ°€
 
 
 
 
82
  def translate_to_korean(text):
83
- translator = Translator()
84
  try:
85
- result = translator.translate(text, dest='ko', src='en')
86
- return result.text
87
- except:
88
- return text # λ²ˆμ—­ μ‹€νŒ¨μ‹œ 원본 ν…μŠ€νŠΈ λ°˜ν™˜
 
89
 
90
  @spaces.GPU
91
  def image_vision(image_input_path, prompt):
@@ -111,7 +114,7 @@ def image_vision(image_input_path, prompt):
111
  # [SEG]λŠ” λ³΄μ‘΄ν•˜λ©΄μ„œ λ‚˜λ¨Έμ§€ ν…μŠ€νŠΈλ§Œ λ²ˆμ—­
112
  if '[SEG]' in answer:
113
  parts = answer.split('[SEG]')
114
- translated_parts = [translate_to_korean(part) for part in parts]
115
  answer = '[SEG]'.join(translated_parts)
116
  else:
117
  answer = translate_to_korean(answer)
@@ -152,39 +155,15 @@ def video_vision(video_input_path, prompt, video_interval):
152
  if is_korean:
153
  if '[SEG]' in prediction:
154
  parts = prediction.split('[SEG]')
155
- translated_parts = [translate_to_korean(part) for part in parts]
156
  prediction = '[SEG]'.join(translated_parts)
157
  else:
158
  prediction = translate_to_korean(prediction)
159
 
160
  if '[SEG]' in prediction and Visualizer is not None:
161
- _seg_idx = 0
162
- pred_masks = result['prediction_masks'][_seg_idx]
163
- seg_frames = []
164
- for frame_idx in range(len(vid_frames)):
165
- pred_mask = pred_masks[frame_idx]
166
- temp_dir = tempfile.mkdtemp()
167
- os.makedirs(temp_dir, exist_ok=True)
168
- seg_frame = visualize(pred_mask, image_paths[frame_idx], temp_dir)
169
- seg_frames.append(seg_frame)
170
-
171
- output_video = "output_video.mp4"
172
- frame = cv2.imread(seg_frames[0])
173
- height, width, layers = frame.shape
174
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
175
- video = cv2.VideoWriter(output_video, fourcc, new_fps, (width, height))
176
-
177
- for img_path in seg_frames:
178
- frame = cv2.imread(img_path)
179
- video.write(frame)
180
-
181
- video.release()
182
- print(f"Video created successfully at {output_video}")
183
-
184
- return prediction, output_video
185
-
186
- else:
187
- return prediction, None
188
 
189
 
190
 
 
76
  cv2.imwrite(output_path, visual_result)
77
  return output_path
78
 
 
79
 
80
+
81
+ # μ½”λ“œ 상단에 import μΆ”κ°€
82
+ from deep_translator import GoogleTranslator
83
+
84
+ # λ²ˆμ—­ ν•¨μˆ˜ μˆ˜μ •
85
  def translate_to_korean(text):
 
86
  try:
87
+ translator = GoogleTranslator(source='en', target='ko')
88
+ return translator.translate(text)
89
+ except Exception as e:
90
+ print(f"Translation error: {e}")
91
+ return text
92
 
93
  @spaces.GPU
94
  def image_vision(image_input_path, prompt):
 
114
  # [SEG]λŠ” λ³΄μ‘΄ν•˜λ©΄μ„œ λ‚˜λ¨Έμ§€ ν…μŠ€νŠΈλ§Œ λ²ˆμ—­
115
  if '[SEG]' in answer:
116
  parts = answer.split('[SEG]')
117
+ translated_parts = [translate_to_korean(part.strip()) for part in parts]
118
  answer = '[SEG]'.join(translated_parts)
119
  else:
120
  answer = translate_to_korean(answer)
 
155
  if is_korean:
156
  if '[SEG]' in prediction:
157
  parts = prediction.split('[SEG]')
158
+ translated_parts = [translate_to_korean(part.strip()) for part in parts]
159
  prediction = '[SEG]'.join(translated_parts)
160
  else:
161
  prediction = translate_to_korean(prediction)
162
 
163
  if '[SEG]' in prediction and Visualizer is not None:
164
+ # λ‚˜λ¨Έμ§€ μ½”λ“œλŠ” 동일...
165
+
166
+ return prediction, output_video if '[SEG]' in prediction else (prediction, None)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
168
 
169