Skip to content

Commit

Permalink
Make use of the default binarization method for extra processing
Browse files Browse the repository at this point in the history
Or at the very least remove color information which is not important
for text detection.
  • Loading branch information
pevogam committed Apr 29, 2024
1 parent b27f315 commit 79e1936
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions guibot/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,6 +1868,7 @@ def __configure_backend(self, backend=None, category="text", reset=False):
# 13 different page segmentation modes - see Tesseract API
self.params[category]["psmode"] = CVParameter(3, 0, 13, enumerated=True)
self.params[category]["extra_configs"] = CVParameter("")
self.params[category]["binarize_detection"] = CVParameter(False)
self.params[category]["recursion_height"] = CVParameter(0.3, 0.0, 1.0, 0.01)
self.params[category]["recursion_width"] = CVParameter(0.3, 0.0, 1.0, 0.01)
elif backend == "east":
Expand Down Expand Up @@ -2280,13 +2281,22 @@ def binarize_step(threshold, text_img):
def _detect_text_boxes(self, haystack):
import cv2
import numpy
char_canvas = numpy.array(haystack.pil_image)

detection_img = numpy.array(haystack.pil_image)
if self.params["tdetect"]["binarize_detection"].value:
detection_img = self._binarize_image(detection_img)
else:
detection_img = cv2.cvtColor(detection_img, cv2.COLOR_RGB2GRAY)
detection_width = int(self.params["tdetect"]["recursion_width"].value * haystack.width)
detection_height = int(self.params["tdetect"]["recursion_height"].value * haystack.height)

char_canvas = detection_img
text_canvas = numpy.array(haystack.pil_image)
self.imglog.hotmaps.append(char_canvas)
self.imglog.hotmaps.append(text_canvas)

text_regions = []
recursive_regions = [(0, 0, numpy.array(haystack.pil_image))]
recursive_regions = [(0, 0, detection_img)]
while len(recursive_regions) > 0:
offset_x, offset_y, next_region = recursive_regions.pop()
region_w, region_h = next_region.shape[1], next_region.shape[0]
Expand All @@ -2309,9 +2319,7 @@ def _detect_text_boxes(self, haystack):
if text == "":
logging.debug("Empty text found, skipping region")
continue
recursion_width = self.params["tdetect"]["recursion_width"].value * haystack.width
recursion_height = self.params["tdetect"]["recursion_height"].value * haystack.height
if (w > recursion_width and h > 0) or (h > recursion_height and w > 0):
if (w > detection_width and h > 0) or (h > detection_height and w > 0):
subregion_npy = next_region[max(dy, 0):min(dy+h, region_h),
max(dx, 0):min(dx+w, region_w)]
if next_region.shape != subregion_npy.shape:
Expand Down

0 comments on commit 79e1936

Please sign in to comment.