Allow overriding of CLI arguments in the config
This commit is contained in:
@@ -1,9 +1,27 @@
|
|||||||
import fire
|
import fire
|
||||||
|
import inspect
|
||||||
from owocr.run import run
|
from owocr.run import run, init_config
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
init_config()
|
||||||
|
|
||||||
|
from owocr.run import config
|
||||||
|
fullargspec = inspect.getfullargspec(run)
|
||||||
|
old_defaults = fullargspec[0]
|
||||||
|
old_default_values = fullargspec[3]
|
||||||
|
new_defaults = []
|
||||||
|
|
||||||
|
if config.has_config:
|
||||||
|
index = 0
|
||||||
|
for argument in old_defaults:
|
||||||
|
if config.get_general(argument) == None:
|
||||||
|
new_defaults.append(old_default_values[index])
|
||||||
|
else:
|
||||||
|
new_defaults.append(config.get_general(argument))
|
||||||
|
index += 1
|
||||||
|
|
||||||
|
run.__defaults__ = tuple(new_defaults)
|
||||||
|
|
||||||
fire.Fire(run)
|
fire.Fire(run)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
53
owocr/config.py
Normal file
53
owocr/config.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import os
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
has_config = False
|
||||||
|
general_config = {}
|
||||||
|
engine_config = {}
|
||||||
|
|
||||||
|
def _parse(self, value):
|
||||||
|
value = value.strip()
|
||||||
|
if value.lower() == 'false':
|
||||||
|
return False
|
||||||
|
if value.lower() == 'true':
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
int(value)
|
||||||
|
return int(value)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
float(value)
|
||||||
|
return float(value)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
return value
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
config_file = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini')
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
res = config.read(config_file)
|
||||||
|
|
||||||
|
if len(res) != 0:
|
||||||
|
self.has_config = True
|
||||||
|
for key in config:
|
||||||
|
if key == 'general':
|
||||||
|
for sub_key in config[key]:
|
||||||
|
self.general_config[sub_key.lower()] = self._parse(config[key][sub_key])
|
||||||
|
elif key != 'DEFAULT':
|
||||||
|
self.engine_config[key.lower()] = {}
|
||||||
|
for sub_key in config[key]:
|
||||||
|
self.engine_config[key.lower()][sub_key.lower()] = self._parse(config[key][sub_key])
|
||||||
|
|
||||||
|
def get_general(self, value):
|
||||||
|
try:
|
||||||
|
return self.general_config[value]
|
||||||
|
except KeyError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_engine(self, value):
|
||||||
|
try:
|
||||||
|
return self.engine_config[value]
|
||||||
|
except KeyError:
|
||||||
|
return None
|
||||||
@@ -78,18 +78,13 @@ class MangaOcr:
|
|||||||
key = 'm'
|
key = 'm'
|
||||||
available = False
|
available = False
|
||||||
|
|
||||||
def __init__(self, config={'pretrained_model_name_or_path':'kha-white/manga-ocr-base','force_cpu':'False'}, pretrained_model_name_or_path='', force_cpu=False):
|
def __init__(self, config={'pretrained_model_name_or_path':'kha-white/manga-ocr-base','force_cpu': False}):
|
||||||
if 'manga_ocr' not in sys.modules:
|
if 'manga_ocr' not in sys.modules:
|
||||||
logger.warning('manga-ocr not available, Manga OCR will not work!')
|
logger.warning('manga-ocr not available, Manga OCR will not work!')
|
||||||
else:
|
else:
|
||||||
if pretrained_model_name_or_path == '':
|
|
||||||
pretrained_model_name_or_path = config['pretrained_model_name_or_path']
|
|
||||||
if config['force_cpu'] == 'True':
|
|
||||||
force_cpu = True
|
|
||||||
|
|
||||||
logger.disable('manga_ocr')
|
logger.disable('manga_ocr')
|
||||||
logger.info(f'Loading Manga OCR model')
|
logger.info(f'Loading Manga OCR model')
|
||||||
self.model = MOCR(pretrained_model_name_or_path, force_cpu)
|
self.model = MOCR(config['pretrained_model_name_or_path'], config['force_cpu'])
|
||||||
self.available = True
|
self.available = True
|
||||||
logger.info('Manga OCR ready')
|
logger.info('Manga OCR ready')
|
||||||
|
|
||||||
|
|||||||
64
owocr/run.py
64
owocr/run.py
@@ -1,8 +1,6 @@
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import os
|
|
||||||
import configparser
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import fire
|
import fire
|
||||||
@@ -21,6 +19,7 @@ from notifypy import Notify
|
|||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
from owocr import *
|
from owocr import *
|
||||||
|
from owocr.config import Config
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import win32gui
|
import win32gui
|
||||||
@@ -31,6 +30,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
config = None
|
||||||
|
|
||||||
class WindowsClipboardThread(threading.Thread):
|
class WindowsClipboardThread(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -197,6 +197,11 @@ def get_path_key(path):
|
|||||||
return path, path.lstat().st_mtime
|
return path, path.lstat().st_mtime
|
||||||
|
|
||||||
|
|
||||||
|
def init_config():
|
||||||
|
global config
|
||||||
|
config = Config()
|
||||||
|
|
||||||
|
|
||||||
def run(read_from='clipboard',
|
def run(read_from='clipboard',
|
||||||
write_to='clipboard',
|
write_to='clipboard',
|
||||||
engine='',
|
engine='',
|
||||||
@@ -205,6 +210,8 @@ def run(read_from='clipboard',
|
|||||||
delete_images=False
|
delete_images=False
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
|
Japanese OCR client
|
||||||
|
|
||||||
Run OCR in the background, waiting for new images to appear either in system clipboard or a directory, or to be sent via a websocket.
|
Run OCR in the background, waiting for new images to appear either in system clipboard or a directory, or to be sent via a websocket.
|
||||||
Recognized texts can be either saved to system clipboard, appended to a text file or sent via a websocket.
|
Recognized texts can be either saved to system clipboard, appended to a text file or sent via a websocket.
|
||||||
|
|
||||||
@@ -227,56 +234,39 @@ def run(read_from='clipboard',
|
|||||||
websocket_port = 7331
|
websocket_port = 7331
|
||||||
notifications = False
|
notifications = False
|
||||||
|
|
||||||
config_file = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini')
|
if config.has_config:
|
||||||
config = configparser.ConfigParser()
|
if config.get_general('engines'):
|
||||||
res = config.read(config_file)
|
for config_engine in config.get_general('engines').split(','):
|
||||||
|
config_engines.append(config_engine.lower())
|
||||||
|
|
||||||
if len(res) != 0:
|
if config.get_general('logger_format'):
|
||||||
try:
|
logger_format = config.get_general('logger_format')
|
||||||
for config_engine in config['general']['engines'].split(','):
|
|
||||||
config_engines.append(config_engine.strip().lower())
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
if config.get_general('engine_color'):
|
||||||
logger_format = config['general']['logger_format'].strip()
|
engine_color = config.get_general('engine_color')
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
if config.get_general('delay_secs'):
|
||||||
engine_color = config['general']['engine_color'].strip()
|
delay_secs = config.get_general('delay_secs')
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
if config.get_general('websocket_port'):
|
||||||
delay_secs = float(config['general']['delay_secs'].strip())
|
websocket_port = config.get_general('websocket_port')
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
if config.get_general('notifications'):
|
||||||
websocket_port = int(config['general']['websocket_port'].strip())
|
notifications = config.get_general('notifications')
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
if config['general']['notifications'].strip().lower() == 'true':
|
|
||||||
notifications = True
|
|
||||||
except KeyError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
logger.configure(handlers=[{'sink': sys.stderr, 'format': logger_format}])
|
logger.configure(handlers=[{'sink': sys.stderr, 'format': logger_format}])
|
||||||
|
|
||||||
if len(res) != 0:
|
if config.has_config:
|
||||||
logger.info('Parsed config file')
|
logger.info('Parsed config file')
|
||||||
else:
|
else:
|
||||||
logger.warning('No config file, defaults will be used')
|
logger.warning('No config file, defaults will be used')
|
||||||
|
|
||||||
for _,engine_class in sorted(inspect.getmembers(sys.modules[__name__], lambda x: hasattr(x, '__module__') and __package__ + '.ocr' in x.__module__ and inspect.isclass(x))):
|
for _,engine_class in sorted(inspect.getmembers(sys.modules[__name__], lambda x: hasattr(x, '__module__') and __package__ + '.ocr' in x.__module__ and inspect.isclass(x))):
|
||||||
if len(config_engines) == 0 or engine_class.name in config_engines:
|
if len(config_engines) == 0 or engine_class.name in config_engines:
|
||||||
try:
|
if config.get_engine(engine_class.name) == None:
|
||||||
engine_instance = engine_class(config[engine_class.name])
|
|
||||||
except KeyError:
|
|
||||||
engine_instance = engine_class()
|
engine_instance = engine_class()
|
||||||
|
else:
|
||||||
|
engine_instance = engine_class(config.get_engine(engine_class.name))
|
||||||
|
|
||||||
if engine_instance.available:
|
if engine_instance.available:
|
||||||
engine_instances.append(engine_instance)
|
engine_instances.append(engine_instance)
|
||||||
|
|||||||
@@ -1,10 +1,16 @@
|
|||||||
[general]
|
[general]
|
||||||
;engines = avision,glens,gvision,azure,mangaocr,winrtocr,easyocr,paddleocr
|
;engines = avision,glens,gvision,azure,mangaocr,winrtocr,easyocr,paddleocr
|
||||||
|
;engine = glens
|
||||||
|
;read_from = clipboard
|
||||||
|
;write_to = clipboard
|
||||||
|
;pause_at_startup = False
|
||||||
;logger_format = <green>{time:HH:mm:ss.SSS}</green> | <level>{message}</level>
|
;logger_format = <green>{time:HH:mm:ss.SSS}</green> | <level>{message}</level>
|
||||||
;engine_color = cyan
|
;engine_color = cyan
|
||||||
;websocket_port = 7331
|
;websocket_port = 7331
|
||||||
;delay_secs = 0.5
|
;delay_secs = 0.5
|
||||||
;notifications = False
|
;notifications = False
|
||||||
|
;ignore_flag = False
|
||||||
|
;delete_images = False
|
||||||
[winrtocr]
|
[winrtocr]
|
||||||
;url = http://aaa.xxx.yyy.zzz:8000
|
;url = http://aaa.xxx.yyy.zzz:8000
|
||||||
[azure]
|
[azure]
|
||||||
|
|||||||
Reference in New Issue
Block a user