From 7eb1da97d7f4fc4090f411ff6c37a2f7b1a1f035 Mon Sep 17 00:00:00 2001 From: Quildra Date: Sat, 2 Aug 2025 16:38:18 +0100 Subject: [PATCH] refactor: remove unused multi-preprocessing code and configurations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove ENABLE_MULTI_PREPROCESSING, ENABLE_CONTRAST_ENHANCEMENT, ENABLE_SHARPENING, ENABLE_ULTRALYTICS_PREPROCESSING flags - Delete detectWithMultiplePreprocessing() method (complex parallel execution) - Delete enhanceImageForDetection() method (CLAHE contrast enhancement) - Delete applySharpeningFilter() method (convolution sharpening) - Delete preprocessOriginalStyle() method (simple resize) - Simplify detectWithPreprocessing() to only use ultralytics standard preprocessing - Remove method parameter since only one preprocessing approach is used - Clean up orphaned comments and documentation This removes ~150 lines of unused code while maintaining 100% functionality. Single ultralytics preprocessing provides good accuracy at 640x640 resolution for small UI elements like pokeballs and shiny icons. Related todos: #remove-unused-multipass 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../pokegoalshelper/ml/YOLOInferenceEngine.kt | 168 +----------------- 1 file changed, 8 insertions(+), 160 deletions(-) diff --git a/app/src/main/java/com/quillstudios/pokegoalshelper/ml/YOLOInferenceEngine.kt b/app/src/main/java/com/quillstudios/pokegoalshelper/ml/YOLOInferenceEngine.kt index 5cc0a55..60b6eca 100644 --- a/app/src/main/java/com/quillstudios/pokegoalshelper/ml/YOLOInferenceEngine.kt +++ b/app/src/main/java/com/quillstudios/pokegoalshelper/ml/YOLOInferenceEngine.kt @@ -44,8 +44,7 @@ class YOLOInferenceEngine( private const val NUM_DETECTIONS = 300 private const val NUM_CLASSES = 95 - // Enhanced accuracy settings for ONNX (fixed input size) - WITH PER-METHOD COORDINATE TRANSFORM - private const val ENABLE_MULTI_PREPROCESSING = false // Multiple preprocessing techniques - DISABLED for mobile performance + // Enhanced accuracy settings for ONNX (fixed input size) private const val ENABLE_TTA = true // Test-time augmentation private const val MAX_INFERENCE_TIME_MS = 4500L // Leave 500ms for other processing @@ -56,10 +55,7 @@ class YOLOInferenceEngine( var DEBUG_CLASS_FILTER: String? = null // Set to class name to show only that class var SHOW_ALL_CONFIDENCES = false // Show all detections with their confidences - // Preprocessing enhancement techniques - private const val ENABLE_CONTRAST_ENHANCEMENT = true - private const val ENABLE_SHARPENING = true - private const val ENABLE_ULTRALYTICS_PREPROCESSING = true // Re-enabled with fixed coordinates + // Preprocessing enhancement techniques (single pass only) private const val ENABLE_NOISE_REDUCTION = true // Confidence threshold optimization for mobile ONNX vs raw processing @@ -222,16 +218,8 @@ class YOLOInferenceEngine( { PGHLog.d(TAG, "🎯 Starting ONNX YOLO detection...") - val detections = if (ENABLE_MULTI_PREPROCESSING) - { - // Multiple preprocessing methods with parallel execution - detectWithMultiplePreprocessing(inputMat) - } - else - { - // Single preprocessing method - detectWithPreprocessing(inputMat, "ultralytics") - } + // Single preprocessing method - ultralytics standard + val detections = detectWithPreprocessing(inputMat) // Update performance stats lastInferenceTime = System.currentTimeMillis() - start_time @@ -249,71 +237,13 @@ class YOLOInferenceEngine( } } - private suspend fun detectWithMultiplePreprocessing(inputMat: Mat): List - { - val futures = mutableListOf>>() - - try - { - // Submit different preprocessing methods using shared executor - if (ENABLE_ULTRALYTICS_PREPROCESSING) - { - futures.add(preprocessingExecutor.submit> { - detectWithPreprocessing(inputMat, "ultralytics") - }) - } - - if (ENABLE_CONTRAST_ENHANCEMENT) - { - futures.add(preprocessingExecutor.submit> { - detectWithPreprocessing(inputMat, "enhanced") - }) - } - - if (ENABLE_SHARPENING) - { - futures.add(preprocessingExecutor.submit> { - detectWithPreprocessing(inputMat, "sharpened") - }) - } - - // Collect results with timeout - val all_detections = mutableListOf() - for (future in futures) - { - try - { - val detections = future.get(MAX_INFERENCE_TIME_MS / futures.size, TimeUnit.MILLISECONDS) - all_detections.addAll(detections) - } - catch (e: Exception) - { - PGHLog.w(TAG, "⚠️ Preprocessing method timed out or failed", e) - } - } - - // Merge and filter detections - return mergeAndFilterDetections(all_detections) - } - catch (e: Exception) - { - PGHLog.e(TAG, "❌ Error in multiple preprocessing", e) - return emptyList() - } - } - private fun detectWithPreprocessing(inputMat: Mat, method: String): List + private fun detectWithPreprocessing(inputMat: Mat): List { try { - // Apply preprocessing based on method - val preprocessed_mat = when (method) - { - "ultralytics" -> preprocessUltralyticsStyle(inputMat) - "enhanced" -> enhanceImageForDetection(inputMat) - "sharpened" -> applySharpeningFilter(inputMat) - else -> preprocessOriginalStyle(inputMat) - } + // Apply ultralytics preprocessing (only method used) + val preprocessed_mat = preprocessUltralyticsStyle(inputMat) return try { @@ -326,7 +256,7 @@ class YOLOInferenceEngine( } catch (e: Exception) { - PGHLog.e(TAG, "❌ Error in preprocessing method '$method'", e) + PGHLog.e(TAG, "❌ Error in preprocessing", e) return emptyList() } } @@ -474,88 +404,6 @@ class YOLOInferenceEngine( } } - /** - * Enhanced preprocessing with CLAHE contrast enhancement - */ - private fun enhanceImageForDetection(inputMat: Mat): Mat - { - val enhanced = Mat() - try - { - // Apply CLAHE for better contrast - val gray = Mat() - val enhanced_gray = Mat() - - if (inputMat.channels() == 3) - { - Imgproc.cvtColor(inputMat, gray, Imgproc.COLOR_BGR2GRAY) - } - else if (inputMat.channels() == 4) - { - Imgproc.cvtColor(inputMat, gray, Imgproc.COLOR_BGRA2GRAY) - } - else - { - inputMat.copyTo(gray) - } - - val clahe = Imgproc.createCLAHE(CLAHE_CLIP_LIMIT, Size(CLAHE_TILE_SIZE, CLAHE_TILE_SIZE)) - clahe.apply(gray, enhanced_gray) - - // Convert back to color - if (inputMat.channels() >= 3) - { - Imgproc.cvtColor(enhanced_gray, enhanced, Imgproc.COLOR_GRAY2BGR) - } - else - { - enhanced_gray.copyTo(enhanced) - } - - gray.release() - enhanced_gray.release() - } - catch (e: Exception) - { - PGHLog.e(TAG, "❌ Error enhancing image", e) - inputMat.copyTo(enhanced) - } - return enhanced - } - - /** - * Apply sharpening filter for enhanced edge detection - */ - private fun applySharpeningFilter(inputMat: Mat): Mat - { - val sharpened = Mat() - try - { - // Create sharpening kernel - val kernel = Mat(3, 3, CvType.CV_32F) - kernel.put(0, 0, 0.0, SHARPENING_EDGE_VALUE, 0.0, SHARPENING_EDGE_VALUE, SHARPENING_CENTER_VALUE, SHARPENING_EDGE_VALUE, 0.0, SHARPENING_EDGE_VALUE, 0.0) - - // Apply filter - Imgproc.filter2D(inputMat, sharpened, -1, kernel) - - kernel.release() - } - catch (e: Exception) - { - PGHLog.e(TAG, "❌ Error sharpening image", e) - inputMat.copyTo(sharpened) - } - return sharpened - } - - /** - * Original style preprocessing (simple resize) - */ - private fun preprocessOriginalStyle(inputMat: Mat): Mat - { - return safeResize(inputMat, Size(config.inputSize.toDouble(), config.inputSize.toDouble())) - } - /** * Letterbox resize maintaining aspect ratio with gray padding */