Skip to content

Commit

Permalink
Refactor: Update MainWindow to set default box display style and load…
Browse files Browse the repository at this point in the history
… external OCR model

- Updated the `MainWindow` class in `mainwindow.py` to set the default box display style for the OCR model combo box.
- Added the ability to load a custom OCR model file in the `ocrModelChanged` method of `MainWindow`.
- Updated the `setOcrModel` method in `TextDetector` class of `tesseract.py` to handle loading external OCR models.
  • Loading branch information
royshil committed Oct 15, 2024
1 parent b4ede12 commit 1f7e437
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
35 changes: 29 additions & 6 deletions src/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,14 @@ def __init__(self, translator: QTranslator, parent: QObject):
"General Scoreboard",
"General Fonts (English)",
"General Scoreboard Large",
"Load External OCR Model",
]
self.ui.comboBox_ocrModel.addItems(ocr_models)
self.ui.comboBox_ocrModel.setCurrentIndex(
fetch_data("scoresight.json", "ocr_model", 1)
) # default to General Scoreboard
# default to General Scoreboard
ocr_model_from_storage = fetch_data("scoresight.json", "ocr_model", 1)
if type(ocr_model_from_storage) == str:
ocr_model_from_storage = 4
self.ui.comboBox_ocrModel.setCurrentIndex(ocr_model_from_storage)

self.ui.frame_source_view.setEnabled(False)
self.ui.groupBox_target_settings.setEnabled(False)
Expand Down Expand Up @@ -539,15 +542,35 @@ def detectionCadenceChanged(self, detections_per_second):
1000 / detections_per_second
)

def ocrModelChanged(self, index):
self.globalSettingsChanged("ocr_model", index)
def ocrModelChanged(self, index: int | str):
ocrModel = None
if index == 4:
# load a custom OCR model
file, _ = QFileDialog.getOpenFileName(
self, "Open OCR Model File", "", "OCR Model Files (*.traineddata)"
)
if not file:
return
# get absolute file path
file = path.abspath(file)
self.globalSettingsChanged("ocr_model", file)
ocrModel = file
elif type(index) == int:
self.globalSettingsChanged("ocr_model", index)
ocrModel = index
elif type(index) == str:
# check if the index is a valid existing file
if path.exists(index):
self.globalSettingsChanged("ocr_model", index)
ocrModel = index

# update the ocr model in the text detector
if (
self.image_viewer
and self.image_viewer.timerThread
and self.image_viewer.timerThread.textDetector
):
self.image_viewer.timerThread.textDetector.setOcrModel(index)
self.image_viewer.timerThread.textDetector.setOcrModel(ocrModel)

def openLogsDialog(self):
if self.log_dialog is None:
Expand Down
30 changes: 23 additions & 7 deletions src/tesseract.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TextDetectionTarget,
TextDetectionTargetWithResult,
)
from sc_logging import logger


def autocrop(image_in):
Expand Down Expand Up @@ -112,25 +113,40 @@ def __init__(self):
else:
self.setOcrModel(TextDetector.OcrModelIndex.DAKTRONICS)

def setOcrModel(self, ocrModelIndex):
def setOcrModel(self, ocrModelIndex: OcrModelIndex | str | None = None):
ocr_model = None
if ocrModelIndex == self.OcrModelIndex.DAKTRONICS:
external_folder = None
if ocrModelIndex == TextDetector.OcrModelIndex.DAKTRONICS:
ocr_model = "daktronics"
if ocrModelIndex == self.OcrModelIndex.SCOREBOARD_GENERAL:
elif ocrModelIndex == TextDetector.OcrModelIndex.SCOREBOARD_GENERAL:
ocr_model = "scoreboard_general"
if ocrModelIndex == self.OcrModelIndex.GENERAL_ENGLISH:
elif ocrModelIndex == TextDetector.OcrModelIndex.GENERAL_ENGLISH:
ocr_model = "eng"
if ocrModelIndex == self.OcrModelIndex.SCOREBOARD_GENERAL_LARGE:
elif ocrModelIndex == TextDetector.OcrModelIndex.SCOREBOARD_GENERAL_LARGE:
ocr_model = "scoreboard_general_large"
if ocr_model is None:
elif isinstance(ocrModelIndex, str):
# check the model file exists at the path
if path.exists(ocrModelIndex):
# Take the folder as the tessdata folder
external_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:
return

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

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

0 comments on commit 1f7e437

Please sign in to comment.