OpenCVLaneDetectionDemo / create_sample_images.py
copilot-swe-agent[bot]
Fix cross-platform compatibility and add error handling
f4c30f7
"""
Create sample images to demonstrate lane detection
"""
import cv2
import numpy as np
import os
import tempfile
def create_sample_frames():
"""Create and save sample frames showing lane detection"""
# First, create a test video and process it
from create_test_video import create_test_video
from lane_detection import process_video
temp_dir = tempfile.gettempdir()
input_video = os.path.join(temp_dir, "demo_input.mp4")
output_video = os.path.join(temp_dir, "demo_output.mp4")
# Create a test video with clear lanes
print("Creating demo video...")
create_test_video(input_video, duration_sec=2, fps=15)
print("Processing video with lane detection...")
success = process_video(input_video, output_video)
if not success:
print("Failed to process video")
return
# Extract a sample frame from the output
cap = cv2.VideoCapture(output_video)
# Get a frame from the middle of the video
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
middle_frame = total_frames // 2
cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame)
ret, frame = cap.read()
if ret:
# Save the frame
output_path = os.path.join(temp_dir, "lane_detection_demo.png")
cv2.imwrite(output_path, frame)
print(f"✓ Sample frame saved to: {output_path}")
print(f" Frame shows original (left) and lane detection (right)")
# Also create a smaller version for documentation
height, width = frame.shape[:2]
scale = 0.5
small_frame = cv2.resize(frame, (int(width * scale), int(height * scale)))
small_output_path = os.path.join(temp_dir, "lane_detection_demo_small.png")
cv2.imwrite(small_output_path, small_frame)
print(f"✓ Smaller version saved to: {small_output_path}")
else:
print("Failed to extract frame")
cap.release()
if __name__ == "__main__":
create_sample_frames()