Skip to content

Commit 59fc00d

Browse files
Merge pull request #990 from roboflow/auto-ocr-stitch
Added "Auto" Mode to the Stitch OCR Detections Block
2 parents 33a4a0a + 4da3187 commit 59fc00d

File tree

1 file changed

+26
-0
lines changed
  • inference/core/workflows/core_steps/transformations/stitch_ocr_detections

1 file changed

+26
-0
lines changed

inference/core/workflows/core_steps/transformations/stitch_ocr_detections/v1.py

+26
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
5151
* **"vertical_bottom_to_top"**: Vertical reading from bottom to top ⬆️
5252
53+
* **"auto"**: Automatically detects the reading direction based on the spatial arrangement of text elements.
54+
5355
#### Why Use This Transformation?
5456
5557
This is especially useful for:
@@ -109,6 +111,7 @@ class BlockManifest(WorkflowBlockManifest):
109111
"right_to_left",
110112
"vertical_top_to_bottom",
111113
"vertical_bottom_to_top",
114+
"auto",
112115
] = Field(
113116
title="Reading Direction",
114117
description="The direction of the text in the image.",
@@ -131,6 +134,10 @@ class BlockManifest(WorkflowBlockManifest):
131134
"name": "Bottom To Top (Vertical)",
132135
"description": "Vertical reading from bottom to top",
133136
},
137+
"auto": {
138+
"name": "Auto",
139+
"description": "Automatically detect the reading direction based on text arrangement.",
140+
},
134141
}
135142
},
136143
)
@@ -167,6 +174,23 @@ def get_execution_engine_compatibility(cls) -> Optional[str]:
167174
return ">=1.0.0,<2.0.0"
168175

169176

177+
def detect_reading_direction(detections: sv.Detections) -> str:
178+
if len(detections) == 0:
179+
return "left_to_right"
180+
181+
xyxy = detections.xyxy
182+
widths = xyxy[:, 2] - xyxy[:, 0]
183+
heights = xyxy[:, 3] - xyxy[:, 1]
184+
185+
avg_width = np.mean(widths)
186+
avg_height = np.mean(heights)
187+
188+
if avg_width > avg_height:
189+
return "left_to_right"
190+
else:
191+
return "vertical_top_to_bottom"
192+
193+
170194
class StitchOCRDetectionsBlockV1(WorkflowBlock):
171195
@classmethod
172196
def get_manifest(cls) -> Type[WorkflowBlockManifest]:
@@ -178,6 +202,8 @@ def run(
178202
reading_direction: str,
179203
tolerance: int,
180204
) -> BlockResult:
205+
if reading_direction == "auto":
206+
reading_direction = detect_reading_direction(predictions[0])
181207
return [
182208
stitch_ocr_detections(
183209
detections=detections,

0 commit comments

Comments
 (0)