File size: 5,596 Bytes
1a219fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32ffff1
1a219fd
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

import cv2
import numpy as np
from app import EngineScanner
import os

def create_synthetic_engine_image():
    """Create a synthetic engine image for testing"""
    # Create a blank canvas
    img = np.ones((800, 1200, 3), dtype=np.uint8) * 100
    
    # Draw engine body (dark brown)
    engine_color = (60, 40, 30)
    cv2.rectangle(img, (150, 100), (1050, 700), engine_color, -1)
    
    # Draw bearing caps (lighter metal)
    cap_color = (180, 180, 160)
    
    # Draw 4 cylinder bores
    cylinder_centers = [(300, 400), (500, 400), (700, 400), (900, 400)]
    cylinder_radius = 60
    
    for cx, cy in cylinder_centers:
        # Bearing cap
        cv2.rectangle(img, (cx-80, cy-80), (cx+80, cy+80), cap_color, -1)
        
        # Cylinder bore (polished metal)
        cv2.circle(img, (cx, cy), cylinder_radius, (220, 220, 200), -1)
        
        # Add some shine/gradient
        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)
    
    # Add some realistic defects (metal chips)
    defect_color = (255, 255, 240)
    
    # Add chips in first cylinder
    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)
    
    # Add some noise for realism
    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()
    
    # Create scanner instance
    print("1. Initializing scanner...")
    scanner = EngineScanner()
    print("   βœ“ Scanner initialized")
    print()
    
    # Create test image
    print("2. Generating synthetic engine image...")
    test_image = create_synthetic_engine_image()
    print("   βœ“ Test image created")
    print()
    
    # Save test image
    test_img_path = "test_engine.jpg"
    cv2.imwrite(test_img_path, test_image)
    print(f"   βœ“ Test image saved as '{test_img_path}'")
    print()
    
    # Run scan
    print("3. Running engine scan...")
    result_image, report = scanner.scan_engine(test_image)
    print("   βœ“ Scan completed")
    print()
    
    # Display report
    print("4. Scan Report:")
    print(report)
    print()
    
    # Save result
    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()
    
    # Check outputs
    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()
    
    # Test 1: Preprocessing
    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()
    
    # Test 2: Center detection
    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()
    
    # Test 3: Bounding box
    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()
    
    # Test 4: Cylinder detection
    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()
    
    # Test 5: Defect analysis
    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__":
  
    
    # Run unit tests first
    test_individual_functions()
    
    # Run full integration test
    test_scanner()