diff --git a/README.md b/README.md index 0885e17..a0c7059 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,8 @@ However: - you can switch OCR provider with its corresponding keyboard key (refer to the list above). You can also start the script paused with the -p option or with a specific provider with the -e option (refer to `owocr -h` for the list) - holding ctrl or cmd at any time will pause image processing temporarily - for systems where text can be copied to the clipboard at the same time as images, if `*ocr_ignore*` is copied with an image, the image will be ignored -- a config file (located in `user directory/.config/owocr_config.ini`) can be used to limit providers (to reduce clutter/memory usage) as well as specifying provider settings such as api keys etc (a sample config file is provided) +- optionally, notifications can be enabled in the config file to show the text with a native OS notification +- a config file (to be created in `user directory/.config/owocr_config.ini`, on Windows `user directory` is the `C:\Users\yourusername` folder) can be used to limit providers (to reduce clutter/memory usage) as well as specifying provider settings such as api keys etc. A sample config file is provided [here](https://raw.githubusercontent.com/AuroraWright/owocr/master/owocr_config.ini) # Acknowledgments diff --git a/owocr/run.py b/owocr/run.py index 5e37acd..4e30c84 100644 --- a/owocr/run.py +++ b/owocr/run.py @@ -17,6 +17,7 @@ from PIL import Image from PIL import UnidentifiedImageError from loguru import logger from pynput import keyboard +from desktop_notifier import DesktopNotifier import inspect from owocr import * @@ -77,12 +78,14 @@ def are_images_identical(img1, img2): return (img1.shape == img2.shape) and (img1 == img2).all() -def process_and_write_results(engine_instance, engine_color, img_or_path, write_to): +def process_and_write_results(engine_instance, engine_color, img_or_path, write_to, notifier): t0 = time.time() text = engine_instance(img_or_path) t1 = time.time() logger.opt(ansi=True).info(f"Text recognized in {t1 - t0:0.03f}s using <{engine_color}>{engine_instance.readable_name}: {text}") + if notifier != None: + notifier.send_sync(title="owocr", message=text, timeout=5) if write_to == 'websocket': websocket_server_thread.send_text(text) @@ -182,6 +185,7 @@ def run(read_from='clipboard', engine_color = 'cyan' delay_secs = 0.5 websocket_port = 7331 + notifier = None config_file = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini') config = configparser.ConfigParser() @@ -214,6 +218,12 @@ def run(read_from='clipboard', except KeyError: pass + try: + if config['general']['notifications'].strip() == 'True': + notifier = DesktopNotifier() + except KeyError: + pass + logger.configure(handlers=[{"sink": sys.stderr, "format": logger_format}]) if len(res) != 0: @@ -337,7 +347,7 @@ def run(read_from='clipboard', else: if not paused and not tmp_paused: img = Image.open(io.BytesIO(item)) - process_and_write_results(engine_instances[engine_index], engine_color, img, write_to) + process_and_write_results(engine_instances[engine_index], engine_color, img, write_to, notifier) elif read_from == 'clipboard': if not paused and not tmp_paused: if mac_clipboard_polling: @@ -363,7 +373,7 @@ def run(read_from='clipboard', logger.warning('Error while reading from clipboard ({})'.format(error)) else: if not just_unpaused and (ignore_flag or pyperclip.paste() != '*ocr_ignore*') and isinstance(img, Image.Image) and not are_images_identical(img, old_img): - process_and_write_results(engine_instances[engine_index], engine_color, img, write_to) + process_and_write_results(engine_instances[engine_index], engine_color, img, write_to, notifier) if just_unpaused: just_unpaused = False @@ -383,7 +393,7 @@ def run(read_from='clipboard', except (UnidentifiedImageError, OSError) as e: logger.warning(f'Error while reading file {path}: {e}') else: - process_and_write_results(engine_instances[engine_index], engine_color, img, write_to) + process_and_write_results(engine_instances[engine_index], engine_color, img, write_to, notifier) img.close() if delete_images: Path.unlink(path) diff --git a/owocr_config.ini b/owocr_config.ini index 4c1701f..0c596cc 100644 --- a/owocr_config.ini +++ b/owocr_config.ini @@ -4,6 +4,7 @@ ;engine_color = cyan ;websocket_port = 7331 ;delay_secs = 0.5 +;notifications = False [winrtocr] ;url = http://aaa.xxx.yyy.zzz:8000 [azure] diff --git a/requirements.txt b/requirements.txt index 35d6460..c049f41 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ Pillow>=10.0.0 pyperclip unidic_lite pynput -websockets \ No newline at end of file +websockets +desktop-notifier \ No newline at end of file diff --git a/setup.py b/setup.py index a12f447..39e140d 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,8 @@ setup( "pyperclip", "unidic_lite", "pynput", - "websockets" + "websockets", + "desktop-notifier" ], entry_points={ "console_scripts": [