Browse Source

Expand OCR bounding boxes by 5% for stat values

- Add 5% expansion to bounding boxes for hp_value, attack_value, defense_value, sp_atk_value, sp_def_value, speed_value
- Expansion applied before clipping to image boundaries
- Added debug logging to show bbox expansion transformations
- Should improve OCR accuracy for tight stat value detections

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
feature/expand-stat-ocr-boxes
Quildra 5 months ago
parent
commit
5cc4ddd82b
  1. 35
      app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt

35
app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt

@ -636,18 +636,37 @@ class ScreenCaptureService : Service() {
if (detection == null) return null
try {
// Validate and clip bounding box to image boundaries
// Expand bounding box by 5% for stat value classes to improve OCR accuracy
val bbox = detection.boundingBox
val clippedX = kotlin.math.max(0, kotlin.math.min(bbox.x, mat.cols() - 1))
val clippedY = kotlin.math.max(0, kotlin.math.min(bbox.y, mat.rows() - 1))
val clippedWidth = kotlin.math.max(1, kotlin.math.min(bbox.width, mat.cols() - clippedX))
val clippedHeight = kotlin.math.max(1, kotlin.math.min(bbox.height, mat.rows() - clippedY))
val expandedBbox = if (detection.className in setOf("hp_value", "attack_value", "defense_value", "sp_atk_value", "sp_def_value", "speed_value")) {
val expansionFactor = 0.05f // 5% expansion
val widthExpansion = (bbox.width * expansionFactor).toInt()
val heightExpansion = (bbox.height * expansionFactor).toInt()
Rect(
bbox.x - widthExpansion,
bbox.y - heightExpansion,
bbox.width + (2 * widthExpansion),
bbox.height + (2 * heightExpansion)
)
} else {
bbox
}
// Validate and clip bounding box to image boundaries
val clippedX = kotlin.math.max(0, kotlin.math.min(expandedBbox.x, mat.cols() - 1))
val clippedY = kotlin.math.max(0, kotlin.math.min(expandedBbox.y, mat.rows() - 1))
val clippedWidth = kotlin.math.max(1, kotlin.math.min(expandedBbox.width, mat.cols() - clippedX))
val clippedHeight = kotlin.math.max(1, kotlin.math.min(expandedBbox.height, mat.rows() - clippedY))
val safeBbox = Rect(clippedX, clippedY, clippedWidth, clippedHeight)
// Debug logging for problematic bounding boxes
if (safeBbox.x != bbox.x || safeBbox.y != bbox.y || safeBbox.width != bbox.width || safeBbox.height != bbox.height) {
Log.w(TAG, "⚠️ Clipped bbox for ${detection.className}: original=[${bbox.x},${bbox.y},${bbox.width},${bbox.height}] → safe=[${safeBbox.x},${safeBbox.y},${safeBbox.width},${safeBbox.height}] (image: ${mat.cols()}x${mat.rows()})")
// Debug logging for bounding box transformations
if (expandedBbox != bbox) {
Log.d(TAG, "📏 Expanded bbox for ${detection.className}: [${bbox.x},${bbox.y},${bbox.width},${bbox.height}] → [${expandedBbox.x},${expandedBbox.y},${expandedBbox.width},${expandedBbox.height}]")
}
if (safeBbox.x != expandedBbox.x || safeBbox.y != expandedBbox.y || safeBbox.width != expandedBbox.width || safeBbox.height != expandedBbox.height) {
Log.w(TAG, "⚠️ Clipped bbox for ${detection.className}: expanded=[${expandedBbox.x},${expandedBbox.y},${expandedBbox.width},${expandedBbox.height}] → safe=[${safeBbox.x},${safeBbox.y},${safeBbox.width},${safeBbox.height}] (image: ${mat.cols()}x${mat.rows()})")
}
// Extract region of interest using safe bounding box

Loading…
Cancel
Save