Skip to content

Commit cb5ed55

Browse files
committed
Fix OCR validation
1 parent 0e6a682 commit cb5ed55

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

ruff.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# The source skeleton for this configuration can be found at
55
# https://github.com/BesLogic/shared-configs/blob/main/ruff.toml
66
# Modifications to this file that are not project-specific should also be done upstream.
7-
# These configs are incompatible with ruff<0.5.7
87

98
# https://docs.astral.sh/ruff/configuration/
109
target-version = "py311" # Change this to the oldest supported version by your application
1110
line-length = 100
1211
preview = true
12+
required-version = ">=0.9.0" # Removed ISC compatibility warning
1313

1414
[format]
1515
docstring-code-format = true

src/AutoSplitImage.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,26 @@ def __parse_text_file(self, path: str):
135135

136136
with open(path, mode="rb") as f:
137137
data = tomllib.load(f)
138+
try:
139+
self.texts = [text.lower().strip() for text in data["texts"]]
140+
self.__rect = (data["left"], data["right"], data["top"], data["bottom"])
141+
except KeyError as exception:
142+
error_messages.ocr_missing_key(path, exception.args[0])
143+
return
138144

139-
self.texts = [text.lower().strip() for text in data["texts"]]
140-
self.__rect = (data["left"], data["right"], data["top"], data["bottom"])
141145
self.__ocr_comparison_methods = data.get("methods", [0])
142146
self.__fps_limit = data.get("fps_limit", 0)
143147

144-
if self.__validate_ocr():
148+
if not self.__validate_ocr():
145149
error_messages.wrong_ocr_values(path)
146150
return
147151

148152
def __validate_ocr(self):
149153
values = [*self.__rect, *self.__ocr_comparison_methods, self.__fps_limit]
150154
return (
151155
all(value >= 0 for value in values) # Check for invalid negative values
152-
and self.__rect[1] > self.__rect[0]
153-
and self.__rect[3] > self.__rect[2]
156+
and self.__rect[0] < self.__rect[1]
157+
and self.__rect[2] < self.__rect[3]
154158
)
155159

156160
def __read_image_bytes(self, path: str):

src/error_messages.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,13 @@ def tesseract_missing(ocr_split_file_path: str):
267267
)
268268

269269

270+
def ocr_missing_key(ocr_split_file_path: str, missing_key: str):
271+
set_text_message(f"{ocr_split_file_path!r} is missing an entry for {missing_key!r}")
272+
273+
270274
def wrong_ocr_values(ocr_split_file_path: str):
271275
set_text_message(
272276
f"{ocr_split_file_path!r} has invalid values."
273-
+ "\nPlease make sure that `left < right` and `top < bottom`. "
277+
+ "\nPlease make sure that `left &lt; right` and `top &lt; bottom`. "
274278
+ "Also check for negative values in the 'methods' or 'fps_limit' settings"
275279
)

0 commit comments

Comments
 (0)