forked from ogkalu2/comic-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.py
460 lines (376 loc) · 22.3 KB
/
pipeline.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import os, json
import cv2, shutil
from datetime import datetime
from typing import List
from PySide6 import QtCore
from PySide6.QtGui import QColor
from modules.detection import TextBlockDetector
from modules.ocr.ocr import OCRProcessor
from modules.translator import Translator
from modules.utils.textblock import TextBlock, sort_blk_list
from modules.utils.pipeline_utils import inpaint_map, get_config
from modules.rendering.render import get_best_render_area, pyside_word_wrap
from modules.utils.pipeline_utils import generate_mask, get_language_code, is_directory_empty
from modules.utils.translator_utils import get_raw_translation, get_raw_text, format_translations, set_upper_case
from modules.utils.archives import make
from app.ui.canvas.rectangle import MoveableRectItem
from app.ui.canvas.text_item import SelectionOutlineInfo
from app.ui.canvas.save_renderer import ImageSaveRenderer
class ComicTranslatePipeline:
def __init__(self, main_page):
self.main_page = main_page
self.block_detector_cache = None
self.inpainter_cache = None
self.cached_inpainter_key = None
self.ocr = OCRProcessor()
def load_box_coords(self, blk_list: List[TextBlock]):
self.main_page.image_viewer.clear_rectangles()
if self.main_page.image_viewer.hasPhoto() and blk_list:
for blk in blk_list:
x1, y1, x2, y2 = blk.xyxy
rect = QtCore.QRectF(0, 0, x2 - x1, y2 - y1)
rect_item = MoveableRectItem(rect, self.main_page.image_viewer.photo)
if blk.tr_origin_point:
rect_item.setTransformOriginPoint(QtCore.QPointF(*blk.tr_origin_point))
rect_item.setPos(x1,y1)
rect_item.setRotation(blk.angle)
self.main_page.connect_rect_item_signals(rect_item)
self.main_page.image_viewer.rectangles.append(rect_item)
rect = self.main_page.find_corresponding_rect(self.main_page.blk_list[0], 0.5)
self.main_page.image_viewer.select_rectangle(rect)
self.main_page.set_tool('box')
def detect_blocks(self, load_rects=True):
if self.main_page.image_viewer.hasPhoto():
if self.block_detector_cache is None:
device = 0 if self.main_page.settings_page.is_gpu_enabled() else 'cpu'
self.block_detector_cache = TextBlockDetector('models/detection/comic-speech-bubble-detector.pt',
'models/detection/comic-text-segmenter.pt','models/detection/manga-text-detector.pt',
device)
image = self.main_page.image_viewer.get_cv2_image()
blk_list = self.block_detector_cache.detect(image)
return blk_list, load_rects
def on_blk_detect_complete(self, result):
blk_list, load_rects = result
source_lang = self.main_page.s_combo.currentText()
source_lang_english = self.main_page.lang_mapping.get(source_lang, source_lang)
rtl = True if source_lang_english == 'Japanese' else False
blk_list = sort_blk_list(blk_list, rtl)
self.main_page.blk_list = blk_list
if load_rects:
self.load_box_coords(blk_list)
def manual_inpaint(self):
image_viewer = self.main_page.image_viewer
settings_page = self.main_page.settings_page
mask = image_viewer.get_mask_for_inpainting()
image = image_viewer.get_cv2_image()
if self.inpainter_cache is None or self.cached_inpainter_key != settings_page.get_tool_selection('inpainter'):
device = 'cuda' if settings_page.is_gpu_enabled() else 'cpu'
inpainter_key = settings_page.get_tool_selection('inpainter')
InpainterClass = inpaint_map[inpainter_key]
self.inpainter_cache = InpainterClass(device)
self.cached_inpainter_key = inpainter_key
config = get_config(settings_page)
inpaint_input_img = self.inpainter_cache(image, mask, config)
inpaint_input_img = cv2.convertScaleAbs(inpaint_input_img)
return inpaint_input_img
def inpaint_complete(self, result):
inpainted, original_image = result
self.main_page.set_cv2_image(inpainted)
# get_best_render_area(self.main_page.blk_list, original_image, inpainted)
def inpaint(self):
image = self.main_page.image_viewer.get_cv2_image()
inpainted = self.manual_inpaint()
return inpainted, image
def get_selected_block(self):
rect = self.main_page.image_viewer.selected_rect
srect = rect.mapRectToScene(rect.rect())
srect_coords = srect.getCoords()
blk = self.main_page.find_corresponding_text_block(srect_coords)
return blk
def OCR_image(self, single_block=False):
source_lang = self.main_page.s_combo.currentText()
if self.main_page.image_viewer.hasPhoto() and self.main_page.image_viewer.rectangles:
image = self.main_page.image_viewer.get_cv2_image()
self.ocr.initialize(self.main_page, source_lang)
if single_block:
blk = self.get_selected_block()
self.ocr.process(image, [blk])
else:
self.ocr.process(image, self.main_page.blk_list)
print("Block Length: ", len(self.main_page.blk_list))
def translate_image(self, single_block=False):
source_lang = self.main_page.s_combo.currentText()
target_lang = self.main_page.t_combo.currentText()
if self.main_page.image_viewer.hasPhoto() and self.main_page.blk_list:
settings_page = self.main_page.settings_page
image = self.main_page.image_viewer.get_cv2_image()
extra_context = settings_page.get_llm_settings()['extra_context']
target_lang_en = self.main_page.lang_mapping.get(target_lang, None)
trg_lng_cd = get_language_code(target_lang_en)
upper_case = settings_page.ui.uppercase_checkbox.isChecked()
translator = Translator(self.main_page, source_lang, target_lang)
if single_block:
blk = self.get_selected_block()
translator.translate([blk], image, extra_context)
set_upper_case([blk], upper_case)
else:
translator.translate(self.main_page.blk_list, image, extra_context)
set_upper_case(self.main_page.blk_list, upper_case)
def skip_save(self, directory, timestamp, base_name, extension, archive_bname, image):
path = os.path.join(directory, f"comic_translate_{timestamp}", "translated_images", archive_bname)
if not os.path.exists(path):
os.makedirs(path, exist_ok=True)
cv2.imwrite(os.path.join(path, f"{base_name}_translated{extension}"), image)
def log_skipped_image(self, directory, timestamp, image_path):
with open(os.path.join(directory, f"comic_translate_{timestamp}", "skipped_images.txt"), 'a', encoding='UTF-8') as file:
file.write(image_path + "\n")
def batch_process(self):
timestamp = datetime.now().strftime("%b-%d-%Y_%I-%M-%S%p")
total_images = len(self.main_page.image_files)
for index, image_path in enumerate(self.main_page.image_files):
# index, step, total_steps, change_name
self.main_page.progress_update.emit(index, total_images, 0, 10, True)
settings_page = self.main_page.settings_page
source_lang = self.main_page.image_states[image_path]['source_lang']
target_lang = self.main_page.image_states[image_path]['target_lang']
target_lang_en = self.main_page.lang_mapping.get(target_lang, None)
trg_lng_cd = get_language_code(target_lang_en)
base_name = os.path.splitext(os.path.basename(image_path))[0]
extension = os.path.splitext(image_path)[1]
directory = os.path.dirname(image_path)
archive_bname = ""
for archive in self.main_page.file_handler.archive_info:
images = archive['extracted_images']
archive_path = archive['archive_path']
for img_pth in images:
if img_pth == image_path:
directory = os.path.dirname(archive_path)
archive_bname = os.path.splitext(os.path.basename(archive_path))[0]
image = cv2.imread(image_path)
# Text Block Detection
self.main_page.progress_update.emit(index, total_images, 1, 10, False)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
if self.block_detector_cache is None:
bdetect_device = 0 if self.main_page.settings_page.is_gpu_enabled() else 'cpu'
self.block_detector_cache = TextBlockDetector('models/detection/comic-speech-bubble-detector.pt',
'models/detection/comic-text-segmenter.pt', 'models/detection/manga-text-detector.pt',
bdetect_device)
blk_list = self.block_detector_cache.detect(image)
self.main_page.progress_update.emit(index, total_images, 2, 10, False)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
if blk_list:
self.ocr.initialize(self.main_page, source_lang)
try:
self.ocr.process(image, blk_list)
source_lang_english = self.main_page.lang_mapping.get(source_lang, source_lang)
rtl = True if source_lang_english == 'Japanese' else False
blk_list = sort_blk_list(blk_list, rtl)
except Exception as e:
error_message = str(e)
print(error_message)
self.skip_save(directory, timestamp, base_name, extension, archive_bname, image)
self.main_page.image_skipped.emit(image_path, "OCR", error_message)
self.log_skipped_image(directory, timestamp, image_path)
continue
else:
self.skip_save(directory, timestamp, base_name, extension, archive_bname, image)
self.main_page.image_skipped.emit(image_path, "Text Blocks", "")
self.log_skipped_image(directory, timestamp, image_path)
continue
self.main_page.progress_update.emit(index, total_images, 3, 10, False)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
# Clean Image of text
export_settings = settings_page.get_export_settings()
if self.inpainter_cache is None or self.cached_inpainter_key != settings_page.get_tool_selection('inpainter'):
device = 'cuda' if settings_page.is_gpu_enabled() else 'cpu'
inpainter_key = settings_page.get_tool_selection('inpainter')
InpainterClass = inpaint_map[inpainter_key]
self.inpainter_cache = InpainterClass(device)
self.cached_inpainter_key = inpainter_key
config = get_config(settings_page)
mask = generate_mask(image, blk_list)
self.main_page.progress_update.emit(index, total_images, 4, 10, False)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
inpaint_input_img = self.inpainter_cache(image, mask, config)
inpaint_input_img = cv2.convertScaleAbs(inpaint_input_img)
# Saving cleaned image
self.main_page.image_history[image_path] = [image_path]
self.main_page.current_history_index[image_path] = 0
self.main_page.image_processed.emit(index, inpaint_input_img, image_path)
inpaint_input_img = cv2.cvtColor(inpaint_input_img, cv2.COLOR_BGR2RGB)
if export_settings['export_inpainted_image']:
path = os.path.join(directory, f"comic_translate_{timestamp}", "cleaned_images", archive_bname)
if not os.path.exists(path):
os.makedirs(path, exist_ok=True)
cv2.imwrite(os.path.join(path, f"{base_name}_cleaned{extension}"), inpaint_input_img)
self.main_page.progress_update.emit(index, total_images, 5, 10, False)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
# Get Translations/ Export if selected
extra_context = settings_page.get_llm_settings()['extra_context']
translator = Translator(self.main_page, source_lang, target_lang)
try:
translator.translate(blk_list, image, extra_context)
except Exception as e:
error_message = str(e)
print(error_message)
self.skip_save(directory, timestamp, base_name, extension, archive_bname, image)
self.main_page.image_skipped.emit(image_path, "Translator", error_message)
self.log_skipped_image(directory, timestamp, image_path)
continue
entire_raw_text = get_raw_text(blk_list)
entire_translated_text = get_raw_translation(blk_list)
# Parse JSON strings and check if they're empty objects or invalid
try:
raw_text_obj = json.loads(entire_raw_text)
translated_text_obj = json.loads(entire_translated_text)
if (not raw_text_obj) or (not translated_text_obj):
self.skip_save(directory, timestamp, base_name, extension, archive_bname, image)
self.main_page.image_skipped.emit(image_path, "Translator", "")
self.log_skipped_image(directory, timestamp, image_path)
continue
except json.JSONDecodeError as e:
# Handle invalid JSON
error_message = str(e)
self.skip_save(directory, timestamp, base_name, extension, archive_bname, image)
self.main_page.image_skipped.emit(image_path, "Translator", error_message)
self.log_skipped_image(directory, timestamp, image_path)
continue
if export_settings['export_raw_text']:
path = os.path.join(directory, f"comic_translate_{timestamp}", "raw_texts", archive_bname)
if not os.path.exists(path):
os.makedirs(path, exist_ok=True)
file = open(os.path.join(path, os.path.splitext(os.path.basename(image_path))[0] + "_raw.txt"), 'w', encoding='UTF-8')
file.write(entire_raw_text)
if export_settings['export_translated_text']:
path = os.path.join(directory, f"comic_translate_{timestamp}", "translated_texts", archive_bname)
if not os.path.exists(path):
os.makedirs(path, exist_ok=True)
file = open(os.path.join(path, os.path.splitext(os.path.basename(image_path))[0] + "_translated.txt"), 'w', encoding='UTF-8')
file.write(entire_translated_text)
self.main_page.progress_update.emit(index, total_images, 7, 10, False)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
# Text Rendering
render_settings = self.main_page.render_settings()
upper_case = render_settings.upper_case
outline = render_settings.outline
format_translations(blk_list, trg_lng_cd, upper_case=upper_case)
get_best_render_area(blk_list, image, inpaint_input_img)
font = render_settings.font_family
font_color = QColor(render_settings.color)
max_font_size = render_settings.max_font_size
min_font_size = render_settings.min_font_size
line_spacing = float(render_settings.line_spacing)
outline_width = float(render_settings.outline_width)
outline_color = QColor(render_settings.outline_color)
bold = render_settings.bold
italic = render_settings.italic
underline = render_settings.underline
alignment_id = render_settings.alignment_id
alignment = self.main_page.button_to_alignment[alignment_id]
text_items_state = []
for blk in blk_list:
x1, y1, width, height = blk.xywh
translation = blk.translation
if not translation or len(translation) == 1:
continue
translation, font_size = pyside_word_wrap(translation, font, width, height,
line_spacing, outline_width, bold, italic, underline,
alignment, max_font_size, min_font_size)
# Display text if on current page
if index == self.main_page.curr_img_idx:
self.main_page.blk_rendered.emit(translation, font_size, blk)
if any(lang in trg_lng_cd.lower() for lang in ['zh', 'ja', 'th']):
translation = translation.replace(' ', '')
text_items_state.append({
'text': translation,
'font_family': font,
'font_size': font_size,
'text_color': font_color,
'alignment': alignment,
'line_spacing': line_spacing,
'outline_color': outline_color,
'outline_width': outline_width,
'bold': bold,
'italic': italic,
'underline': underline,
'position': (x1, y1),
'rotation': blk.angle,
'scale': 1.0,
'transform_origin': blk.tr_origin_point,
'width': width,
'selection_outlines': [SelectionOutlineInfo(0, len(translation),
outline_color, outline_width)] if outline else []
})
self.main_page.image_states[image_path]['viewer_state'].update({
'text_items_state': text_items_state
})
self.main_page.progress_update.emit(index, total_images, 9, 10, False)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
# Saving blocks with texts to history
self.main_page.image_states[image_path].update({
'blk_list': blk_list
})
if index == self.main_page.curr_img_idx:
self.main_page.blk_list = blk_list
render_save_dir = os.path.join(directory, f"comic_translate_{timestamp}", "translated_images", archive_bname)
if not os.path.exists(render_save_dir):
os.makedirs(render_save_dir, exist_ok=True)
sv_pth = os.path.join(render_save_dir, f"{base_name}_translated{extension}")
im = cv2.cvtColor(inpaint_input_img, cv2.COLOR_RGB2BGR)
renderer = ImageSaveRenderer(im)
viewer_state = self.main_page.image_states[image_path]['viewer_state']
renderer.add_state_to_image(viewer_state)
renderer.save_image(sv_pth)
self.main_page.progress_update.emit(index, total_images, 10, 10, False)
archive_info_list = self.main_page.file_handler.archive_info
if archive_info_list:
save_as_settings = settings_page.get_export_settings()['save_as']
for archive_index, archive in enumerate(archive_info_list):
archive_index_input = total_images + archive_index
self.main_page.progress_update.emit(archive_index_input, total_images, 1, 3, True)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
archive_path = archive['archive_path']
archive_ext = os.path.splitext(archive_path)[1]
archive_bname = os.path.splitext(os.path.basename(archive_path))[0]
archive_directory = os.path.dirname(archive_path)
save_as_ext = f".{save_as_settings[archive_ext.lower()]}"
save_dir = os.path.join(archive_directory, f"comic_translate_{timestamp}", "translated_images", archive_bname)
check_from = os.path.join(archive_directory, f"comic_translate_{timestamp}")
self.main_page.progress_update.emit(archive_index_input, total_images, 2, 3, True)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
# Create the new archive
output_base_name = f"{archive_bname}"
target_lang = self.main_page.image_states[archive['extracted_images'][0]]['target_lang']
target_lang_en = self.main_page.lang_mapping.get(target_lang, target_lang)
trg_lng_code = get_language_code(target_lang_en)
make(save_as_ext=save_as_ext, input_dir=save_dir,
output_dir=archive_directory, output_base_name=output_base_name,
trg_lng=trg_lng_code)
self.main_page.progress_update.emit(archive_index_input, total_images, 3, 3, True)
if self.main_page.current_worker and self.main_page.current_worker.is_cancelled:
self.main_page.current_worker = None
break
# Clean up temporary
if os.path.exists(save_dir):
shutil.rmtree(save_dir)
# The temp dir is removed when closing the app
if is_directory_empty(check_from):
shutil.rmtree(check_from)