From 02cc0cef84e3f36e8313750d0c214c45dca80fca Mon Sep 17 00:00:00 2001 From: suke <32221726+wangsrGit119@users.noreply.github.com> Date: Sun, 15 Dec 2024 11:52:58 +0800 Subject: [PATCH] feat: Add OCR fallback when MLM is unavailable for image processing feat: Add OCR fallback when MLM is unavailable for image processing - Add OCR text extraction using easyocr when MLM client/model is not configured - Support both Chinese and English text recognition - Add OCR results under "OCR Text" section in markdown output - Only execute OCR as fallback when MLM description is not available --- src/markitdown/_markitdown.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/markitdown/_markitdown.py b/src/markitdown/_markitdown.py index 96997cf..332b2d1 100644 --- a/src/markitdown/_markitdown.py +++ b/src/markitdown/_markitdown.py @@ -798,6 +798,20 @@ def convert(self, local_path, **kwargs) -> Union[None, DocumentConverterResult]: ).strip() + "\n" ) + # add ocr only when MLM is not available + if mlm_client is None or mlm_model is None: + try: + import easyocr + reader = easyocr.Reader(['ch_sim','en']) # support chinese and english + ocr_result = reader.readtext(local_path) + if ocr_result: + md_content += "\n" + for detection in ocr_result: + text = detection[1] # extract text + md_content += f"- {text}\n" + except ImportError: + # easyocr not installed + pass return DocumentConverterResult( title=None,