Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- add_text.py +65 -0
- translator.py +41 -0
add_text.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
+
import numpy as np
|
| 3 |
+
import textwrap
|
| 4 |
+
import cv2
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def add_text(image, text, font_path, bubble_contour):
|
| 8 |
+
"""
|
| 9 |
+
Add text inside a speech bubble contour.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
image (numpy.ndarray): Processed bubble image (cv2 format - BGR).
|
| 13 |
+
text (str): Text to be placed inside the speech bubble.
|
| 14 |
+
font_path (str): Font path.
|
| 15 |
+
bubble_contour (numpy.ndarray): Contour of the detected speech bubble.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
numpy.ndarray: Image with text placed inside the speech bubble.
|
| 19 |
+
"""
|
| 20 |
+
pil_image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
| 21 |
+
draw = ImageDraw.Draw(pil_image)
|
| 22 |
+
|
| 23 |
+
x, y, w, h = cv2.boundingRect(bubble_contour)
|
| 24 |
+
|
| 25 |
+
line_height = 16
|
| 26 |
+
font_size = 14
|
| 27 |
+
wrapping_ratio = 0.075
|
| 28 |
+
|
| 29 |
+
wrapped_text = textwrap.fill(text, width=int(w * wrapping_ratio),
|
| 30 |
+
break_long_words=True)
|
| 31 |
+
|
| 32 |
+
font = ImageFont.truetype(font_path, size=font_size)
|
| 33 |
+
|
| 34 |
+
lines = wrapped_text.split('\n')
|
| 35 |
+
total_text_height = (len(lines)) * line_height
|
| 36 |
+
|
| 37 |
+
while total_text_height > h:
|
| 38 |
+
line_height -= 2
|
| 39 |
+
font_size -= 2
|
| 40 |
+
wrapping_ratio += 0.025
|
| 41 |
+
|
| 42 |
+
wrapped_text = textwrap.fill(text, width=int(w * wrapping_ratio),
|
| 43 |
+
break_long_words=True)
|
| 44 |
+
|
| 45 |
+
font = ImageFont.truetype(font_path, size=font_size)
|
| 46 |
+
|
| 47 |
+
lines = wrapped_text.split('\n')
|
| 48 |
+
total_text_height = (len(lines)) * line_height
|
| 49 |
+
|
| 50 |
+
# Vertical centering
|
| 51 |
+
text_y = y + (h - total_text_height) // 2
|
| 52 |
+
|
| 53 |
+
for line in lines:
|
| 54 |
+
text_length = draw.textlength(line, font=font)
|
| 55 |
+
|
| 56 |
+
# Horizontal centering
|
| 57 |
+
text_x = x + (w - text_length) // 2
|
| 58 |
+
|
| 59 |
+
draw.text((text_x, text_y), line, font=font, fill=(0, 0, 0))
|
| 60 |
+
|
| 61 |
+
text_y += line_height
|
| 62 |
+
|
| 63 |
+
image[:, :, :] = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
| 64 |
+
|
| 65 |
+
return image
|
translator.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from deep_translator import GoogleTranslator
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class MangaTranslator:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self.target = "en"
|
| 8 |
+
self.source = "ja"
|
| 9 |
+
|
| 10 |
+
def translate(self, text, method="google"):
|
| 11 |
+
"""
|
| 12 |
+
Translates the given text to the target language using the specified method.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
text (str): The text to be translated.
|
| 16 |
+
method (str):'google' for Google Translator,
|
| 17 |
+
'hf' for Helsinki-NLP's opus-mt-ja-en model (HF pipeline)
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
str: The translated text.
|
| 21 |
+
"""
|
| 22 |
+
if method == "hf":
|
| 23 |
+
return self._translate_with_hf(self._preprocess_text(text))
|
| 24 |
+
elif method == "google":
|
| 25 |
+
return self._translate_with_google(self._preprocess_text(text))
|
| 26 |
+
else:
|
| 27 |
+
raise ValueError("Invalid translation method.")
|
| 28 |
+
|
| 29 |
+
def _translate_with_google(self, text):
|
| 30 |
+
translator = GoogleTranslator(source=self.source, target=self.target)
|
| 31 |
+
translated_text = translator.translate(text)
|
| 32 |
+
return translated_text if translated_text != None else text
|
| 33 |
+
|
| 34 |
+
def _translate_with_hf(self, text):
|
| 35 |
+
pipe = pipeline("translation", model=f"Helsinki-NLP/opus-mt-ja-en")
|
| 36 |
+
translated_text = pipe(text)[0]["translation_text"]
|
| 37 |
+
return translated_text if translated_text != None else text
|
| 38 |
+
|
| 39 |
+
def _preprocess_text(self, text):
|
| 40 |
+
preprocessed_text = text.replace(".", ".")
|
| 41 |
+
return preprocessed_text
|