From 6d90dc9f15bebe8d6df9e81ee5a0f3fde48580b6 Mon Sep 17 00:00:00 2001 From: Aherobo Ovie Victor Date: Fri, 11 Jul 2025 22:59:36 +0100 Subject: [PATCH] Fix Run All Tests to use last detection result for dynamic messaging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Fixed Logic: - Reverted hardcoded image back to memory image (training/memory/out1.png) - Added lastDetectionResult global variable to track last detection - Run All Tests now uses the last detection result (from upload or hardcoded test) ✅ Dynamic Behavior: - Upload image with memory → Run All Tests shows '✅ Found X memory modules' - Upload image without memory → Run All Tests shows '❌ No memory modules' - No previous upload → Run All Tests uses hardcoded image (with memory) ✅ Workflow: 1. User uploads image without memory → detects 0 modules 2. User clicks 'Run All Tests' → shows '❌ No memory modules' 3. User uploads image with memory → detects X modules 4. User clicks 'Run All Tests' → shows '✅ Found X memory modules' ✅ Reset Logic: - lastDetectionResult reset when file upload is reset - Clean state management between different uploads Now the Run All Tests correctly reflects the last detection result --- main.py | 2 +- static/script.js | 51 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index 797b7ab..279e010 100644 --- a/main.py +++ b/main.py @@ -37,7 +37,7 @@ MODEL_PATH = 'runs/detect/memory_module_detection/weights/best.pt' detector = MemoryModuleDetector(MODEL_PATH) # Hardcoded test image path -HARDCODED_IMAGE_PATH = 'training/no_memory/out1.png' +HARDCODED_IMAGE_PATH = 'training/memory/out1.png' def allowed_file(filename): """Check if file extension is allowed.""" diff --git a/static/script.js b/static/script.js index 56096e5..1c039a4 100644 --- a/static/script.js +++ b/static/script.js @@ -2,6 +2,7 @@ const API_BASE_URL = 'http://localhost:5002'; let uploadedFile = null; +let lastDetectionResult = null; // Initialize the application document.addEventListener('DOMContentLoaded', function() { @@ -210,6 +211,7 @@ function handleFile(file) { function resetFileUpload() { uploadedFile = null; + lastDetectionResult = null; // Reset last detection result // Reset upload area HTML const uploadArea = document.getElementById('uploadArea'); @@ -272,6 +274,8 @@ async function processUploadedImage() { hideLoading(); if (result.success) { + // Store the last detection result for Run All Tests + lastDetectionResult = result; displayResults(result, 'Uploaded Image Detection'); // Add option to upload another file addUploadAnotherOption(); @@ -313,6 +317,8 @@ async function testHardcodedImage() { hideLoading(); if (result.success) { + // Store the last detection result for Run All Tests + lastDetectionResult = result; displayResults(result, 'Hardcoded Image Test'); } else { alert(`Test failed: ${result.error}`); @@ -402,25 +408,40 @@ async function runAllTests() { }); } - // Test 2: Image with Memory Modules - try { - const response = await fetch(`${API_BASE_URL}/detect/hardcoded`); - const result = await response.json(); + // Test 2: Image with Memory Modules (use last detection result if available) + if (lastDetectionResult) { + // Use the last detection result from uploaded/tested image testResults.push({ name: 'Image with Memory Modules', - success: result.success, - message: result.success ? - (result.num_detections > 0 ? - `✅ Found ${result.num_detections} memory modules` : + success: lastDetectionResult.success, + message: lastDetectionResult.success ? + (lastDetectionResult.num_detections > 0 ? + `✅ Found ${lastDetectionResult.num_detections} memory modules` : `❌ No memory modules`) : - `❌ Error: ${result.error}` - }); - } catch (error) { - testResults.push({ - name: 'Image with Memory Modules', - success: false, - message: `❌ Error: ${error.message}` + `❌ Error: ${lastDetectionResult.error}` }); + } else { + // Fallback to hardcoded test if no previous detection + try { + const response = await fetch(`${API_BASE_URL}/detect/hardcoded`); + const result = await response.json(); + lastDetectionResult = result; // Store for future use + testResults.push({ + name: 'Image with Memory Modules', + success: result.success, + message: result.success ? + (result.num_detections > 0 ? + `✅ Found ${result.num_detections} memory modules` : + `❌ No memory modules`) : + `❌ Error: ${result.error}` + }); + } catch (error) { + testResults.push({ + name: 'Image with Memory Modules', + success: false, + message: `❌ Error: ${error.message}` + }); + } } // Test 3: API Information