OpenCVLaneDetectionDemo / test_lane_detection.py
copilot-swe-agent[bot]
Add Gradio UI with OpenCV lane detection implementation
2b342d6
raw
history blame
3.01 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 app import region_of_interest, process_frame
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 not identical to input (lanes should be drawn)
assert not np.array_equal(result, frame), "Result should have lane lines drawn"
print("βœ“ process_frame test passed")
def test_imports():
"""Test that all required modules can be imported"""
print("Testing imports...")
try:
import gradio
print("βœ“ gradio imported successfully")
except ImportError as e:
print(f"βœ— Failed to import gradio: {e}")
return False
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
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()
print("\nβœ… All tests passed!")
except Exception as e:
print(f"\n❌ Test failed: {e}")
import traceback
traceback.print_exc()
sys.exit(1)