Skip to content

Commit a76b50f

Browse files
committed
refactor(cv): extract generic box resolution to domain layer
Introduce GenericBoxResolver to process generic_box detections and update YOLO labels.
1 parent 0706cad commit a76b50f

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed
-5 Bytes
Binary file not shown.

cv-image-to-xml/src/main/assets/labels.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
dropdown
1+
generic_box
2+
dropdown_symbol
23
image_placeholder
34
button
45
checkbox_checked
6+
checkbox_unchecked
57
radio_button_unchecked
68
radio_button_checked
7-
text_entry_box
8-
checkbox_unchecked
99
slider
1010
switch_off
1111
switch_on

cv-image-to-xml/src/main/java/org/appdevforall/codeonthego/computervision/data/repository/ComputerVisionRepositoryImpl.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.appdevforall.codeonthego.computervision.domain.model.DetectionResult
1010
import com.google.mlkit.vision.text.Text
1111
import kotlinx.coroutines.Dispatchers
1212
import kotlinx.coroutines.withContext
13+
import org.appdevforall.codeonthego.computervision.domain.GenericBoxResolver
1314

1415
class ComputerVisionRepositoryImpl(
1516
private val assetManager: AssetManager,
@@ -26,7 +27,8 @@ class ComputerVisionRepositoryImpl(
2627
override suspend fun runYoloInference(bitmap: Bitmap): Result<List<DetectionResult>> =
2728
withContext(Dispatchers.Default) {
2829
runCatching {
29-
yoloModelSource.runInference(bitmap)
30+
val rawDetections = yoloModelSource.runInference(bitmap)
31+
GenericBoxResolver().resolve(rawDetections)
3032
}
3133
}
3234

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.appdevforall.codeonthego.computervision.domain
2+
3+
import android.graphics.RectF
4+
import org.appdevforall.codeonthego.computervision.domain.model.DetectionResult
5+
import kotlin.math.hypot
6+
7+
class GenericBoxResolver {
8+
9+
fun resolve(detections: List<DetectionResult>): List<DetectionResult> {
10+
val dropdownSymbols = detections.filter { it.label == "dropdown_symbol" }
11+
12+
return detections.mapNotNull { det ->
13+
when (det.label) {
14+
"dropdown_symbol" -> null
15+
"generic_box" -> {
16+
val hasSymbolNearby = dropdownSymbols.any { symbol ->
17+
isNearby(det.boundingBox, symbol.boundingBox, 0.8f)
18+
}
19+
det.copy(label = if (hasSymbolNearby) "dropdown" else "text_entry_box")
20+
}
21+
else -> det
22+
}
23+
}
24+
}
25+
26+
private fun isNearby(box1: RectF, box2: RectF, thresholdFactor: Float = 1.5f): Boolean {
27+
val avgDim1 = (box1.width() + box1.height()) / 2f
28+
val distanceThreshold = thresholdFactor * avgDim1
29+
30+
val distance = hypot(
31+
(box1.centerX() - box2.centerX()).toDouble(),
32+
(box1.centerY() - box2.centerY()).toDouble()
33+
).toFloat()
34+
35+
return distance < distanceThreshold
36+
}
37+
}

0 commit comments

Comments
 (0)