Skip to content

Commit

Permalink
Improve OCR preprocess procedure. Improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
hv0905 committed Jan 4, 2024
1 parent a230014 commit ad11c0d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions app/Services/ocr_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ def __init__(self):
def _image_preprocess(img: Image.Image) -> Image.Image:
if img.mode != 'RGB':
img = img.convert('RGB')
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
# 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

def ocr_interface(self, img: Image.Image, need_preprocess=True) -> str:
pass
Expand All @@ -32,7 +31,10 @@ class EasyPaddleOCRService(OCRService):
def __init__(self):
super().__init__()
from easypaddleocr import EasyPaddleOCR
self._paddle_ocr_module = EasyPaddleOCR(use_angle_cls=True, needWarmUp=True, devices=self._device)
self._paddle_ocr_module = EasyPaddleOCR(use_angle_cls=True,
needWarmUp=True,
devices=self._device,
warmup_size=(960, 960))
logger.success("EasyPaddleOCR loaded successfully")

def _easy_paddleocr_process(self, img: Image.Image) -> str:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pillow>9.3.0
numpy

# OCR - you can choose other option if necessary, or completely disable it if you don't need this feature
easypaddleocr
easypaddleocr>=0.1.2
# easyocr
# paddleocr

Expand Down

0 comments on commit ad11c0d

Please sign in to comment.