StarRailCopilot/module/ocr/models.py

83 lines
1.9 KiB
Python
Raw Permalink Normal View History

from pponnxcr import TextSystem as TextSystem_
2023-09-08 14:23:57 +00:00
2023-10-17 05:57:03 +00:00
from module.base.decorator import cached_property, del_cached_property
2023-09-08 14:23:57 +00:00
from module.exception import ScriptError
DIC_LANG_TO_MODEL = {
'cn': 'zhs',
'en': 'en',
'jp': 'ja',
'tw': 'zht',
}
def lang2model(lang: str) -> str:
"""
Args:
lang: In-game language name, defined in VALID_LANG
Returns:
str: Model name, defined in pponnxcr.utility
"""
return DIC_LANG_TO_MODEL.get(lang, lang)
def model2lang(model: str) -> str:
"""
Args:
model: Model name, defined in pponnxcr.utility
Returns:
str: In-game language name, defined in VALID_LANG
"""
for k, v in DIC_LANG_TO_MODEL.items():
if model == v:
return k
return model
class TextSystem(TextSystem_):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.text_recognizer.rec_batch_num = 1
class OcrModel:
2023-09-08 14:23:57 +00:00
def get_by_model(self, model: str) -> TextSystem:
try:
return self.__getattribute__(model)
except AttributeError:
raise ScriptError(f'OCR model "{model}" does not exists')
def get_by_lang(self, lang: str) -> TextSystem:
try:
model = lang2model(lang)
return self.__getattribute__(model)
except AttributeError:
raise ScriptError(f'OCR model under lang "{lang}" does not exists')
2023-10-17 05:57:03 +00:00
def resource_release(self):
del_cached_property(self, 'zhs')
del_cached_property(self, 'en')
del_cached_property(self, 'ja')
del_cached_property(self, 'zht')
2023-09-08 14:23:57 +00:00
@cached_property
def zhs(self):
return TextSystem('zhs')
@cached_property
def en(self):
return TextSystem('en')
@cached_property
def ja(self):
2023-10-17 05:57:03 +00:00
return TextSystem('ja')
2023-09-08 14:23:57 +00:00
@cached_property
2023-09-08 14:23:57 +00:00
def zht(self):
return TextSystem('zht')
OCR_MODEL = OcrModel()