From 5cc4ddd82b64c5a5b418b8d7475d8ae604dfce59 Mon Sep 17 00:00:00 2001 From: Quildra Date: Tue, 29 Jul 2025 20:20:59 +0100 Subject: [PATCH] Expand OCR bounding boxes by 5% for stat values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../pokegoalshelper/ScreenCaptureService.kt | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt b/app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt index 997d33b..1ca6455 100644 --- a/app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt +++ b/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