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

Added "Auto" Mode to the Stitch OCR Detections Block #990

Merged
merged 3 commits into from
Jan 31, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@

* **"vertical_bottom_to_top"**: Vertical reading from bottom to top ⬆️

* **"auto"**: Automatically detects the reading direction based on the spatial arrangement of text elements.

#### Why Use This Transformation?

This is especially useful for:
Expand Down Expand Up @@ -109,6 +111,7 @@ class BlockManifest(WorkflowBlockManifest):
"right_to_left",
"vertical_top_to_bottom",
"vertical_bottom_to_top",
"auto",
] = Field(
title="Reading Direction",
description="The direction of the text in the image.",
Expand All @@ -131,6 +134,10 @@ class BlockManifest(WorkflowBlockManifest):
"name": "Bottom To Top (Vertical)",
"description": "Vertical reading from bottom to top",
},
"auto": {
"name": "Auto",
"description": "Automatically detect the reading direction based on text arrangement.",
},
}
},
)
Expand Down Expand Up @@ -167,6 +174,23 @@ def get_execution_engine_compatibility(cls) -> Optional[str]:
return ">=1.0.0,<2.0.0"


def detect_reading_direction(detections: sv.Detections) -> str:
if len(detections) == 0:
return "left_to_right"

xyxy = detections.xyxy
widths = xyxy[:, 2] - xyxy[:, 0]
heights = xyxy[:, 3] - xyxy[:, 1]

avg_width = np.mean(widths)
avg_height = np.mean(heights)

if avg_width > avg_height:
return "left_to_right"
else:
return "vertical_top_to_bottom"


class StitchOCRDetectionsBlockV1(WorkflowBlock):
@classmethod
def get_manifest(cls) -> Type[WorkflowBlockManifest]:
Expand All @@ -178,6 +202,8 @@ def run(
reading_direction: str,
tolerance: int,
) -> BlockResult:
if reading_direction == "auto":
reading_direction = detect_reading_direction(predictions[0])
return [
stitch_ocr_detections(
detections=detections,
Expand Down