Spaces:
Sleeping
Sleeping
File size: 6,203 Bytes
2b342d6 3e26e16 e40d5b1 2b342d6 4ae3a76 2b342d6 4ae3a76 2b342d6 63ea6ab 3e26e16 e40d5b1 3e26e16 2b342d6 4ae3a76 63ea6ab 2b342d6 63ea6ab 4ae3a76 3235f6a 63ea6ab 4ae3a76 63ea6ab 2b342d6 63ea6ab 2b342d6 3e26e16 63ea6ab 4ae3a76 63ea6ab 2b342d6 63ea6ab 2b342d6 63ea6ab 2b342d6 63ea6ab 4ae3a76 63ea6ab 4ae3a76 63ea6ab 4ae3a76 63ea6ab 4ae3a76 63ea6ab 2b342d6 4ae3a76 63ea6ab 4ae3a76 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
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()
|