Skip to content

Commit

Permalink
add --skip-lang
Browse files Browse the repository at this point in the history
  • Loading branch information
zyddnys committed May 1, 2024
1 parent ae2b3a7 commit b5f91d9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ Colorizer: **mc2**
--save-text-file SAVE_TEXT_FILE Like --save-text but with a specified file path.
--filter-text FILTER_TEXT Filter regions by their text with a regex. Example
usage: --text-filter ".*badtext.*"
--skip-lang Skip translation if source image is one of the provide languages,
use comma to separate multiple languages. Example: JPN,ENG
--prep-manual Prepare for manual typesetting by outputting blank,
inpainted images, plus copies of the original for
reference
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ FIL: Filipino (Tagalog)
--save-text-file SAVE_TEXT_FILE Like --save-text but with a specified file path.
--filter-text FILTER_TEXT Filter regions by their text with a regex. Example
usage: --text-filter ".*badtext.*"
--skip-lang Skip translation if source image is one of the provide languages,
use comma to separate multiple languages. Example: JPN,ENG
--prep-manual Prepare for manual typesetting by outputting blank,
inpainted images, plus copies of the original for
reference
Expand Down
14 changes: 14 additions & 0 deletions manga_translator/manga_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from aiohttp.web_middlewares import middleware
from omegaconf import OmegaConf
import langcodes
import langdetect
import requests
import os
import re
Expand Down Expand Up @@ -50,6 +51,7 @@
from .translators import (
TRANSLATORS,
VALID_LANGUAGES,
LANGDETECT_MAP,
LanguageUnsupportedException,
TranslatorChain,
dispatch as dispatch_translation,
Expand Down Expand Up @@ -395,6 +397,16 @@ async def _translate(self, ctx: Context) -> Context:
# If no text was found result is intermediate image product
ctx.result = ctx.upscaled
return ctx

if ctx.skip_lang is not None :
skip_langs = ctx.skip_lang.split(',')
detected_text = ''.join([l.text for l in ctx.textlines])
source_language = LANGDETECT_MAP.get(langdetect.detect(detected_text), 'UNKNOWN')
if source_language in skip_langs :
print('skip due to', source_language, 'in', skip_langs)
await self._report_progress('finished', True)
ctx.result = ctx.upscaled
return ctx

# -- Textline merge
await self._report_progress('textline_merge')
Expand Down Expand Up @@ -846,6 +858,7 @@ async def server_process_inner(main_loop, logger_task, session, websocket, task)

params = {
'target_lang': task.target_language,
'skip_lang': task.skip_language,
'detector': task.detector,
'direction': task.direction,
'translator': task.translator,
Expand Down Expand Up @@ -1269,6 +1282,7 @@ class PostSchema(Schema):
upscaler = fields.Str(required=False, validate=lambda a: a.lower() in UPSCALERS)
translator = fields.Str(required=False, validate=lambda a: a.lower() in TRANSLATORS)
direction = fields.Str(required=False, validate=lambda a: a.lower() in {'auto', 'h', 'v'})
skip_language = fields.Str(required=False)
upscale_ratio = fields.Integer(required=False)
translator_chain = fields.Str(required=False)
selective_translation = fields.Str(required=False)
Expand Down
27 changes: 27 additions & 0 deletions manga_translator/translators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,30 @@ async def dispatch(chain: TranslatorChain, queries: List[str], use_mtpe: bool =
if args is not None:
args['translations'][tgt_lang] = queries
return queries

LANGDETECT_MAP = {
'zh-cn': 'CHS',
'zh-tw': 'CHT',
'cs': 'CSY',
'nl': 'NLD',
'en': 'ENG',
'fr': 'FRA',
'de': 'DEU',
'hu': 'HUN',
'it': 'ITA',
'ja': 'JPN',
'ko': 'KOR',
'pl': 'PLK',
'pt': 'PTB',
'ro': 'ROM',
'ru': 'RUS',
'es': 'ESP',
'tr': 'TRK',
'uk': 'UKR',
'vi': 'VIN',
'ar': 'ARA',
'hr': 'HRV',
'th': 'THA',
'id': 'IND',
'tl': 'FIL'
}
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry.dependencies]
python = ">=3.10, <3.13"
langcodes = "^3.3.0"
langdetect = ">=1.0.0"
networkx = "^3.1"
torch = "^2.0.1"
torchvision = "^0.15.2"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ aiofiles
arabic-reshaper
pyhyphen
langcodes
langdetect
manga-ocr
pydensecrf@https://github.com/lucasb-eyer/pydensecrf/archive/refs/heads/master.zip

0 comments on commit b5f91d9

Please sign in to comment.