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)
|
- 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
|
- 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
|
- 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
|
# Acknowledgments
|
||||||
|
|
||||||
|
|||||||
18
owocr/run.py
18
owocr/run.py
@@ -17,6 +17,7 @@ from PIL import Image
|
|||||||
from PIL import UnidentifiedImageError
|
from PIL import UnidentifiedImageError
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from pynput import keyboard
|
from pynput import keyboard
|
||||||
|
from desktop_notifier import DesktopNotifier
|
||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
from owocr import *
|
from owocr import *
|
||||||
@@ -77,12 +78,14 @@ def are_images_identical(img1, img2):
|
|||||||
return (img1.shape == img2.shape) and (img1 == img2).all()
|
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()
|
t0 = time.time()
|
||||||
text = engine_instance(img_or_path)
|
text = engine_instance(img_or_path)
|
||||||
t1 = time.time()
|
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}")
|
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':
|
if write_to == 'websocket':
|
||||||
websocket_server_thread.send_text(text)
|
websocket_server_thread.send_text(text)
|
||||||
@@ -182,6 +185,7 @@ def run(read_from='clipboard',
|
|||||||
engine_color = 'cyan'
|
engine_color = 'cyan'
|
||||||
delay_secs = 0.5
|
delay_secs = 0.5
|
||||||
websocket_port = 7331
|
websocket_port = 7331
|
||||||
|
notifier = None
|
||||||
|
|
||||||
config_file = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini')
|
config_file = os.path.join(os.path.expanduser('~'),'.config','owocr_config.ini')
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@@ -214,6 +218,12 @@ def run(read_from='clipboard',
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
if config['general']['notifications'].strip() == 'True':
|
||||||
|
notifier = DesktopNotifier()
|
||||||
|
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 len(res) != 0:
|
||||||
@@ -337,7 +347,7 @@ def run(read_from='clipboard',
|
|||||||
else:
|
else:
|
||||||
if not paused and not tmp_paused:
|
if not paused and not tmp_paused:
|
||||||
img = Image.open(io.BytesIO(item))
|
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':
|
elif read_from == 'clipboard':
|
||||||
if not paused and not tmp_paused:
|
if not paused and not tmp_paused:
|
||||||
if mac_clipboard_polling:
|
if mac_clipboard_polling:
|
||||||
@@ -363,7 +373,7 @@ def run(read_from='clipboard',
|
|||||||
logger.warning('Error while reading from clipboard ({})'.format(error))
|
logger.warning('Error while reading from clipboard ({})'.format(error))
|
||||||
else:
|
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):
|
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:
|
if just_unpaused:
|
||||||
just_unpaused = False
|
just_unpaused = False
|
||||||
@@ -383,7 +393,7 @@ def run(read_from='clipboard',
|
|||||||
except (UnidentifiedImageError, OSError) as e:
|
except (UnidentifiedImageError, OSError) as e:
|
||||||
logger.warning(f'Error while reading file {path}: {e}')
|
logger.warning(f'Error while reading file {path}: {e}')
|
||||||
else:
|
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()
|
img.close()
|
||||||
if delete_images:
|
if delete_images:
|
||||||
Path.unlink(path)
|
Path.unlink(path)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
;engine_color = cyan
|
;engine_color = cyan
|
||||||
;websocket_port = 7331
|
;websocket_port = 7331
|
||||||
;delay_secs = 0.5
|
;delay_secs = 0.5
|
||||||
|
;notifications = False
|
||||||
[winrtocr]
|
[winrtocr]
|
||||||
;url = http://aaa.xxx.yyy.zzz:8000
|
;url = http://aaa.xxx.yyy.zzz:8000
|
||||||
[azure]
|
[azure]
|
||||||
|
|||||||
@@ -7,3 +7,4 @@ pyperclip
|
|||||||
unidic_lite
|
unidic_lite
|
||||||
pynput
|
pynput
|
||||||
websockets
|
websockets
|
||||||
|
desktop-notifier
|
||||||
Reference in New Issue
Block a user