-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Added support for rapidocr-onnxruntime #1502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bigbruno
wants to merge
2
commits into
ocrmypdf:main
Choose a base branch
from
bigbruno:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+661
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| """Interface to RapidOCR.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| try: | ||
| from rapidocr_onnxruntime import RapidOCR, __version__ | ||
|
|
||
| RAPIDOCR_AVAILABLE = True | ||
| except ImportError: | ||
| RAPIDOCR_AVAILABLE = False | ||
| __version__ = 'not installed' | ||
|
|
||
|
|
||
| class RapidOcrLoggerAdapter(logging.LoggerAdapter): | ||
| """Prepend [rapidocr] to messages emitted from RapidOCR.""" | ||
|
|
||
| def process(self, msg, kwargs): | ||
| kwargs['extra'] = self.extra | ||
| return f'[rapidocr] {msg}', kwargs | ||
|
|
||
|
|
||
| def version() -> str: | ||
| """Return RapidOCR version.""" | ||
| if RAPIDOCR_AVAILABLE: | ||
| return __version__ | ||
| return 'not installed' | ||
|
|
||
|
|
||
| def is_available() -> bool: | ||
| """Check if RapidOCR is available.""" | ||
| return RAPIDOCR_AVAILABLE | ||
|
|
||
|
|
||
| def get_languages() -> set[str]: | ||
| """Return list of supported languages. | ||
|
|
||
| RapidOCR doesn't work with ISO language codes like Tesseract. | ||
| This is a mapping of supported languages. | ||
| """ | ||
| if not RAPIDOCR_AVAILABLE: | ||
| return set() | ||
|
|
||
| # Basic language support by RapidOCR | ||
| return {'chi_sim', 'chi_tra', 'eng', 'fre', 'ger', 'kor', 'jpn'} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
| """Built-in plugin to implement OCR using RapidOCR.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import logging | ||
|
|
||
| from ocrmypdf import hookimpl | ||
| from ocrmypdf.exceptions import MissingDependencyError | ||
| from ocrmypdf.ocr_engine.rapidocr import ( | ||
| RAPIDOCR_AVAILABLE, | ||
| IMPORT_ERROR_MESSAGE, | ||
| RapidOcrEngine, | ||
| ) | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @hookimpl | ||
| def add_options(parser): | ||
| """Add RapidOCR specific command line arguments.""" | ||
| rapidocr_group = parser.add_argument_group("RapidOCR", "RapidOCR engine options") | ||
|
|
||
| # Check if --ocr-engine already exists | ||
| has_ocr_engine = False | ||
| for action in parser._actions: | ||
| if action.dest == 'ocr_engine': | ||
| has_ocr_engine = True | ||
| # Update choices if the option exists but doesn't include 'rapidocr' | ||
| if 'rapidocr' not in action.choices: | ||
| action.choices.append('rapidocr') | ||
| break | ||
|
|
||
| # Only add the argument if it doesn't exist yet | ||
| if not has_ocr_engine: | ||
| parser.add_argument( | ||
| '--ocr-engine', | ||
| choices=['tesseract', 'rapidocr'], | ||
| default='tesseract', | ||
| help="Choose OCR engine (default: tesseract)", | ||
| ) | ||
|
|
||
| rapidocr_group.add_argument( | ||
| '--rapidocr-use-angle-cls', | ||
| action=argparse.BooleanOptionalAction, | ||
| default=False, | ||
| help="Enable angle classification in RapidOCR to detect rotated text", | ||
| ) | ||
|
|
||
| rapidocr_group.add_argument( | ||
| '--rapidocr-lang', | ||
| choices=['ch', 'en', 'french', 'german', 'korean', 'japan'], | ||
| default='en', | ||
| help="Language for RapidOCR recognition (default: en)", | ||
| ) | ||
|
|
||
|
|
||
| @hookimpl | ||
| def check_options(options): | ||
| """Validate RapidOCR settings.""" | ||
| if hasattr(options, 'ocr_engine') and options.ocr_engine == 'rapidocr': | ||
| log.info("RapidOCR engine selected in options") | ||
|
|
||
| if not RAPIDOCR_AVAILABLE: | ||
| log.error("RapidOCR engine requested but not available") | ||
| error_msg = ( | ||
| "RapidOCR selected as OCR engine but rapidocr_onnxruntime could not be imported. " | ||
| f"Error details: {IMPORT_ERROR_MESSAGE}\n" | ||
| "Install it with: pip install rapidocr_onnxruntime" | ||
| ) | ||
| raise MissingDependencyError(error_msg) | ||
| else: | ||
| # Force the option to be set again to make sure it's effective | ||
| options.ocr_engine = 'rapidocr' | ||
| log.info("RapidOCR engine confirmed as available") | ||
|
|
||
|
|
||
| @hookimpl | ||
| def get_ocr_engine(): | ||
| """Return the OCR engine to use.""" | ||
| # Add verbose logging | ||
| if not RAPIDOCR_AVAILABLE: | ||
| log.warning("get_ocr_engine(): RapidOCR not available, returning None") | ||
| return None | ||
|
|
||
| log.info("get_ocr_engine(): Returning RapidOCR engine") | ||
| return RapidOcrEngine() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything in
ocrmypdf._execis for managing subprocesses we interact with. Since RapidOCR is a library and not a process, everything here should be moved to either builtin_plugins.rapidocr_engine or ocr_engine.rapidocr