Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rich for progress tracking #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 81 additions & 10 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pypdfium2 = "^4.25.0"
opencv-python = "^4.9.0.80"
tabulate = "^0.9.0"
filetype = "^1.2.0"
rich = "^13.7.0"

[tool.poetry.group.dev.dependencies]
jupyter = "^1.0.0"
Expand Down
58 changes: 35 additions & 23 deletions surya/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from torch import nn
import numpy as np
from PIL import Image
from rich.progress import track
from surya.postprocessing.heatmap import get_and_clean_boxes
from surya.postprocessing.affinity import get_vertical_lines, get_horizontal_lines
from surya.model.processing import prepare_image, split_image
Expand All @@ -19,7 +20,9 @@ def batch_inference(images: List, model, processor):
split_index = []
split_heights = []
image_splits = []
for i, image in enumerate(images):
for i, image in track(
enumerate(images), total=len(images), description="Image preprocessing"
):
image_parts, split_height = split_image(image, processor)
image_splits.extend(image_parts)
split_index.extend([i] * len(image_parts))
Expand All @@ -28,8 +31,11 @@ def batch_inference(images: List, model, processor):
image_splits = [prepare_image(image, processor) for image in image_splits]

pred_parts = []
for i in range(0, len(image_splits), settings.DETECTOR_BATCH_SIZE):
batch = image_splits[i:i+settings.DETECTOR_BATCH_SIZE]
for i in track(
range(0, len(image_splits), settings.DETECTOR_BATCH_SIZE),
description="Batch inference",
):
batch = image_splits[i : i + settings.DETECTOR_BATCH_SIZE]
# Batch images in dim 0
batch = torch.stack(batch, dim=0)
batch = batch.to(model.dtype)
Expand All @@ -45,19 +51,27 @@ def batch_inference(images: List, model, processor):

heatmap_shape = list(heatmap.shape)
correct_shape = [processor.size["height"], processor.size["width"]]
cv2_size = list(reversed(correct_shape)) # opencv uses (width, height) instead of (height, width)
cv2_size = list(
reversed(correct_shape)
) # opencv uses (width, height) instead of (height, width)

if heatmap_shape != correct_shape:
heatmap = cv2.resize(heatmap, cv2_size, interpolation=cv2.INTER_LINEAR)

affinity_shape = list(affinity_map.shape)
if affinity_shape != correct_shape:
affinity_map = cv2.resize(affinity_map, cv2_size, interpolation=cv2.INTER_LINEAR)
affinity_map = cv2.resize(
affinity_map, cv2_size, interpolation=cv2.INTER_LINEAR
)

pred_parts.append((heatmap, affinity_map))

preds = []
for i, (idx, height) in enumerate(zip(split_index, split_heights)):
for i, (idx, height) in track(
enumerate(zip(split_index, split_heights)),
total=len(split_index),
description="Stitching predictions",
):
if len(preds) <= idx:
preds.append(pred_parts[i])
else:
Expand All @@ -76,7 +90,7 @@ def batch_inference(images: List, model, processor):

assert len(preds) == len(images)
results = []
for i in range(len(images)):
for i in track(range(len(images)), description="Generating results"):
heatmap, affinity_map = preds[i]
heat_img = Image.fromarray((heatmap * 255).astype(np.uint8))
aff_img = Image.fromarray((affinity_map * 255).astype(np.uint8))
Expand All @@ -86,21 +100,19 @@ def batch_inference(images: List, model, processor):
bboxes = get_and_clean_boxes(heatmap, heatmap_size, orig_sizes[i])
bbox_data = [bbox.model_dump() for bbox in bboxes]
vertical_lines = get_vertical_lines(affinity_map, affinity_size, orig_sizes[i])
horizontal_lines = get_horizontal_lines(affinity_map, affinity_size, orig_sizes[i])

results.append({
"bboxes": [bbd["bbox"] for bbd in bbox_data],
"polygons": [bbd["corners"] for bbd in bbox_data],
"vertical_lines": vertical_lines,
"horizontal_lines": horizontal_lines,
"heatmap": heat_img,
"affinity_map": aff_img,
})
horizontal_lines = get_horizontal_lines(
affinity_map, affinity_size, orig_sizes[i]
)

results.append(
{
"bboxes": [bbd["bbox"] for bbd in bbox_data],
"polygons": [bbd["corners"] for bbd in bbox_data],
"vertical_lines": vertical_lines,
"horizontal_lines": horizontal_lines,
"heatmap": heat_img,
"affinity_map": aff_img,
}
)

return results






Loading