File size: 3,594 Bytes
2b342d6
 
 
 
 
 
 
 
 
 
 
e40d5b1
2b342d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40d5b1
 
 
2b342d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40d5b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b342d6
 
 
 
 
 
 
 
 
 
 
 
 
 
e40d5b1
2b342d6
 
 
 
 
 
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
"""
Basic tests for lane detection functionality
"""
import numpy as np
import cv2
import sys
import os

# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from lane_detection import region_of_interest, process_frame, process_video


def test_region_of_interest():
    """Test region of interest masking"""
    print("Testing region_of_interest function...")
    
    # Create a test image
    img = np.ones((100, 100), dtype=np.uint8) * 255
    
    # Define vertices
    vertices = np.array([[(20, 100), (40, 50), (60, 50), (80, 100)]], dtype=np.int32)
    
    # Apply ROI
    result = region_of_interest(img, vertices)
    
    # Check that result has correct shape
    assert result.shape == img.shape, f"Expected shape {img.shape}, got {result.shape}"
    
    # Check that areas outside ROI are masked (zero)
    assert result[10, 10] == 0, "Pixels outside ROI should be 0"
    
    print("βœ“ region_of_interest test passed")


def test_process_frame():
    """Test frame processing for lane detection"""
    print("Testing process_frame function...")
    
    # Create a test frame with simulated road
    height, width = 480, 640
    frame = np.zeros((height, width, 3), dtype=np.uint8)
    
    # Draw white lines to simulate lanes
    cv2.line(frame, (200, height), (280, int(height*0.6)), (255, 255, 255), 5)
    cv2.line(frame, (440, height), (360, int(height*0.6)), (255, 255, 255), 5)
    
    # Process the frame
    result = process_frame(frame)
    
    # Check that result has correct shape
    assert result.shape == frame.shape, f"Expected shape {frame.shape}, got {result.shape}"
    
    # Check that result is a valid image (not None and correct dtype)
    assert result is not None, "Result should not be None"
    assert result.dtype == np.uint8, "Result should be uint8 type"
    
    print("βœ“ process_frame test passed")


def test_imports():
    """Test that all required modules can be imported"""
    print("Testing imports...")
    
    try:
        import cv2
        print("βœ“ opencv-python imported successfully")
    except ImportError as e:
        print(f"βœ— Failed to import cv2: {e}")
        return False
    
    try:
        import numpy
        print("βœ“ numpy imported successfully")
    except ImportError as e:
        print(f"βœ— Failed to import numpy: {e}")
        return False
    
    return True


def test_video_processing():
    """Test complete video processing"""
    print("Testing video processing...")
    
    from create_test_video import create_test_video
    
    # Create test video
    input_path = "/tmp/test_input.mp4"
    output_path = "/tmp/test_output.mp4"
    
    create_test_video(input_path, duration_sec=1, fps=10)
    
    # Process video
    success = process_video(input_path, output_path)
    
    assert success, "Video processing should succeed"
    assert os.path.exists(output_path), "Output file should exist"
    assert os.path.getsize(output_path) > 0, "Output file should not be empty"
    
    print("βœ“ video processing test passed")


if __name__ == "__main__":
    print("Running lane detection tests...\n")
    
    # Test imports
    if not test_imports():
        print("\nImport tests failed!")
        sys.exit(1)
    
    print()
    
    # Test functions
    try:
        test_region_of_interest()
        test_process_frame()
        test_video_processing()
        print("\nβœ… All tests passed!")
    except Exception as e:
        print(f"\n❌ Test failed: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)