georgescutelnicu
commited on
Upload 3 files
Browse files- app.py +7 -5
- requirements +8 -0
- translator.py +28 -7
app.py
CHANGED
@@ -22,7 +22,7 @@ def predict(img, translation_method, font):
|
|
22 |
translation_method = "google"
|
23 |
if font == None:
|
24 |
font = "fonts/animeace_i.ttf"
|
25 |
-
|
26 |
results = detect_bubbles(MODEL, img)
|
27 |
|
28 |
manga_translator = MangaTranslator()
|
@@ -39,8 +39,8 @@ def predict(img, translation_method, font):
|
|
39 |
text = mocr(im)
|
40 |
|
41 |
detected_image, cont = process_bubble(detected_image)
|
42 |
-
|
43 |
-
text_translated = manga_translator.translate(text,
|
44 |
method=translation_method)
|
45 |
|
46 |
image_with_text = add_text(detected_image, text_translated, font, cont)
|
@@ -49,9 +49,11 @@ def predict(img, translation_method, font):
|
|
49 |
|
50 |
demo = gr.Interface(fn=predict,
|
51 |
inputs=["image",
|
52 |
-
gr.Dropdown([("Google", "google"),
|
53 |
("Helsinki-NLP's opus-mt-ja-en model",
|
54 |
-
"hf")
|
|
|
|
|
55 |
label="Translation Method",
|
56 |
value="google"),
|
57 |
gr.Dropdown([("animeace_i", "fonts/animeace_i.ttf"),
|
|
|
22 |
translation_method = "google"
|
23 |
if font == None:
|
24 |
font = "fonts/animeace_i.ttf"
|
25 |
+
|
26 |
results = detect_bubbles(MODEL, img)
|
27 |
|
28 |
manga_translator = MangaTranslator()
|
|
|
39 |
text = mocr(im)
|
40 |
|
41 |
detected_image, cont = process_bubble(detected_image)
|
42 |
+
|
43 |
+
text_translated = manga_translator.translate(text,
|
44 |
method=translation_method)
|
45 |
|
46 |
image_with_text = add_text(detected_image, text_translated, font, cont)
|
|
|
49 |
|
50 |
demo = gr.Interface(fn=predict,
|
51 |
inputs=["image",
|
52 |
+
gr.Dropdown([("Google", "google"),
|
53 |
("Helsinki-NLP's opus-mt-ja-en model",
|
54 |
+
"hf"),
|
55 |
+
("Baidu", "baidu"),
|
56 |
+
("Bing", "bing")],],
|
57 |
label="Translation Method",
|
58 |
value="google"),
|
59 |
gr.Dropdown([("animeace_i", "fonts/animeace_i.ttf"),
|
requirements
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
deep-translator==1.11.4
|
2 |
+
translators==5.9.1
|
3 |
+
huggingface-hub==0.22.2
|
4 |
+
manga-ocr==0.1.11
|
5 |
+
numpy==1.24.2
|
6 |
+
opencv-python==4.9.0.80
|
7 |
+
pillow==10.3.0
|
8 |
+
ultralytics==8.1.43
|
translator.py
CHANGED
@@ -1,11 +1,18 @@
|
|
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 |
"""
|
@@ -13,16 +20,18 @@ class MangaTranslator:
|
|
13 |
|
14 |
Args:
|
15 |
text (str): The text to be translated.
|
16 |
-
method (str):
|
17 |
-
|
|
|
|
|
18 |
|
19 |
Returns:
|
20 |
str: The translated text.
|
21 |
"""
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
return
|
26 |
else:
|
27 |
raise ValueError("Invalid translation method.")
|
28 |
|
@@ -36,6 +45,18 @@ class MangaTranslator:
|
|
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
|
|
|
1 |
+
from deep_translator import GoogleTranslator, LingueeTranslator
|
2 |
from transformers import pipeline
|
3 |
+
import translators as ts
|
4 |
|
5 |
|
6 |
class MangaTranslator:
|
7 |
def __init__(self):
|
8 |
self.target = "en"
|
9 |
self.source = "ja"
|
10 |
+
self.translators = {
|
11 |
+
"google": self._translate_with_google,
|
12 |
+
"hf": self._translate_with_hf,
|
13 |
+
"baidu": self._translate_with_baidu,
|
14 |
+
"bing": self._translate_with_bing
|
15 |
+
}
|
16 |
|
17 |
def translate(self, text, method="google"):
|
18 |
"""
|
|
|
20 |
|
21 |
Args:
|
22 |
text (str): The text to be translated.
|
23 |
+
method (str):"google" for Google Translator,
|
24 |
+
"hf" for Helsinki-NLP's opus-mt-ja-en model (HF pipeline)
|
25 |
+
"baidu" for Baidu Translate
|
26 |
+
"bing" for Microsoft Bing Translator
|
27 |
|
28 |
Returns:
|
29 |
str: The translated text.
|
30 |
"""
|
31 |
+
translator_func = self.translators.get(method)
|
32 |
+
|
33 |
+
if translator_func:
|
34 |
+
return translator_func(self._preprocess_text(text))
|
35 |
else:
|
36 |
raise ValueError("Invalid translation method.")
|
37 |
|
|
|
45 |
translated_text = pipe(text)[0]["translation_text"]
|
46 |
return translated_text if translated_text != None else text
|
47 |
|
48 |
+
def _translate_with_baidu(self, text):
|
49 |
+
translated_text = ts.translate_text(text, translator="baidu",
|
50 |
+
from_language="jp",
|
51 |
+
to_language=self.target)
|
52 |
+
return translated_text if translated_text != None else text
|
53 |
+
|
54 |
+
def _translate_with_bing(self, text):
|
55 |
+
translated_text = ts.translate_text(text, translator="bing",
|
56 |
+
from_language=self.source,
|
57 |
+
to_language=self.target)
|
58 |
+
return translated_text if translated_text != None else text
|
59 |
+
|
60 |
def _preprocess_text(self, text):
|
61 |
preprocessed_text = text.replace(".", ".")
|
62 |
return preprocessed_text
|