Skip to content

Commit

Permalink
Refactor: Update setOcrModel method to accept additional data types
Browse files Browse the repository at this point in the history
The setOcrModel method in the TextDetector class has been modified to accept not only OcrModelIndex and str data types, but also int. This change allows for more flexibility when specifying the OCR model to be used. Additionally, the method now logs the selected OCR model and the corresponding model folder.
  • Loading branch information
royshil committed Oct 15, 2024
1 parent 1f7e437 commit c6131e5
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/tesseract.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ def __init__(self):
else:
self.setOcrModel(TextDetector.OcrModelIndex.DAKTRONICS)

def setOcrModel(self, ocrModelIndex: OcrModelIndex | str | None = None):
def setOcrModel(self, ocrModelIndex: OcrModelIndex | int | str | None = None):
ocr_model = None
external_folder = None
model_folder = resource_path("tesseract", "tessdata")
if ocrModelIndex == TextDetector.OcrModelIndex.DAKTRONICS:
ocr_model = "daktronics"
elif ocrModelIndex == TextDetector.OcrModelIndex.SCOREBOARD_GENERAL:
Expand All @@ -128,25 +128,22 @@ def setOcrModel(self, ocrModelIndex: OcrModelIndex | str | None = None):
# check the model file exists at the path
if path.exists(ocrModelIndex):
# Take the folder as the tessdata folder
external_folder = path.dirname(ocrModelIndex)
model_folder = path.dirname(ocrModelIndex)
# Take the model name without extension as the "language"
ocr_model = path.basename(ocrModelIndex)
ocr_model = path.splitext(ocr_model)[0]
elif ocr_model is None:

if ocr_model is None:
return

logger.info(f"Setting OCR model to {ocr_model}")
logger.info(f"Setting OCR model to {ocr_model} from {model_folder}")

with self.api_lock:
if self.api is not None:
self.api.End()
self.api = None
self.api = PyTessBaseAPI(
path=(
resource_path("tesseract", "tessdata")
if external_folder is None
else external_folder
),
path=model_folder,
lang=ocr_model,
)
# single word PSM
Expand Down

0 comments on commit c6131e5

Please sign in to comment.