ocr: expose oriented text quads instead of upright boxes
- Commit
82e515e7a9b720f3d4a1bbc4c280f7dd459bc399- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-20 19:24 UTC (3 months ago)
- Parents
5c1fd2a
Diff
gradle/libs.versions.toml
diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex dad2c542a4..1604b1a8b2 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -120,7 +120,7 @@ appauth = "0.11.1" # Own libraries (vayun-mathur, via JitPack) stockfishLibrary = "1.1.0"-ncnnAndroid = "1.7.0"+ncnnAndroid = "1.8.0" # AppStore – Play install via Aurora gplayapi = "3.6.4"
library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt
diff --git a/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt b/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.ktindex 9d6c81216f..8738cb0ce2 100644--- a/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt+++ b/library/ocr/src/main/java/com/vayunmathur/library/ocr/OcrEngine.kt@@ -39,14 +39,36 @@ import kotlinx.coroutines.withContext */ class OcrEngine(private val context: Context) { - /** One recognised, axis-aligned text region in source-bitmap pixel coordinates. */+ /** A corner of a [TextBox] quad, in source-bitmap pixel coordinates. */+ data class Corner(val x: Float, val y: Float)++ /**+ * One recognised text region in source-bitmap pixel coordinates.+ *+ * [corners] are the four corners of the region's oriented quad in reading+ * order: corner 0 -> 1 runs along the text and corner 0 -> 3 spans its+ * height. [left]/[top]/[right]/[bottom] are the axis-aligned bounds of that+ * quad, so they stay meaningful for consumers that only need a rect.+ * [vertical] marks a region read as vertical script (stacked glyphs).+ */ data class TextBox( val text: String, val left: Int, val top: Int, val right: Int, val bottom: Int,- )+ val corners: List<Corner> = listOf(+ Corner(left.toFloat(), top.toFloat()),+ Corner(right.toFloat(), top.toFloat()),+ Corner(right.toFloat(), bottom.toFloat()),+ Corner(left.toFloat(), bottom.toFloat()),+ ),+ val vertical: Boolean = false,+ ) {+ /** Centre of the quad. Bands reading order without skewing on tilt. */+ val centerY: Float get() = corners.sumOf { it.y.toDouble() }.toFloat() / corners.size+ val centerX: Float get() = corners.sumOf { it.x.toDouble() }.toFloat() / corners.size+ } /** Full result: the joined [text] plus the individual [boxes] it came from. */ data class OcrResult(val text: String, val boxes: List<TextBox>)@@ -74,9 +96,21 @@ class OcrEngine(private val context: Context) { val boxes = engine.recognize(bitmap) .asSequence() .filter { it.text.isNotBlank() && it.right > it.left && it.bottom > it.top }- // Reading order: top-to-bottom, then left-to-right within a row band.- .sortedWith(compareBy({ it.top / READING_ROW_BAND }, { it.left }))- .map { TextBox(it.text.trim(), it.left, it.top, it.right, it.bottom) }+ .map { line ->+ TextBox(+ text = line.text.trim(),+ left = line.left,+ top = line.top,+ right = line.right,+ bottom = line.bottom,+ corners = List(4) { Corner(line.quad[it * 2], line.quad[it * 2 + 1]) },+ vertical = line.vertical,+ )+ }+ // Reading order: top-to-bottom, then left-to-right within a+ // row band. Banding on the quad centre rather than its top+ // edge keeps a tilted line's regions in one band.+ .sortedWith(compareBy({ it.centerY.toInt() / READING_ROW_BAND }, { it.centerX })) .toList() OcrResult(boxes.joinToString("\n") { it.text }.trim(), boxes) } catch (e: Exception) {