scaner / scanner.py
eho69's picture
Update scanner.py
7f40ffe verified
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()