OpenCVLaneDetectionDemo / test_lane_detection.py
copilot-swe-agent[bot]
kr4phy
Refactor code into modular structure with separate lane detection module
e40d5b1
Raw History Blame Contribute Delete
3.59 kB
"""
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)