Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tempfile | |
| import os | |
| from lane_detection import process_video as process_video_file | |
| def process_video(video_path, method, use_enhanced, use_segmented, progress=gr.Progress()): | |
| """ | |
| Process the uploaded video and return side-by-side comparison. | |
| Wrapper function for Gradio interface with progress tracking. | |
| """ | |
| if video_path is None: | |
| return None | |
| # Create temporary output file with explicit .mp4 extension | |
| temp_dir = tempfile.gettempdir() | |
| temp_filename = f"lane_detection_{os.urandom(8).hex()}.mp4" | |
| output_path = os.path.join(temp_dir, temp_filename) | |
| try: | |
| # Progress callback function | |
| def update_progress(value, desc): | |
| progress(value, desc=desc) | |
| # Process the video with selected method and progress callback | |
| success = process_video_file(video_path, output_path, method, use_enhanced, use_segmented, progress_callback=update_progress) | |
| if success and os.path.exists(output_path): | |
| return output_path | |
| else: | |
| return None | |
| except Exception as e: | |
| print(f"Error processing video: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return None | |
| finally: | |
| # Clean up input if it was a temporary file | |
| if video_path and video_path.startswith(temp_dir): | |
| try: | |
| if os.path.exists(video_path): | |
| os.remove(video_path) | |
| except: | |
| pass | |
| # Create Gradio interface | |
| with gr.Blocks(title="Lane Detection Demo") as demo: | |
| gr.Markdown("# ๐ OpenCV Lane Detection Demo") | |
| gr.Markdown("Upload a video to detect lane lines. Choose between multiple advanced methods.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| video_input = gr.Video(label="Upload Video") | |
| with gr.Row(): | |
| method_selector = gr.Radio( | |
| choices=[ | |
| "basic_standard", | |
| "basic_segmented", | |
| "advanced", | |
| "yolop", | |
| "ufld", | |
| "scnn" | |
| ], | |
| value="yolop", | |
| label="Detection Method", | |
| info="Select lane detection algorithm" | |
| ) | |
| enhanced_checkbox = gr.Checkbox( | |
| value=True, | |
| label="Enhanced Thresholding", | |
| info="Better accuracy but slightly slower (Advanced method only)", | |
| visible=True | |
| ) | |
| segmented_checkbox = gr.Checkbox( | |
| value=False, | |
| label="Segmented Lines", | |
| info="Multiple segments for better curve representation (Basic method only)", | |
| visible=False | |
| ) | |
| process_btn = gr.Button("Process Video", variant="primary") | |
| with gr.Column(): | |
| video_output = gr.Video( | |
| label="Result (Original | Lane Detection)", | |
| format="mp4" # Explicitly set format for better browser compatibility | |
| ) | |
| # Update checkbox visibility based on method | |
| def update_checkboxes(method): | |
| enhanced_visible = (method == "advanced") | |
| segmented_visible = (method in ["basic_standard", "basic_segmented"]) | |
| return gr.Checkbox(visible=enhanced_visible), gr.Checkbox(visible=segmented_visible) | |
| method_selector.change( | |
| fn=update_checkboxes, | |
| inputs=method_selector, | |
| outputs=[enhanced_checkbox, segmented_checkbox] | |
| ) | |
| process_btn.click( | |
| fn=process_video, | |
| inputs=[video_input, method_selector, enhanced_checkbox, segmented_checkbox], | |
| outputs=video_output | |
| ) | |
| gr.Markdown(""" | |
| ### Detection Methods: | |
| **๐น Basic Standard (Hough Transform):** | |
| - Fast and lightweight | |
| - Good for straight lanes | |
| - Uses single averaged line per lane | |
| - Fastest processing speed | |
| **๐น Basic Segmented (Hough Transform):** | |
| - Fast processing | |
| - Multiple line segments for better curve representation | |
| - Good for moderately curved lanes | |
| - Better than basic for curves | |
| **๐น Advanced (Perspective Transform + Polynomial):** | |
| - Perspective transform to bird's eye view | |
| - Polynomial fitting with sliding windows | |
| - Excellent for curved and dashed lanes | |
| - Enhanced mode uses CLAHE and gradient filtering | |
| - Best accuracy but slower | |
| **๐น YOLOP (Multi-task Learning):** | |
| - Inspired by YOLOP (You Only Look Once for Panoptic Driving) | |
| - Multi-color lane detection (white and yellow) | |
| - Contour-based segmentation | |
| - Good for various lane colors | |
| - Fast with good accuracy | |
| **๐น UFLD (Ultra Fast Lane Detection):** | |
| - Inspired by Ultra Fast Structure-aware Deep Lane Detection | |
| - Row-wise classification approach | |
| - Adaptive thresholding with CLAHE | |
| - Excellent balance of speed and accuracy | |
| - Real-time capable | |
| **๐น SCNN (Spatial CNN):** | |
| - Inspired by Spatial CNN for traffic lane detection | |
| - Spatial message passing in four directions | |
| - Multi-scale edge detection | |
| - Best for complex scenarios | |
| - High accuracy for challenging conditions | |
| ### How it works: | |
| 1. Upload a video file containing road scenes | |
| 2. Select detection method: | |
| - For fastest: Use **Basic Standard** | |
| - For curves: Use **Basic Segmented**, **UFLD**, or **Advanced** | |
| - For best accuracy: Use **SCNN** or **Advanced + Enhanced** | |
| - For multi-color lanes: Use **YOLOP** | |
| 3. Click "Process Video" button and monitor the progress bar | |
| 4. The system will process each frame and create a side-by-side comparison | |
| 5. Download the result video showing original and detected lanes | |
| ### Tips: | |
| - **Basic Standard/Segmented**: Fastest, good for straight or gentle curves | |
| - **YOLOP**: Best for detecting both white and yellow lanes | |
| - **UFLD**: Excellent balance of speed and accuracy | |
| - **Advanced + Enhanced**: Best for dashed and curved lanes | |
| - **SCNN**: Best overall accuracy for complex road conditions | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch() | |