Spaces:
Runtime error
Runtime error
Update utility/utils.py
Browse files- utility/utils.py +15 -10
utility/utils.py
CHANGED
|
@@ -111,26 +111,31 @@ def ocr_with_paddle(img):
|
|
| 111 |
|
| 112 |
# Check if img is a file path or an image array
|
| 113 |
if isinstance(img, str):
|
| 114 |
-
# If img is a file path, load the image
|
| 115 |
img = cv2.imread(img)
|
| 116 |
-
|
| 117 |
-
# Perform OCR
|
| 118 |
result = ocr.ocr(img)
|
| 119 |
|
| 120 |
-
# Iterate through OCR
|
| 121 |
for line in result[0]:
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
final_text += ' ' + text
|
| 124 |
-
boxes.append(box)
|
| 125 |
|
| 126 |
-
#
|
| 127 |
points = [(int(point[0]), int(point[1])) for point in box]
|
| 128 |
cv2.polylines(img, [np.array(points)], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
img_with_boxes = img
|
| 132 |
|
| 133 |
-
return final_text,
|
| 134 |
|
| 135 |
# Function to draw bounding boxes around text
|
| 136 |
#def draw_boxes(image, boxes):
|
|
|
|
| 111 |
|
| 112 |
# Check if img is a file path or an image array
|
| 113 |
if isinstance(img, str):
|
|
|
|
| 114 |
img = cv2.imread(img)
|
| 115 |
+
|
| 116 |
+
# Perform OCR
|
| 117 |
result = ocr.ocr(img)
|
| 118 |
|
| 119 |
+
# Iterate through the OCR result
|
| 120 |
for line in result[0]:
|
| 121 |
+
# Check how many values are returned (2 or 3) and unpack accordingly
|
| 122 |
+
if len(line) == 3:
|
| 123 |
+
box, text, _ = line # When 3 values are returned
|
| 124 |
+
elif len(line) == 2:
|
| 125 |
+
box, text = line # When only 2 values are returned
|
| 126 |
+
|
| 127 |
+
# Store the recognized text and bounding boxes
|
| 128 |
final_text += ' ' + text
|
| 129 |
+
boxes.append(box)
|
| 130 |
|
| 131 |
+
# Draw the bounding box
|
| 132 |
points = [(int(point[0]), int(point[1])) for point in box]
|
| 133 |
cv2.polylines(img, [np.array(points)], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 134 |
|
| 135 |
+
# Store the image with bounding boxes in a variable
|
| 136 |
+
img_with_boxes = img
|
| 137 |
|
| 138 |
+
return final_text, img_with_boxes
|
| 139 |
|
| 140 |
# Function to draw bounding boxes around text
|
| 141 |
#def draw_boxes(image, boxes):
|