From 1bee16cde59c9d1375344839f88f59c5d36c3f62 Mon Sep 17 00:00:00 2001 From: pk5ls20 Date: Fri, 5 Jan 2024 03:56:22 +0800 Subject: [PATCH] Keep the initial preprocessing and adjust the `EasyPaddleOCR` preprocessing to include only RGB conversion. --- app/Services/ocr_services.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/Services/ocr_services.py b/app/Services/ocr_services.py index 622c370..415776c 100644 --- a/app/Services/ocr_services.py +++ b/app/Services/ocr_services.py @@ -18,10 +18,11 @@ def __init__(self): def _image_preprocess(img: Image.Image) -> Image.Image: if img.mode != 'RGB': img = img.convert('RGB') - # Limit maximum size to 960*960 - if img.size[0] > 960 or img.size[1] > 960: - img.thumbnail((960, 960), Image.Resampling.LANCZOS) - return img + if img.size[0] > 1024 or img.size[1] > 1024: + img.thumbnail((1024, 1024), Image.Resampling.LANCZOS) + new_img = Image.new('RGB', (1024, 1024), (0, 0, 0)) + new_img.paste(img, ((1024 - img.size[0]) // 2, (1024 - img.size[1]) // 2)) + return new_img def ocr_interface(self, img: Image.Image, need_preprocess=True) -> str: pass @@ -37,6 +38,13 @@ def __init__(self): warmup_size=(960, 960)) logger.success("EasyPaddleOCR loaded successfully") + @staticmethod + def _image_preprocess(img: Image.Image) -> Image.Image: + # Optimized `easypaddleocr` doesn't require scaling preprocess + if img.mode != 'RGB': + img = img.convert('RGB') + return img + def _easy_paddleocr_process(self, img: Image.Image) -> str: _, ocr_result, _ = self._paddle_ocr_module.ocr(np.array(img)) if ocr_result: