From b96839d436e1dc48e2c5ad927fb2920a68830fda Mon Sep 17 00:00:00 2001 From: Aherobo Ovie Victor Date: Fri, 11 Jul 2025 22:11:09 +0100 Subject: [PATCH] Fix Run All Tests to properly test both memory and no-memory scenarios --- main.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++-- static/script.js | 37 +++++++++++++++++++------- 2 files changed, 94 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index 022dc23..2cadd6e 100644 --- a/main.py +++ b/main.py @@ -71,7 +71,8 @@ def home(): '/': 'GET - Frontend interface or API information', '/api': 'GET - API information (JSON)', '/detect': 'POST - Upload image for memory module detection', - '/detect/hardcoded': 'GET - Process hardcoded test image', + '/detect/hardcoded': 'GET - Process hardcoded test image (with memory)', + '/detect/no-memory': 'GET - Process test image without memory modules', '/detect/base64': 'POST - Process base64 encoded image', '/health': 'GET - Health check' }, @@ -89,7 +90,8 @@ def api_info(): '/': 'GET - Frontend interface or API information', '/api': 'GET - API information (JSON)', '/detect': 'POST - Upload image for memory module detection', - '/detect/hardcoded': 'GET - Process hardcoded test image', + '/detect/hardcoded': 'GET - Process hardcoded test image (with memory)', + '/detect/no-memory': 'GET - Process test image without memory modules', '/detect/base64': 'POST - Process base64 encoded image', '/health': 'GET - Health check' }, @@ -254,6 +256,68 @@ def detect_hardcoded_image(): 'success': False }), 500 +@app.route('/detect/no-memory', methods=['GET']) +def detect_no_memory_image(): + """ + Process hardcoded test image without memory modules for testing. + + Optional query parameters: + - confidence: confidence threshold (default: 0.8) + + Returns: + - JSON with detections (should be 0) and annotated image (base64) + """ + try: + # Check if model is loaded + if detector.model is None: + return jsonify({ + 'error': 'Model not loaded. Please train the model first.', + 'success': False + }), 500 + + # Use no-memory test image + no_memory_image_path = 'training/no_memory/out21.png' + + # Check if no-memory image exists + if not os.path.exists(no_memory_image_path): + return jsonify({ + 'error': f'No-memory test image not found at {no_memory_image_path}', + 'success': False + }), 404 + + # Get confidence threshold from query parameters (default 80%) + conf_threshold = float(request.args.get('confidence', 0.8)) + + # Run detection + detections, annotated_image = detector.detect( + no_memory_image_path, + conf_threshold=conf_threshold + ) + + # Convert annotated image to base64 + annotated_base64 = image_to_base64(annotated_image) + + # Prepare response + response_data = { + 'success': True, + 'detections': detections, + 'num_detections': len(detections), + 'annotated_image': annotated_base64, + 'confidence_threshold': conf_threshold, + 'test_image_path': no_memory_image_path + } + + logger.info(f"Processed no-memory image: found {len(detections)} memory modules") + + return jsonify(response_data) + + except Exception as e: + logger.error(f"Error processing no-memory image: {str(e)}") + return jsonify({ + 'error': f'Error processing no-memory image: {str(e)}', + 'success': False + }), 500 + @app.route('/detect/base64', methods=['POST']) def detect_base64_image(): """ diff --git a/static/script.js b/static/script.js index 9586de4..ea856d4 100644 --- a/static/script.js +++ b/static/script.js @@ -390,26 +390,45 @@ async function runAllTests() { }); } - // Test 2: Hardcoded Image + // Test 2: Image with Memory Modules try { const response = await fetch(`${API_BASE_URL}/detect/hardcoded`); const result = await response.json(); testResults.push({ - name: 'Hardcoded Image Detection', - success: result.success, - message: result.success ? - `Found ${result.num_detections} memory modules` : - `Error: ${result.error}` + name: 'Image with Memory Modules', + success: result.success && result.num_detections > 0, + message: result.success ? + `✅ Found ${result.num_detections} memory modules` : + `❌ Error: ${result.error}` }); } catch (error) { testResults.push({ - name: 'Hardcoded Image Detection', + name: 'Image with Memory Modules', success: false, - message: `Error: ${error.message}` + message: `❌ Error: ${error.message}` + }); + } + + // Test 3: Image without Memory Modules + try { + const response = await fetch(`${API_BASE_URL}/detect/no-memory`); + const result = await response.json(); + testResults.push({ + name: 'Image without Memory Modules', + success: result.success && result.num_detections === 0, + message: result.success ? + `✅ Correctly detected ${result.num_detections} memory modules` : + `❌ Error: ${result.error}` + }); + } catch (error) { + testResults.push({ + name: 'Image without Memory Modules', + success: false, + message: `❌ Error: ${error.message}` }); } - // Test 3: API Information + // Test 4: API Information try { const response = await fetch(`${API_BASE_URL}/api`); const result = await response.json();