Add OS notifications
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
18
owocr/run.py
18
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}</{engine_color}>: {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)
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -7,3 +7,4 @@ pyperclip
|
||||
unidic_lite
|
||||
pynput
|
||||
websockets
|
||||
desktop-notifier
|
||||
Reference in New Issue
Block a user