Add support for Linux clipboard reading (#44)
* Allow clipboard reading on Linux using Pillow 10 * Update README.md to reflect Pillow changes * Update README.md with Linux clipboard requirements * Fix "Using CUDA;Using CPU" console output * Add verbose option to help with debugging * Restore wayland codepath for writing text to clipboard
This commit is contained in:
@@ -18,7 +18,7 @@ class MangaOcr:
|
||||
if not force_cpu and torch.cuda.is_available():
|
||||
logger.info('Using CUDA')
|
||||
self.model.cuda()
|
||||
if not force_cpu and torch.backends.mps.is_available():
|
||||
elif not force_cpu and torch.backends.mps.is_available():
|
||||
logger.info('Using MPS')
|
||||
self.model.to('mps')
|
||||
else:
|
||||
|
||||
@@ -48,7 +48,8 @@ def run(read_from='clipboard',
|
||||
write_to='clipboard',
|
||||
pretrained_model_name_or_path='kha-white/manga-ocr-base',
|
||||
force_cpu=False,
|
||||
delay_secs=0.1
|
||||
delay_secs=0.1,
|
||||
verbose=False
|
||||
):
|
||||
"""
|
||||
Run OCR in the background, waiting for new images to appear either in system clipboard, or a directory.
|
||||
@@ -58,6 +59,7 @@ def run(read_from='clipboard',
|
||||
:param write_to: Specifies where to save recognized texts to. Can be either "clipboard", or a path to a text file.
|
||||
:param pretrained_model_name_or_path: Path to a trained model, either local or from Transformers' model hub.
|
||||
:param force_cpu: If True, OCR will use CPU even if GPU is available.
|
||||
:param verbose: If True, unhides all warnings.
|
||||
:param delay_secs: How often to check for new images, in seconds.
|
||||
"""
|
||||
|
||||
@@ -76,12 +78,6 @@ def run(read_from='clipboard',
|
||||
raise NotImplementedError(msg)
|
||||
|
||||
if read_from == 'clipboard':
|
||||
|
||||
if sys.platform not in ('darwin', 'win32'):
|
||||
msg = 'Reading images from clipboard works only on macOS and Windows. ' \
|
||||
'On Linux, run "manga_ocr /path/to/screenshot/folder" to read images from a folder instead.'
|
||||
raise NotImplementedError(msg)
|
||||
|
||||
from PIL import ImageGrab
|
||||
logger.info('Reading from clipboard')
|
||||
|
||||
@@ -91,15 +87,21 @@ def run(read_from='clipboard',
|
||||
|
||||
try:
|
||||
img = ImageGrab.grabclipboard()
|
||||
except OSError:
|
||||
logger.warning('Error while reading from clipboard')
|
||||
except OSError as error:
|
||||
if not verbose and "cannot identify image file" in str(error):
|
||||
# Pillow error when clipboard hasn't changed since last grab (Linux)
|
||||
pass
|
||||
elif not verbose and "target image/png not available" in str(error):
|
||||
# Pillow error when clipboard contains text (Linux, X11)
|
||||
pass
|
||||
else:
|
||||
logger.warning('Error while reading from clipboard ({})'.format(error))
|
||||
else:
|
||||
if isinstance(img, Image.Image) and not are_images_identical(img, old_img):
|
||||
process_and_write_results(mocr, img, write_to)
|
||||
|
||||
time.sleep(delay_secs)
|
||||
|
||||
|
||||
else:
|
||||
read_from = Path(read_from)
|
||||
if not read_from.is_dir():
|
||||
|
||||
Reference in New Issue
Block a user