From ea0878bef176a08db87300ee068e63b14135a2d5 Mon Sep 17 00:00:00 2001 From: AuroraWright Date: Fri, 26 Jan 2024 01:42:14 +0100 Subject: [PATCH] Implement reading from screen capture --- owocr/run.py | 36 +++++++++++++++++++++++++++++++++++- owocr_config.ini | 3 +++ requirements.txt | 5 ++++- setup.py | 4 +++- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/owocr/run.py b/owocr/run.py index de40d95..384dc21 100644 --- a/owocr/run.py +++ b/owocr/run.py @@ -6,6 +6,7 @@ from pathlib import Path import fire import numpy as np import pyperclipfix +import mss import asyncio import websockets import queue @@ -215,7 +216,7 @@ def run(read_from='clipboard', 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. - :param read_from: Specifies where to read input images from. Can be either "clipboard", "websocket", or a path to a directory. + :param read_from: Specifies where to read input images from. Can be either "clipboard", "websocket", "screencapture", or a path to a directory. :param write_to: Specifies where to save recognized texts to. Can be either "clipboard", "websocket", or a path to a text file. :param delay_secs: How often to check for new images, in seconds. :param engine: OCR engine to use. Available: "mangaocr", "glens", "gvision", "avision", "azure", "winrtocr", "easyocr", "paddleocr". @@ -333,6 +334,30 @@ def run(read_from='clipboard', windows_clipboard_polling = True else: generic_clipboard_polling = True + elif read_from == 'screencapture': + screen_capture_monitor = config.get_general('screen_capture_monitor') + screen_capture_delay_secs = config.get_general('screen_capture_delay_secs') + if not screen_capture_monitor: + screen_capture_monitor = 1 + if not screen_capture_delay_secs: + screen_capture_delay_secs = 3 + with mss.mss() as sct: + mon = sct.monitors + if len(mon) <= screen_capture_monitor: + msg = '"screen_capture_monitor" has to be a valid monitor number!' + raise ValueError(msg) + if config.get_general('screen_capture_coords') in (None, 'whole'): + coord_left = mon[screen_capture_monitor]["left"] + coord_top = mon[screen_capture_monitor]["top"] + coord_width = mon[screen_capture_monitor]["width"] + coord_height = mon[screen_capture_monitor]["height"] + else: + x, y, coord_width, coord_height = [int(c) for c in config.get_general('screen_capture_coords').split(',')] + coord_left = mon[screen_capture_monitor]["left"] + x + coord_top = mon[screen_capture_monitor]["top"] + y + sct_params = {'top': coord_top, 'left': coord_left, 'width': coord_width, 'height': coord_height, 'mon': screen_capture_monitor} + + logger.opt(ansi=True).info(f"Reading with screen capture using <{engine_color}>{engine_instances[engine_index].readable_name}{' (paused)' if paused else ''}") else: allowed_extensions = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp') read_from = Path(read_from) @@ -421,6 +446,15 @@ def run(read_from='clipboard', if not windows_clipboard_polling: time.sleep(delay_secs) + elif read_from == 'screencapture': + if not paused and not tmp_paused: + with mss.mss() as sct: + sct_img = sct.grab(sct_params) + img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX") + process_and_write_results(engine_instances[engine_index], engine_color, img, write_to, notifications) + time.sleep(screen_capture_delay_secs) + else: + time.sleep(delay_secs) else: for path in read_from.iterdir(): if path.suffix.lower() in allowed_extensions: diff --git a/owocr_config.ini b/owocr_config.ini index f6d4479..5203515 100644 --- a/owocr_config.ini +++ b/owocr_config.ini @@ -11,6 +11,9 @@ ;notifications = False ;ignore_flag = False ;delete_images = False +;screen_capture_monitor = 2 +;screen_capture_coords = 400,200,1500,600 +;screen_capture_delay_secs = 3 [winrtocr] ;url = http://aaa.xxx.yyy.zzz:8000 [azure] diff --git a/requirements.txt b/requirements.txt index 0448b3f..70ed981 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,7 @@ Pillow>=10.0.0 pyperclipfix pynput websockets -notify-py \ No newline at end of file +notify-py +mss +pywin32;platform_system=='Windows' +pyobjc;platform_system=='Darwin' \ No newline at end of file diff --git a/setup.py b/setup.py index e8de3ac..affaa86 100644 --- a/setup.py +++ b/setup.py @@ -27,7 +27,9 @@ setup( "pyperclipfix", "pynput", "websockets", - "notify-py" + "notify-py", + "pywin32;platform_system=='Windows'", + "pyobjc;platform_system=='Darwin'" ], entry_points={ "console_scripts": [