Spaces:
Running
on
T4
Running
on
T4
Speed up crop_line function
Browse files- onnx_text_recognition.py +10 -11
onnx_text_recognition.py
CHANGED
@@ -44,18 +44,17 @@ class TextRecognition:
|
|
44 |
"""Crops predicted text line based on the polygon coordinates
|
45 |
and returns binarised text line image."""
|
46 |
poly = np.array([[int(lst[0]), int(lst[1])] for lst in polygon])
|
47 |
-
mask = np.zeros([height, width], dtype=np.uint8)
|
48 |
-
cv2.drawContours(mask, [poly], -1, (255, 255, 255), -1, cv2.LINE_AA)
|
49 |
-
rect = cv2.boundingRect(poly)
|
50 |
-
cropped = image[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]
|
51 |
-
|
52 |
-
mask_crop = mask[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]
|
53 |
-
res = cv2.bitwise_and(cropped, cropped, mask = mask_crop)
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
def crop_lines(self, polygons, image, height, width):
|
61 |
"""Returns a list of line images cropped following the detected polygon coordinates."""
|
|
|
44 |
"""Crops predicted text line based on the polygon coordinates
|
45 |
and returns binarised text line image."""
|
46 |
poly = np.array([[int(lst[0]), int(lst[1])] for lst in polygon])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
rect = cv2.boundingRect(polygon)
|
49 |
+
cropped_image = image[rect[1]: rect[1] + rect[3], rect[0]: rect[0] + rect[2]]
|
50 |
+
mask = np.zeros([rect[3], rect[2]], dtype=np.uint8)
|
51 |
+
cv2.drawContours(mask, [polygon- np.array([[rect[0],rect[1]]])], -1, (255, 255, 255), -1, cv2.LINE_AA)
|
52 |
+
res = cv2.bitwise_and(cropped_image, cropped_image, mask = mask)
|
53 |
+
wbg = np.ones_like(cropped_image, np.uint8)*255
|
54 |
+
cv2.bitwise_not(wbg,wbg, mask=mask)
|
55 |
+
# Overlap the resulted cropped image on the white background
|
56 |
+
dst = wbg+res
|
57 |
+
return dst
|
58 |
|
59 |
def crop_lines(self, polygons, image, height, width):
|
60 |
"""Returns a list of line images cropped following the detected polygon coordinates."""
|