Skip to content

Commit

Permalink
pref: improve MD5HashModel performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pradishb committed Jun 11, 2024
1 parent 78d8797 commit 6b26ca5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions hash_ocr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ def __init__(
):
"""
:param connected_chars: set this to True if the characters of
the font can touch each other, but this will slightly affect
the performance
the font can touch each other, but this will affect the performance
of the OCR
"""
super().__init__(model_path, label_path)
self.chars = self.get_model_chars()
Expand All @@ -224,13 +224,16 @@ def compute_hash(self, img: MatLike):

def classify_connected_letters(self, img: MatLike) -> Tuple[float, str]:
"""
Classify the connected letters by splitting them into
small chunks and check the hash in partial_hash_map
Classify the connected letters by splitting them into small chunks
This method is slow
"""
if img.shape[0] * img.shape[1] == 0:
return float("inf"), ""

for w in reversed(range(self.min_w, self.max_w)):
min_w = self.min_w
# improves performance a lot
max_w = min(img.shape[1], self.max_w)
for w in reversed(range(min_w, max_w + 1)):
partial_img = img[:, :w]
cnts, _ = cv2.findContours(
partial_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
Expand All @@ -252,6 +255,8 @@ def classify_connected_letters(self, img: MatLike) -> Tuple[float, str]:
return float("inf"), ""

def classify_letter(self, img: MatLike) -> Tuple[float, str]:
if img.shape[1] < self.min_w:
return float("inf"), ""
hsh = self.compute_hash(img)
char = self.hash_map.get(hsh, "")
if not char and self.connected_chars:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "hash-ocr"
version = "2.2.2"
version = "2.2.3"
dependencies = ["opencv-python", "opencv-contrib-python"]
requires-python = ">=3"
authors = [{ name = "Pradish Bijukchhe", email = "[email protected]" }]
Expand Down

0 comments on commit 6b26ca5

Please sign in to comment.