Skip to content
Merged
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
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,8 +26,8 @@ object FuzzyAttributeParser {
TEXT("android:text", listOf("text")),
HINT("android:hint", listOf("hint")),
BACKGROUND("android:background", listOf("background", "bg"), ValueType.COLOR),
BACKGROUND_TINT("app:backgroundTint", listOf("backgroundtint", "background_tint"), ValueType.COLOR),
SRC("android:src", listOf("src", "scr"), ValueType.DRAWABLE),
BACKGROUND_TINT("app:backgroundTint", listOf("backgroundtint", "background_tint", "bg_tint"), ValueType.COLOR),
SRC("android:src", listOf("src", "scr", "sre", "5rc"), ValueType.DRAWABLE),
CONTENT_DESCRIPTION("android:contentDescription", listOf("contentdescription", "content_description")),

TEXT_SIZE("android:textSize", listOf("textsize", "text_size"), ValueType.SP_DIMENSION),
Expand DownExpand Up@@ -74,7 +74,7 @@ object FuzzyAttributeParser {
LAYOUT_MARGIN_RIGHT("android:layout_marginRight", listOf("layout_marginright", "layout_margin_right", "margin_right"), ValueType.DIMENSION),

LAYOUT_WEIGHT("android:layout_weight", listOf("layout_weight", "weight"), ValueType.FLOAT),
Comment thread
jatezzz marked this conversation as resolved.
LAYOUT_GRAVITY("android:layout_gravity", listOf("layout_gravity")),
LAYOUT_GRAVITY("android:layout_gravity", listOf("layout_gravity", "layaut_gravity")),
GRAVITY("android:gravity", listOf("gravity")),
ORIENTATION("android:orientation", listOf("orientation")),

Expand DownExpand Up@@ -150,6 +150,7 @@ object FuzzyAttributeParser {
private val ocrLetterZToTwoRegex = Regex("[zZ]")
private val ocrLetterSToFiveRegex = Regex("[sS]")
private val ocrLetterBToSixRegex = Regex("[bB]")
private val ocrDenoiseRegex = Regex("inm|rn|wm|nm")
Comment thread
jatezzz marked this conversation as resolved.

private val matchKeywords = setOf("match", "parent")
private val wrapKeywords = setOf("wrap", "content", "wrapcan")
Expand DownExpand Up@@ -419,7 +420,7 @@ object FuzzyAttributeParser {
.replace(Regex("op$"), "dp")
.replace(Regex("olp$"), "dp")

val numericString = fixedUnit.replace(Regex("[a-z]+$"), "")
val numericString = fixedUnit.replace("_", "")
val numericPart = extractOcrNumber(numericString)

if (numericPart != null) return "${numericPart}dp"
Expand All@@ -432,7 +433,7 @@ object FuzzyAttributeParser {
.replace(" ", "")
.replace(Regex("5p$"), "sp")

val numericString = fixedUnit.replace(Regex("[a-z]+$"), "")
val numericString = fixedUnit.replace("_", "")
val numericPart = extractOcrNumber(numericString)

if (numericPart != null) return "${numericPart}sp"
Expand All@@ -457,22 +458,30 @@ object FuzzyAttributeParser {
}

private fun cleanId(value: String): String {
return value.lowercase()
return denoiseOcrIdentifier(value.lowercase())
.replace(nonAlphanumericRegex, "_")
.replace(multipleUnderscoresRegex, "_")
.trimEnd('_')
.trimStart('_')
}

private fun denoiseOcrIdentifier(value: String): String =
value.replace("rn", "m")
.replace("wm", "m")
private fun denoiseOcrIdentifier(value: String): String {
return value.replace(ocrDenoiseRegex) { matchResult ->
when (matchResult.value) {
"inm" -> "im"
else -> "m"
}
}
}

private fun cleanDrawable(value: String): String {
if (value.startsWith("@drawable/")) return value
val cleaned = value.replace(Regex("[^a-zA-Z0-9_.]"), "")
.substringBeforeLast('.')
.lowercase()

var cleaned = value.lowercase()
.replace(Regex("\\.(png|jpg|jpeg|webp|xml|svg)$"), "")

cleaned = cleaned.replace(Regex("[^a-z0-9_]"), "")

return "@drawable/${denoiseOcrIdentifier(cleaned)}"
}
Comment thread
jatezzz marked this conversation as resolved.

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@ import kotlinx.coroutines.coroutineScope
import org.appdevforall.codeonthego.computervision.data.source.OcrSource
import org.appdevforall.codeonthego.computervision.domain.model.DetectionResult
import org.appdevforall.codeonthego.computervision.utils.BitmapUtils
import org.appdevforall.codeonthego.computervision.utils.OcrTextAssembler

class RegionOcrProcessor(
private val ocrSource: OcrSource,
Expand DownExpand Up@@ -69,12 +70,8 @@ class RegionOcrProcessor(
try {
crop = BitmapUtils.cropRegion(bitmap, component.boundingBox, componentPadding)
preprocessed = BitmapUtils.preprocessForOcr(crop)
val text = ocrSource.recognizeText(preprocessed)
.getOrNull()
?.joinToString(" ") { it.text }
?.replace("\n", " ")
?.trim()
?: ""
val textBlocks = ocrSource.recognizeText(preprocessed).getOrNull()
val text = textBlocks?.let { OcrTextAssembler.extractTextWithTolerance(it) } ?: ""
component.copy(text = text)
} finally {
preprocessed?.recycle()
Expand DownExpand Up@@ -124,7 +121,7 @@ class RegionOcrProcessor(
),
label = "text",
score = 0.99f,
text = line.text.trim(),
text = OcrTextAssembler.joinElementsWithTolerance(line),
isYolo = false
)
}
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
package org.appdevforall.codeonthego.computervision.utils

import com.google.mlkit.vision.text.Text

object OcrTextAssembler {
const val DEFAULT_SPACE_GAP_TOLERANCE_PX = 15f

fun extractTextWithTolerance(
textBlocks: List<Text.TextBlock>,
maxSpaceGap: Float = DEFAULT_SPACE_GAP_TOLERANCE_PX
): String {
return textBlocks.joinToString(" ") { block ->
block.lines.joinToString(" ") { line ->
joinElementsWithTolerance(line, maxSpaceGap)
}
}.trim()
}

fun joinElementsWithTolerance(line: Text.Line, maxSpaceGap: Float = DEFAULT_SPACE_GAP_TOLERANCE_PX): String {
val elements = line.elements.sortedBy { it.boundingBox?.left ?: 0 }
if (elements.isEmpty()) return ""

val builder = StringBuilder()
var prevRight = elements.first().boundingBox?.right ?: 0
builder.append(elements.first().text)

for (i in 1 until elements.size) {
val current = elements[i]
val currentBox = current.boundingBox

if (currentBox != null) {
val gap = currentBox.left - prevRight
if (gap > maxSpaceGap) {
builder.append(" ")
}
prevRight = currentBox.right
} else {
builder.append(" ")
}
builder.append(current.text)
Comment thread
jatezzz marked this conversation as resolved.
}
return builder.toString().trim()
}
}
Loading