From 5efea0f22f731059ee13d1b1070465c35dce28db Mon Sep 17 00:00:00 2001 From: AuroraWright Date: Tue, 22 Apr 2025 16:30:47 -0700 Subject: [PATCH] Fix macos clipboard race condition --- owocr/run.py | 22 ++++++++++++---------- owocr_config.ini | 6 +++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/owocr/run.py b/owocr/run.py index ab2709f..057c814 100644 --- a/owocr/run.py +++ b/owocr/run.py @@ -16,8 +16,7 @@ import asyncio import websockets import socketserver -from PIL import Image -from PIL import UnidentifiedImageError +from PIL import Image, UnidentifiedImageError from loguru import logger from pynput import keyboard from desktop_notifier import DesktopNotifierSync @@ -882,14 +881,17 @@ def run(): with objc.autorelease_pool(): old_count = count count = pasteboard.changeCount() - if not just_unpaused and count != old_count and NSPasteboardTypeTIFF in pasteboard.types(): - clipboard_text = '' - if NSPasteboardTypeString in pasteboard.types(): - clipboard_text = pasteboard.stringForType_(NSPasteboardTypeString) - if ignore_flag or clipboard_text != '*ocr_ignore*': - img = normalize_macos_clipboard(pasteboard.dataForType_(NSPasteboardTypeTIFF)) - img = Image.open(io.BytesIO(img)) - process_clipboard = True + if not just_unpaused and count != old_count: + while len(pasteboard.types()) == 0: + time.sleep(0.1) + if NSPasteboardTypeTIFF in pasteboard.types(): + clipboard_text = '' + if NSPasteboardTypeString in pasteboard.types(): + clipboard_text = pasteboard.stringForType_(NSPasteboardTypeString) + if ignore_flag or clipboard_text != '*ocr_ignore*': + img = normalize_macos_clipboard(pasteboard.dataForType_(NSPasteboardTypeTIFF)) + img = Image.open(io.BytesIO(img)) + process_clipboard = True else: if not paused: old_img = img diff --git a/owocr_config.ini b/owocr_config.ini index 93e6900..771842b 100644 --- a/owocr_config.ini +++ b/owocr_config.ini @@ -35,9 +35,9 @@ ;api_key = api_key_here ;endpoint = https://YOURPROJECT.cognitiveservices.azure.com/ [mangaocr] -pretrained_model_name_or_path = kha-white/manga-ocr-base -force_cpu = False +;pretrained_model_name_or_path = kha-white/manga-ocr-base +;force_cpu = False [easyocr] -gpu = True +;gpu = True [ocrspace] ;api_key = api_key_here \ No newline at end of file