|
|
from utils.japanese_ocr import MangaOcr
|
|
|
import cv2
|
|
|
from PIL import Image
|
|
|
|
|
|
def transcript_page(test_image,translator,predictions):
|
|
|
image = cv2.imread(test_image)
|
|
|
mocr = MangaOcr()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
japanese_transcript = []
|
|
|
english_transcript = []
|
|
|
for i in range(len(predictions.panels)):
|
|
|
|
|
|
cur_panel = [b for b in predictions.panels if b['order'] == i]
|
|
|
|
|
|
if(len(cur_panel) > 0):
|
|
|
|
|
|
panel_bubbles = [b for b in predictions.speech_bubbles if b['panel_id'] == cur_panel[0]['id']]
|
|
|
panel_bubbles.sort(key=lambda b: b['seq'])
|
|
|
for bubble in panel_bubbles:
|
|
|
xmin,ymin,xmax,ymax = bubble['bbox']
|
|
|
char_id = bubble['char_id'] if 'char_id' in bubble.keys() else None
|
|
|
text = mocr(Image.fromarray(cv2.cvtColor(image[ymin:ymax, xmin:xmax], cv2.COLOR_BGR2RGB)))
|
|
|
print("Character ",char_id," : ",text)
|
|
|
japanese_transcript.append({"char_id":char_id,"text":text})
|
|
|
result = translator(text)
|
|
|
print("Character ",char_id," : ",result[0]['translation_text'])
|
|
|
english_transcript.append({"char_id":char_id,"text":result[0]['translation_text']})
|
|
|
|
|
|
return japanese_transcript,english_transcript |