@ -44,8 +44,7 @@ class YOLOInferenceEngine(
private const val NUM_DETECTIONS = 300
private const val NUM_DETECTIONS = 300
private const val NUM_CLASSES = 95
private const val NUM_CLASSES = 95
// Enhanced accuracy settings for ONNX (fixed input size) - WITH PER-METHOD COORDINATE TRANSFORM
// Enhanced accuracy settings for ONNX (fixed input size)
private const val ENABLE_MULTI_PREPROCESSING = false // Multiple preprocessing techniques - DISABLED for mobile performance
private const val ENABLE_TTA = true // Test-time augmentation
private const val ENABLE_TTA = true // Test-time augmentation
private const val MAX_INFERENCE_TIME_MS = 4500L // Leave 500ms for other processing
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 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
var SHOW_ALL_CONFIDENCES = false // Show all detections with their confidences
// Preprocessing enhancement techniques
// Preprocessing enhancement techniques (single pass only)
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
private const val ENABLE_NOISE_REDUCTION = true
private const val ENABLE_NOISE_REDUCTION = true
// Confidence threshold optimization for mobile ONNX vs raw processing
// Confidence threshold optimization for mobile ONNX vs raw processing
@ -222,16 +218,8 @@ class YOLOInferenceEngine(
{
{
PGHLog . d ( TAG , " 🎯 Starting ONNX YOLO detection... " )
PGHLog . d ( TAG , " 🎯 Starting ONNX YOLO detection... " )
val detections = if ( ENABLE_MULTI_PREPROCESSING )
// Single preprocessing method - ultralytics standard
{
val detections = detectWithPreprocessing ( inputMat )
// Multiple preprocessing methods with parallel execution
detectWithMultiplePreprocessing ( inputMat )
}
else
{
// Single preprocessing method
detectWithPreprocessing ( inputMat , " ultralytics " )
}
// Update performance stats
// Update performance stats
lastInferenceTime = System . currentTimeMillis ( ) - start_time
lastInferenceTime = System . currentTimeMillis ( ) - start_time
@ -249,71 +237,13 @@ class YOLOInferenceEngine(
}
}
}
}
private suspend fun detectWithMultiplePreprocessing ( inputMat : Mat ) : List < Detection >
{
val futures = mutableListOf < Future < List < Detection > > > ( )
try
{
// Submit different preprocessing methods using shared executor
if ( ENABLE_ULTRALYTICS_PREPROCESSING )
{
futures . add ( preprocessingExecutor . submit < List < Detection > > {
detectWithPreprocessing ( inputMat , " ultralytics " )
} )
}
if ( ENABLE_CONTRAST_ENHANCEMENT )
{
futures . add ( preprocessingExecutor . submit < List < Detection > > {
detectWithPreprocessing ( inputMat , " enhanced " )
} )
}
if ( ENABLE_SHARPENING )
{
futures . add ( preprocessingExecutor . submit < List < Detection > > {
detectWithPreprocessing ( inputMat , " sharpened " )
} )
}
// Collect results with timeout
val all_detections = mutableListOf < Detection > ( )
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 < Detection >
private fun detectWithPreprocessing ( inputMat : Mat ) : List < Detection >
{
{
try
try
{
{
// Apply preprocessing based on method
// Apply ultralytics preprocessing (only method used)
val preprocessed_mat = when ( method )
val preprocessed_mat = preprocessUltralyticsStyle ( inputMat )
{
" ultralytics " -> preprocessUltralyticsStyle ( inputMat )
" enhanced " -> enhanceImageForDetection ( inputMat )
" sharpened " -> applySharpeningFilter ( inputMat )
else -> preprocessOriginalStyle ( inputMat )
}
return try
return try
{
{
@ -326,7 +256,7 @@ class YOLOInferenceEngine(
}
}
catch ( e : Exception )
catch ( e : Exception )
{
{
PGHLog . e ( TAG , " ❌ Error in preprocessing method ' $method ' " , e )
PGHLog . e ( TAG , " ❌ Error in preprocessing " , e )
return emptyList ( )
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
* Letterbox resize maintaining aspect ratio with gray padding
* /
* /