Spaces:
Sleeping
Sleeping
copilot-swe-agent[bot]
kr4phy
commited on
Commit
Β·
e40d5b1
1
Parent(s):
2b342d6
Refactor code into modular structure with separate lane detection module
Browse files- app.py +8 -152
- create_test_video.py +66 -0
- lane_detection.py +165 -0
- test_lane_detection.py +27 -10
app.py
CHANGED
|
@@ -1,172 +1,28 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
import tempfile
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def region_of_interest(img, vertices):
|
| 9 |
-
"""
|
| 10 |
-
Apply a region of interest mask to the image.
|
| 11 |
-
"""
|
| 12 |
-
mask = np.zeros_like(img)
|
| 13 |
-
cv2.fillPoly(mask, vertices, 255)
|
| 14 |
-
masked_image = cv2.bitwise_and(img, mask)
|
| 15 |
-
return masked_image
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def draw_lines(img, lines, color=[0, 255, 0], thickness=3):
|
| 19 |
-
"""
|
| 20 |
-
Draw lines on the image.
|
| 21 |
-
"""
|
| 22 |
-
if lines is None:
|
| 23 |
-
return img
|
| 24 |
-
|
| 25 |
-
line_img = np.zeros_like(img)
|
| 26 |
-
|
| 27 |
-
# Separate left and right lane lines
|
| 28 |
-
left_lines = []
|
| 29 |
-
right_lines = []
|
| 30 |
-
|
| 31 |
-
for line in lines:
|
| 32 |
-
x1, y1, x2, y2 = line[0]
|
| 33 |
-
if x2 == x1:
|
| 34 |
-
continue
|
| 35 |
-
slope = (y2 - y1) / (x2 - x1)
|
| 36 |
-
|
| 37 |
-
# Filter by slope to separate left and right lanes
|
| 38 |
-
if slope < -0.5: # Left lane (negative slope)
|
| 39 |
-
left_lines.append(line[0])
|
| 40 |
-
elif slope > 0.5: # Right lane (positive slope)
|
| 41 |
-
right_lines.append(line[0])
|
| 42 |
-
|
| 43 |
-
# Average lines for left and right lanes
|
| 44 |
-
def average_lines(lines, img_shape):
|
| 45 |
-
if len(lines) == 0:
|
| 46 |
-
return None
|
| 47 |
-
|
| 48 |
-
x_coords = []
|
| 49 |
-
y_coords = []
|
| 50 |
-
|
| 51 |
-
for line in lines:
|
| 52 |
-
x1, y1, x2, y2 = line
|
| 53 |
-
x_coords.extend([x1, x2])
|
| 54 |
-
y_coords.extend([y1, y2])
|
| 55 |
-
|
| 56 |
-
# Fit a polynomial to the points
|
| 57 |
-
poly = np.polyfit(y_coords, x_coords, 1)
|
| 58 |
-
|
| 59 |
-
# Calculate line endpoints
|
| 60 |
-
y1 = img_shape[0]
|
| 61 |
-
y2 = int(img_shape[0] * 0.6)
|
| 62 |
-
x1 = int(poly[0] * y1 + poly[1])
|
| 63 |
-
x2 = int(poly[0] * y2 + poly[1])
|
| 64 |
-
|
| 65 |
-
return [x1, y1, x2, y2]
|
| 66 |
-
|
| 67 |
-
# Draw averaged lines
|
| 68 |
-
left_line = average_lines(left_lines, img.shape)
|
| 69 |
-
right_line = average_lines(right_lines, img.shape)
|
| 70 |
-
|
| 71 |
-
if left_line is not None:
|
| 72 |
-
cv2.line(line_img, (left_line[0], left_line[1]), (left_line[2], left_line[3]), color, thickness)
|
| 73 |
-
|
| 74 |
-
if right_line is not None:
|
| 75 |
-
cv2.line(line_img, (right_line[0], right_line[1]), (right_line[2], right_line[3]), color, thickness)
|
| 76 |
-
|
| 77 |
-
return cv2.addWeighted(img, 1.0, line_img, 1.0, 0)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
def process_frame(frame):
|
| 81 |
-
"""
|
| 82 |
-
Process a single frame for lane detection.
|
| 83 |
-
"""
|
| 84 |
-
height, width = frame.shape[:2]
|
| 85 |
-
|
| 86 |
-
# Convert to grayscale
|
| 87 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 88 |
-
|
| 89 |
-
# Apply Gaussian blur
|
| 90 |
-
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 91 |
-
|
| 92 |
-
# Apply Canny edge detection
|
| 93 |
-
edges = cv2.Canny(blur, 50, 150)
|
| 94 |
-
|
| 95 |
-
# Define region of interest (ROI)
|
| 96 |
-
vertices = np.array([[
|
| 97 |
-
(int(width * 0.1), height),
|
| 98 |
-
(int(width * 0.45), int(height * 0.6)),
|
| 99 |
-
(int(width * 0.55), int(height * 0.6)),
|
| 100 |
-
(int(width * 0.9), height)
|
| 101 |
-
]], dtype=np.int32)
|
| 102 |
-
|
| 103 |
-
# Apply ROI mask
|
| 104 |
-
masked_edges = region_of_interest(edges, vertices)
|
| 105 |
-
|
| 106 |
-
# Apply Hough transform to detect lines
|
| 107 |
-
lines = cv2.HoughLinesP(
|
| 108 |
-
masked_edges,
|
| 109 |
-
rho=2,
|
| 110 |
-
theta=np.pi / 180,
|
| 111 |
-
threshold=50,
|
| 112 |
-
minLineLength=40,
|
| 113 |
-
maxLineGap=100
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
# Draw detected lanes on the original frame
|
| 117 |
-
result = draw_lines(frame.copy(), lines)
|
| 118 |
-
|
| 119 |
-
return result
|
| 120 |
|
| 121 |
|
| 122 |
def process_video(video_path):
|
| 123 |
"""
|
| 124 |
Process the uploaded video and return side-by-side comparison.
|
|
|
|
| 125 |
"""
|
| 126 |
if video_path is None:
|
| 127 |
return None
|
| 128 |
|
| 129 |
-
# Open the video
|
| 130 |
-
cap = cv2.VideoCapture(video_path)
|
| 131 |
-
|
| 132 |
-
if not cap.isOpened():
|
| 133 |
-
return None
|
| 134 |
-
|
| 135 |
-
# Get video properties
|
| 136 |
-
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 137 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 138 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 139 |
-
|
| 140 |
# Create temporary output file
|
| 141 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 142 |
output_path = temp_output.name
|
| 143 |
temp_output.close()
|
| 144 |
|
| 145 |
-
#
|
| 146 |
-
|
| 147 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (width * 2, height))
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
break
|
| 154 |
-
|
| 155 |
-
# Process frame for lane detection
|
| 156 |
-
processed_frame = process_frame(frame)
|
| 157 |
-
|
| 158 |
-
# Create side-by-side comparison
|
| 159 |
-
# Original on left, processed on right
|
| 160 |
-
combined = np.hstack((frame, processed_frame))
|
| 161 |
-
|
| 162 |
-
# Write the combined frame
|
| 163 |
-
out.write(combined)
|
| 164 |
-
|
| 165 |
-
# Release resources
|
| 166 |
-
cap.release()
|
| 167 |
-
out.release()
|
| 168 |
-
|
| 169 |
-
return output_path
|
| 170 |
|
| 171 |
|
| 172 |
# Create Gradio interface
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tempfile
|
| 3 |
+
from lane_detection import process_video as process_video_file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def process_video(video_path):
|
| 7 |
"""
|
| 8 |
Process the uploaded video and return side-by-side comparison.
|
| 9 |
+
Wrapper function for Gradio interface.
|
| 10 |
"""
|
| 11 |
if video_path is None:
|
| 12 |
return None
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Create temporary output file
|
| 15 |
temp_output = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 16 |
output_path = temp_output.name
|
| 17 |
temp_output.close()
|
| 18 |
|
| 19 |
+
# Process the video
|
| 20 |
+
success = process_video_file(video_path, output_path)
|
|
|
|
| 21 |
|
| 22 |
+
if success:
|
| 23 |
+
return output_path
|
| 24 |
+
else:
|
| 25 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
# Create Gradio interface
|
create_test_video.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Create a simple test video with lane-like features for testing
|
| 3 |
+
"""
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
def create_test_video(output_path, duration_sec=5, fps=30):
|
| 9 |
+
"""
|
| 10 |
+
Create a test video with simulated road and lanes
|
| 11 |
+
"""
|
| 12 |
+
width, height = 640, 480
|
| 13 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 14 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 15 |
+
|
| 16 |
+
total_frames = duration_sec * fps
|
| 17 |
+
|
| 18 |
+
for frame_idx in range(total_frames):
|
| 19 |
+
# Create dark gray background (asphalt)
|
| 20 |
+
frame = np.ones((height, width, 3), dtype=np.uint8) * 50
|
| 21 |
+
|
| 22 |
+
# Add some texture/noise for realism
|
| 23 |
+
noise = np.random.randint(0, 20, (height, width, 3), dtype=np.uint8)
|
| 24 |
+
frame = cv2.add(frame, noise)
|
| 25 |
+
|
| 26 |
+
# Draw road perspective trapezoid
|
| 27 |
+
road_points = np.array([
|
| 28 |
+
[int(width * 0.1), height],
|
| 29 |
+
[int(width * 0.4), int(height * 0.5)],
|
| 30 |
+
[int(width * 0.6), int(height * 0.5)],
|
| 31 |
+
[int(width * 0.9), height]
|
| 32 |
+
], dtype=np.int32)
|
| 33 |
+
cv2.fillPoly(frame, [road_points], (60, 60, 60))
|
| 34 |
+
|
| 35 |
+
# Calculate lane positions with slight animation
|
| 36 |
+
offset = int(10 * np.sin(frame_idx / 10))
|
| 37 |
+
|
| 38 |
+
# Left lane
|
| 39 |
+
left_bottom = (int(width * 0.3) + offset, height)
|
| 40 |
+
left_top = (int(width * 0.45) + offset, int(height * 0.6))
|
| 41 |
+
cv2.line(frame, left_bottom, left_top, (255, 255, 255), 3)
|
| 42 |
+
|
| 43 |
+
# Right lane
|
| 44 |
+
right_bottom = (int(width * 0.7) + offset, height)
|
| 45 |
+
right_top = (int(width * 0.55) + offset, int(height * 0.6))
|
| 46 |
+
cv2.line(frame, right_bottom, right_top, (255, 255, 255), 3)
|
| 47 |
+
|
| 48 |
+
# Add dashed center line
|
| 49 |
+
for y in range(height, int(height * 0.6), -30):
|
| 50 |
+
if (y // 30) % 2 == 0:
|
| 51 |
+
x_start = int(width * 0.5) + offset
|
| 52 |
+
x_end = int(width * 0.5) + offset
|
| 53 |
+
cv2.line(frame, (x_start, y), (x_end, y - 20), (255, 255, 0), 2)
|
| 54 |
+
|
| 55 |
+
# Write frame
|
| 56 |
+
out.write(frame)
|
| 57 |
+
|
| 58 |
+
out.release()
|
| 59 |
+
print(f"β Test video created: {output_path}")
|
| 60 |
+
print(f" Duration: {duration_sec}s, FPS: {fps}, Frames: {total_frames}")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
# Create test video in /tmp
|
| 65 |
+
output_path = "/tmp/test_road_video.mp4"
|
| 66 |
+
create_test_video(output_path, duration_sec=3, fps=30)
|
lane_detection.py
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Lane detection module using OpenCV
|
| 3 |
+
This module contains the core lane detection logic without UI dependencies.
|
| 4 |
+
"""
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def region_of_interest(img, vertices):
|
| 10 |
+
"""
|
| 11 |
+
Apply a region of interest mask to the image.
|
| 12 |
+
"""
|
| 13 |
+
mask = np.zeros_like(img)
|
| 14 |
+
cv2.fillPoly(mask, vertices, 255)
|
| 15 |
+
masked_image = cv2.bitwise_and(img, mask)
|
| 16 |
+
return masked_image
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def draw_lines(img, lines, color=[0, 255, 0], thickness=3):
|
| 20 |
+
"""
|
| 21 |
+
Draw lines on the image.
|
| 22 |
+
"""
|
| 23 |
+
if lines is None:
|
| 24 |
+
return img
|
| 25 |
+
|
| 26 |
+
line_img = np.zeros_like(img)
|
| 27 |
+
|
| 28 |
+
# Separate left and right lane lines
|
| 29 |
+
left_lines = []
|
| 30 |
+
right_lines = []
|
| 31 |
+
|
| 32 |
+
for line in lines:
|
| 33 |
+
x1, y1, x2, y2 = line[0]
|
| 34 |
+
if x2 == x1:
|
| 35 |
+
continue
|
| 36 |
+
slope = (y2 - y1) / (x2 - x1)
|
| 37 |
+
|
| 38 |
+
# Filter by slope to separate left and right lanes
|
| 39 |
+
if slope < -0.5: # Left lane (negative slope)
|
| 40 |
+
left_lines.append(line[0])
|
| 41 |
+
elif slope > 0.5: # Right lane (positive slope)
|
| 42 |
+
right_lines.append(line[0])
|
| 43 |
+
|
| 44 |
+
# Average lines for left and right lanes
|
| 45 |
+
def average_lines(lines, img_shape):
|
| 46 |
+
if len(lines) == 0:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
x_coords = []
|
| 50 |
+
y_coords = []
|
| 51 |
+
|
| 52 |
+
for line in lines:
|
| 53 |
+
x1, y1, x2, y2 = line
|
| 54 |
+
x_coords.extend([x1, x2])
|
| 55 |
+
y_coords.extend([y1, y2])
|
| 56 |
+
|
| 57 |
+
# Fit a polynomial to the points
|
| 58 |
+
poly = np.polyfit(y_coords, x_coords, 1)
|
| 59 |
+
|
| 60 |
+
# Calculate line endpoints
|
| 61 |
+
y1 = img_shape[0]
|
| 62 |
+
y2 = int(img_shape[0] * 0.6)
|
| 63 |
+
x1 = int(poly[0] * y1 + poly[1])
|
| 64 |
+
x2 = int(poly[0] * y2 + poly[1])
|
| 65 |
+
|
| 66 |
+
return [x1, y1, x2, y2]
|
| 67 |
+
|
| 68 |
+
# Draw averaged lines
|
| 69 |
+
left_line = average_lines(left_lines, img.shape)
|
| 70 |
+
right_line = average_lines(right_lines, img.shape)
|
| 71 |
+
|
| 72 |
+
if left_line is not None:
|
| 73 |
+
cv2.line(line_img, (left_line[0], left_line[1]), (left_line[2], left_line[3]), color, thickness)
|
| 74 |
+
|
| 75 |
+
if right_line is not None:
|
| 76 |
+
cv2.line(line_img, (right_line[0], right_line[1]), (right_line[2], right_line[3]), color, thickness)
|
| 77 |
+
|
| 78 |
+
return cv2.addWeighted(img, 1.0, line_img, 1.0, 0)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def process_frame(frame):
|
| 82 |
+
"""
|
| 83 |
+
Process a single frame for lane detection.
|
| 84 |
+
"""
|
| 85 |
+
height, width = frame.shape[:2]
|
| 86 |
+
|
| 87 |
+
# Convert to grayscale
|
| 88 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 89 |
+
|
| 90 |
+
# Apply Gaussian blur
|
| 91 |
+
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 92 |
+
|
| 93 |
+
# Apply Canny edge detection
|
| 94 |
+
edges = cv2.Canny(blur, 50, 150)
|
| 95 |
+
|
| 96 |
+
# Define region of interest (ROI)
|
| 97 |
+
vertices = np.array([[
|
| 98 |
+
(int(width * 0.1), height),
|
| 99 |
+
(int(width * 0.45), int(height * 0.6)),
|
| 100 |
+
(int(width * 0.55), int(height * 0.6)),
|
| 101 |
+
(int(width * 0.9), height)
|
| 102 |
+
]], dtype=np.int32)
|
| 103 |
+
|
| 104 |
+
# Apply ROI mask
|
| 105 |
+
masked_edges = region_of_interest(edges, vertices)
|
| 106 |
+
|
| 107 |
+
# Apply Hough transform to detect lines
|
| 108 |
+
lines = cv2.HoughLinesP(
|
| 109 |
+
masked_edges,
|
| 110 |
+
rho=2,
|
| 111 |
+
theta=np.pi / 180,
|
| 112 |
+
threshold=50,
|
| 113 |
+
minLineLength=40,
|
| 114 |
+
maxLineGap=100
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Draw detected lanes on the original frame
|
| 118 |
+
result = draw_lines(frame.copy(), lines)
|
| 119 |
+
|
| 120 |
+
return result
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def process_video(input_path, output_path):
|
| 124 |
+
"""
|
| 125 |
+
Process the video and create side-by-side comparison.
|
| 126 |
+
Returns True if successful, False otherwise.
|
| 127 |
+
"""
|
| 128 |
+
# Open the video
|
| 129 |
+
cap = cv2.VideoCapture(input_path)
|
| 130 |
+
|
| 131 |
+
if not cap.isOpened():
|
| 132 |
+
return False
|
| 133 |
+
|
| 134 |
+
# Get video properties
|
| 135 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 136 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 137 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 138 |
+
|
| 139 |
+
# Video writer for output (side-by-side, so width is doubled)
|
| 140 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 141 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width * 2, height))
|
| 142 |
+
|
| 143 |
+
frame_count = 0
|
| 144 |
+
# Process each frame
|
| 145 |
+
while True:
|
| 146 |
+
ret, frame = cap.read()
|
| 147 |
+
if not ret:
|
| 148 |
+
break
|
| 149 |
+
|
| 150 |
+
# Process frame for lane detection
|
| 151 |
+
processed_frame = process_frame(frame)
|
| 152 |
+
|
| 153 |
+
# Create side-by-side comparison
|
| 154 |
+
# Original on left, processed on right
|
| 155 |
+
combined = np.hstack((frame, processed_frame))
|
| 156 |
+
|
| 157 |
+
# Write the combined frame
|
| 158 |
+
out.write(combined)
|
| 159 |
+
frame_count += 1
|
| 160 |
+
|
| 161 |
+
# Release resources
|
| 162 |
+
cap.release()
|
| 163 |
+
out.release()
|
| 164 |
+
|
| 165 |
+
return frame_count > 0
|
test_lane_detection.py
CHANGED
|
@@ -9,7 +9,7 @@ import os
|
|
| 9 |
# Add parent directory to path
|
| 10 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 11 |
|
| 12 |
-
from
|
| 13 |
|
| 14 |
|
| 15 |
def test_region_of_interest():
|
|
@@ -52,8 +52,9 @@ def test_process_frame():
|
|
| 52 |
# Check that result has correct shape
|
| 53 |
assert result.shape == frame.shape, f"Expected shape {frame.shape}, got {result.shape}"
|
| 54 |
|
| 55 |
-
# Check that result is
|
| 56 |
-
assert not
|
|
|
|
| 57 |
|
| 58 |
print("β process_frame test passed")
|
| 59 |
|
|
@@ -62,13 +63,6 @@ def test_imports():
|
|
| 62 |
"""Test that all required modules can be imported"""
|
| 63 |
print("Testing imports...")
|
| 64 |
|
| 65 |
-
try:
|
| 66 |
-
import gradio
|
| 67 |
-
print("β gradio imported successfully")
|
| 68 |
-
except ImportError as e:
|
| 69 |
-
print(f"β Failed to import gradio: {e}")
|
| 70 |
-
return False
|
| 71 |
-
|
| 72 |
try:
|
| 73 |
import cv2
|
| 74 |
print("β opencv-python imported successfully")
|
|
@@ -86,6 +80,28 @@ def test_imports():
|
|
| 86 |
return True
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
if __name__ == "__main__":
|
| 90 |
print("Running lane detection tests...\n")
|
| 91 |
|
|
@@ -100,6 +116,7 @@ if __name__ == "__main__":
|
|
| 100 |
try:
|
| 101 |
test_region_of_interest()
|
| 102 |
test_process_frame()
|
|
|
|
| 103 |
print("\nβ
All tests passed!")
|
| 104 |
except Exception as e:
|
| 105 |
print(f"\nβ Test failed: {e}")
|
|
|
|
| 9 |
# Add parent directory to path
|
| 10 |
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 11 |
|
| 12 |
+
from lane_detection import region_of_interest, process_frame, process_video
|
| 13 |
|
| 14 |
|
| 15 |
def test_region_of_interest():
|
|
|
|
| 52 |
# Check that result has correct shape
|
| 53 |
assert result.shape == frame.shape, f"Expected shape {frame.shape}, got {result.shape}"
|
| 54 |
|
| 55 |
+
# Check that result is a valid image (not None and correct dtype)
|
| 56 |
+
assert result is not None, "Result should not be None"
|
| 57 |
+
assert result.dtype == np.uint8, "Result should be uint8 type"
|
| 58 |
|
| 59 |
print("β process_frame test passed")
|
| 60 |
|
|
|
|
| 63 |
"""Test that all required modules can be imported"""
|
| 64 |
print("Testing imports...")
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
try:
|
| 67 |
import cv2
|
| 68 |
print("β opencv-python imported successfully")
|
|
|
|
| 80 |
return True
|
| 81 |
|
| 82 |
|
| 83 |
+
def test_video_processing():
|
| 84 |
+
"""Test complete video processing"""
|
| 85 |
+
print("Testing video processing...")
|
| 86 |
+
|
| 87 |
+
from create_test_video import create_test_video
|
| 88 |
+
|
| 89 |
+
# Create test video
|
| 90 |
+
input_path = "/tmp/test_input.mp4"
|
| 91 |
+
output_path = "/tmp/test_output.mp4"
|
| 92 |
+
|
| 93 |
+
create_test_video(input_path, duration_sec=1, fps=10)
|
| 94 |
+
|
| 95 |
+
# Process video
|
| 96 |
+
success = process_video(input_path, output_path)
|
| 97 |
+
|
| 98 |
+
assert success, "Video processing should succeed"
|
| 99 |
+
assert os.path.exists(output_path), "Output file should exist"
|
| 100 |
+
assert os.path.getsize(output_path) > 0, "Output file should not be empty"
|
| 101 |
+
|
| 102 |
+
print("β video processing test passed")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
if __name__ == "__main__":
|
| 106 |
print("Running lane detection tests...\n")
|
| 107 |
|
|
|
|
| 116 |
try:
|
| 117 |
test_region_of_interest()
|
| 118 |
test_process_frame()
|
| 119 |
+
test_video_processing()
|
| 120 |
print("\nβ
All tests passed!")
|
| 121 |
except Exception as e:
|
| 122 |
print(f"\nβ Test failed: {e}")
|