Skip to content

Commit

Permalink
Add ability to customize tesseract parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jonchang committed Dec 3, 2024
1 parent 8443c2a commit 270507c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion OCR/ocr/services/tesseract_ocr.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import os

import tesserocr
from tesserocr import PSM
import numpy as np
from PIL import Image


class TesseractOCR:
def __init__(self, psm=PSM.AUTO, variables=dict()):
"""
Initialize the tesseract OCR model.
`psm` (int): an enum (from `PSM`) that defines tesseract's page segmentation mode. Default is `AUTO`.
`variables` (dict): a dict to customize tesseract's behavior with internal variables
"""
self.psm = psm
self.variables = variables

def _set_variables(self, api):
"""
Set custom
"""
for name, value in self.variables.items():
api.SetVariable(name, value)

@staticmethod
def _guess_tessdata_path(wanted_lang="eng") -> bytes:
"""
Expand Down Expand Up @@ -52,7 +70,8 @@ def _guess_tessdata_path(wanted_lang="eng") -> bytes:

def image_to_text(self, segments: dict[str, np.ndarray]) -> dict[str, tuple[str, float]]:
digitized: dict[str, tuple[str, float]] = {}
with tesserocr.PyTessBaseAPI(path=self._guess_tessdata_path()) as api:
with tesserocr.PyTessBaseAPI(psm=self.psm, path=self._guess_tessdata_path()) as api:
self._set_variables(api)
for label, image in segments.items():
if image is None:
continue
Expand Down

0 comments on commit 270507c

Please sign in to comment.