Skip to content

Commit

Permalink
Merge branch 'main' into fix/platform_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz-roboflow authored Jan 31, 2025
2 parents 9bf5a0a + 59fc00d commit 79ee2c9
Showing 1 changed file with 26 additions and 0 deletions.
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

0 comments on commit 79ee2c9

Please sign in to comment.