|
|
| import cv2 |
| import numpy as np |
| from app import EngineScanner |
| import os |
|
|
| def create_synthetic_engine_image(): |
| """Create a synthetic engine image for testing""" |
| |
| img = np.ones((800, 1200, 3), dtype=np.uint8) * 100 |
| |
| |
| engine_color = (60, 40, 30) |
| cv2.rectangle(img, (150, 100), (1050, 700), engine_color, -1) |
| |
| |
| cap_color = (180, 180, 160) |
| |
| |
| cylinder_centers = [(300, 400), (500, 400), (700, 400), (900, 400)] |
| cylinder_radius = 60 |
| |
| for cx, cy in cylinder_centers: |
| |
| cv2.rectangle(img, (cx-80, cy-80), (cx+80, cy+80), cap_color, -1) |
| |
| |
| cv2.circle(img, (cx, cy), cylinder_radius, (220, 220, 200), -1) |
| |
| |
| overlay = img.copy() |
| cv2.circle(overlay, (cx-15, cy-15), 25, (250, 250, 240), -1) |
| cv2.addWeighted(overlay, 0.4, img, 0.6, 0, img) |
| |
| |
| defect_color = (255, 255, 240) |
| |
| |
| np.random.seed(42) |
| for _ in range(5): |
| x = np.random.randint(240, 360) |
| y = np.random.randint(340, 460) |
| size = np.random.randint(3, 8) |
| cv2.circle(img, (x, y), size, defect_color, -1) |
| |
| |
| noise = np.random.normal(0, 5, img.shape).astype(np.uint8) |
| img = cv2.add(img, noise) |
| |
| return img |
|
|
| def test_scanner(): |
| """Test the engine scanner with synthetic image""" |
| print("="*60) |
| print("ENGINE SCANNING SYSTEM - TEST SUITE") |
| print("="*60) |
| print() |
| |
| |
| print("1. Initializing scanner...") |
| scanner = EngineScanner() |
| print(" β Scanner initialized") |
| print() |
| |
| |
| print("2. Generating synthetic engine image...") |
| test_image = create_synthetic_engine_image() |
| print(" β Test image created") |
| print() |
| |
| |
| test_img_path = "test_engine.jpg" |
| cv2.imwrite(test_img_path, test_image) |
| print(f" β Test image saved as '{test_img_path}'") |
| print() |
| |
| |
| print("3. Running engine scan...") |
| result_image, report = scanner.scan_engine(test_image) |
| print(" β Scan completed") |
| print() |
| |
| |
| print("4. Scan Report:") |
| print(report) |
| print() |
| |
| |
| if result_image is not None: |
| result_path = "test_result.jpg" |
| cv2.imwrite(result_path, result_image) |
| print(f" β Result saved as '{result_path}'") |
| else: |
| print(" β No result image generated") |
| print() |
| |
| |
| print("5. Verifying outputs...") |
| results_dir = "scan_results" |
| if os.path.exists(results_dir): |
| files = os.listdir(results_dir) |
| print(f" β Found {len(files)} files in '{results_dir}/':") |
| for f in files: |
| print(f" - {f}") |
| else: |
| print(f" β Results directory not found") |
| print() |
| |
| print("="*60) |
| print("TEST COMPLETE") |
| print("="*60) |
| print() |
| print("Next steps:") |
| print("1. Review 'test_result.jpg' to see annotated output") |
| print("2. Check 'scan_results/' directory for saved files") |
| print("3. Run 'python app.py' to launch the web interface") |
| print() |
|
|
| def test_individual_functions(): |
| """Test individual functions""" |
| print("="*60) |
| print("UNIT TESTS - Individual Functions") |
| print("="*60) |
| print() |
| |
| scanner = EngineScanner() |
| test_image = create_synthetic_engine_image() |
| |
| |
| print("Test 1: Image Preprocessing") |
| gray, enhanced = scanner.preprocess_image(test_image) |
| print(f" Input shape: {test_image.shape}") |
| print(f" Gray shape: {gray.shape}") |
| print(f" Enhanced shape: {enhanced.shape}") |
| print(f" β Preprocessing works") |
| print() |
| |
| |
| print("Test 2: Engine Center Detection") |
| center, contours, mask = scanner.find_engine_center(test_image) |
| print(f" Center found at: {center}") |
| print(f" Number of contours: {len(contours)}") |
| print(f" β Center detection works") |
| print() |
| |
| |
| print("Test 3: Bounding Box Creation") |
| bbox, dimensions = scanner.create_bounding_box(test_image, center, contours) |
| print(f" Bounding box: {bbox}") |
| print(f" Dimensions: {dimensions[0]} x {dimensions[1]} px") |
| print(f" β Bounding box creation works") |
| print() |
| |
| |
| print("Test 4: Cylinder Detection") |
| cylinders = scanner.detect_cylinders(test_image, bbox) |
| print(f" Cylinders detected: {len(cylinders)}") |
| for i, cyl in enumerate(cylinders): |
| print(f" Cylinder {i+1}: center={cyl['center']}, radius={cyl['radius']}") |
| print(f" β Cylinder detection works") |
| print() |
| |
| |
| print("Test 5: Defect Analysis") |
| defects = scanner.analyze_defects(test_image, bbox) |
| print(f" Status: {defects['status']}") |
| print(f" Defect count: {defects['defect_count']}") |
| print(f" Defect percentage: {defects['defect_percentage']}%") |
| print(f" β Defect analysis works") |
| print() |
| |
| print("="*60) |
| print("ALL UNIT TESTS PASSED") |
| print("="*60) |
| print() |
|
|
| if __name__ == "__main__": |
| |
| |
| |
| test_individual_functions() |
| |
| |
| test_scanner() |
| |
|
|