@ -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