Spaces:
Runtime error
Runtime error
napatswift
commited on
Commit
·
b01f517
1
Parent(s):
b7f49b8
Update app
Browse files
main.py
CHANGED
|
@@ -10,6 +10,8 @@ device = 'gpu' if torch.cuda.is_available() else 'cpu'
|
|
| 10 |
|
| 11 |
table_det = init_detector('model/table-det/config.py',
|
| 12 |
'model/table-det/model.pth', device=device)
|
|
|
|
|
|
|
| 13 |
def get_corners(points):
|
| 14 |
"""
|
| 15 |
Returns the top-left, top-right, bottom-right, and bottom-left corners
|
|
@@ -17,39 +19,84 @@ def get_corners(points):
|
|
| 17 |
"""
|
| 18 |
# Sort points by x-coordinate
|
| 19 |
sorted_points = sorted(points, key=lambda p: p[0])
|
| 20 |
-
|
| 21 |
# Split sorted points into left and right halves
|
| 22 |
left_points = sorted_points[:2]
|
| 23 |
right_points = sorted_points[2:]
|
| 24 |
-
|
| 25 |
# Sort left and right points by y-coordinate
|
| 26 |
left_points = sorted(left_points, key=lambda p: p[1])
|
| 27 |
right_points = sorted(right_points, key=lambda p: p[1], reverse=True)
|
| 28 |
-
|
| 29 |
# Return corners in order: top-left, top-right, bottom-right, bottom-left
|
| 30 |
return (left_points[0], right_points[0], right_points[1], left_points[1])
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def predict(image_input):
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def run():
|
| 55 |
demo = gr.Interface(
|
|
|
|
| 10 |
|
| 11 |
table_det = init_detector('model/table-det/config.py',
|
| 12 |
'model/table-det/model.pth', device=device)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
def get_corners(points):
|
| 16 |
"""
|
| 17 |
Returns the top-left, top-right, bottom-right, and bottom-left corners
|
|
|
|
| 19 |
"""
|
| 20 |
# Sort points by x-coordinate
|
| 21 |
sorted_points = sorted(points, key=lambda p: p[0])
|
| 22 |
+
|
| 23 |
# Split sorted points into left and right halves
|
| 24 |
left_points = sorted_points[:2]
|
| 25 |
right_points = sorted_points[2:]
|
| 26 |
+
|
| 27 |
# Sort left and right points by y-coordinate
|
| 28 |
left_points = sorted(left_points, key=lambda p: p[1])
|
| 29 |
right_points = sorted(right_points, key=lambda p: p[1], reverse=True)
|
| 30 |
+
|
| 31 |
# Return corners in order: top-left, top-right, bottom-right, bottom-left
|
| 32 |
return (left_points[0], right_points[0], right_points[1], left_points[1])
|
| 33 |
|
| 34 |
+
|
| 35 |
+
def get_bbox(mask_array):
|
| 36 |
+
"""
|
| 37 |
+
Gets the bounding boxes of tables in a mask array.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
mask_array (numpy.ndarray): The mask array to be processed.
|
| 41 |
+
|
| 42 |
+
Returns:
|
| 43 |
+
list[tuple(int, int, int, int)]: A list of bounding boxes, where each bounding box is a tuple of (top left x, top left y, bottom right x, bottom right y).
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
# Find the contours in the mask array.
|
| 47 |
+
contours, hierarchy = cv2.findContours(
|
| 48 |
+
mask_array, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 49 |
+
|
| 50 |
+
# For each contour, get the bounding box.
|
| 51 |
+
table_bboxes = []
|
| 52 |
+
for cnt in contours:
|
| 53 |
+
|
| 54 |
+
# Get the minimum area rectangle that encloses the contour.
|
| 55 |
+
rect = cv2.minAreaRect(cnt)
|
| 56 |
+
|
| 57 |
+
# Get the corners of the minimum area rectangle.
|
| 58 |
+
box = cv2.boxPoints(rect)
|
| 59 |
+
|
| 60 |
+
# Get the epsilon value, which is used to approximate the contour.
|
| 61 |
+
epsilon = cv2.arcLength(cnt, True)
|
| 62 |
+
|
| 63 |
+
# Approximate the contour using the epsilon value.
|
| 64 |
+
approx = cv2.approxPolyDP(cnt, 0.02 * epsilon, True)
|
| 65 |
+
|
| 66 |
+
# Get the points of the approximated contour.
|
| 67 |
+
points = np.squeeze(approx)
|
| 68 |
+
|
| 69 |
+
# If the number of points is not 4, then use the points of the minimum area rectangle.
|
| 70 |
+
if len(points) != 4:
|
| 71 |
+
points = box
|
| 72 |
+
|
| 73 |
+
# Get the top left, bottom right, bottom left, and top right corners of the bounding box.
|
| 74 |
+
tl, br, bl, tr = get_corners(points.tolist())
|
| 75 |
+
|
| 76 |
+
# Add the bounding box to the list of bounding boxes.
|
| 77 |
+
table_bboxes.append([tl, tr, br, bl])
|
| 78 |
+
|
| 79 |
+
# Return the list of bounding boxes.
|
| 80 |
+
return table_bboxes
|
| 81 |
+
|
| 82 |
+
|
| 83 |
def predict(image_input):
|
| 84 |
+
# Inference the tables in the image.
|
| 85 |
+
result = inference_detector(table_det, image_input)
|
| 86 |
+
|
| 87 |
+
# Create a list to store the bounding boxes.
|
| 88 |
+
bbox_list = []
|
| 89 |
+
|
| 90 |
+
# Get the masks of the tables.
|
| 91 |
+
mask_images = result.pred_instances.masks.cpu().numpy()
|
| 92 |
+
|
| 93 |
+
# For each mask, get the bounding box.
|
| 94 |
+
for mask in mask_images:
|
| 95 |
+
bbox_list.extend(get_bbox(mask.astype(np.uint8)))
|
| 96 |
+
|
| 97 |
+
# Return the bounding boxes.
|
| 98 |
+
return bbox_list
|
| 99 |
+
|
| 100 |
|
| 101 |
def run():
|
| 102 |
demo = gr.Interface(
|