Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,15 +13,8 @@ def draw_lines(image, hough_lines, height):
|
|
| 13 |
if hough_lines is None:
|
| 14 |
return image
|
| 15 |
|
| 16 |
-
|
| 17 |
-
filtered_lines = []
|
| 18 |
-
for line in hough_lines:
|
| 19 |
-
x1, y1, x2, y2 = line[0]
|
| 20 |
-
# Only keep lines where the lowest point (max of y1, y2) is within a certain range from the bottom
|
| 21 |
-
if max(y1, y2) > height * 0.6: # Focus on the lower 40% of the image
|
| 22 |
-
filtered_lines.append(line)
|
| 23 |
|
| 24 |
-
# Draw the filtered lines
|
| 25 |
for line in filtered_lines:
|
| 26 |
x1, y1, x2, y2 = line[0]
|
| 27 |
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
@@ -29,12 +22,11 @@ def draw_lines(image, hough_lines, height):
|
|
| 29 |
|
| 30 |
def process(img):
|
| 31 |
height, width = img.shape[:2]
|
| 32 |
-
# Adjust ROI to focus on a smaller area closer to the vehicle
|
| 33 |
roi_vertices = np.array([
|
| 34 |
-
(width * 0.1, height),
|
| 35 |
-
(width * 0.4, height * 0.6),
|
| 36 |
-
(width * 0.6, height * 0.6),
|
| 37 |
-
(width * 0.9, height)
|
| 38 |
], np.int32)
|
| 39 |
|
| 40 |
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
@@ -42,20 +34,11 @@ def process(img):
|
|
| 42 |
edges = cv2.Canny(blurred_img, 50, 150)
|
| 43 |
roi_img = roi(edges, roi_vertices)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
lines = cv2.HoughLinesP(
|
| 47 |
-
roi_img,
|
| 48 |
-
rho=1,
|
| 49 |
-
theta=np.pi / 180,
|
| 50 |
-
threshold=50,
|
| 51 |
-
minLineLength=30,
|
| 52 |
-
maxLineGap=50
|
| 53 |
-
)
|
| 54 |
|
| 55 |
return draw_lines(img, lines, height) if lines is not None else img
|
| 56 |
|
| 57 |
def lane_detection(video_path):
|
| 58 |
-
# Check if the input video path is valid
|
| 59 |
if not os.path.exists(video_path):
|
| 60 |
return "Error: Video file not found."
|
| 61 |
|
|
@@ -65,56 +48,34 @@ def lane_detection(video_path):
|
|
| 65 |
|
| 66 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 67 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 68 |
-
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 69 |
|
| 70 |
-
# Create a temporary file for the processed video
|
| 71 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 72 |
output_path = temp_file.name
|
| 73 |
temp_file.close()
|
| 74 |
|
| 75 |
-
|
| 76 |
-
try:
|
| 77 |
-
fourcc = cv2.VideoWriter_fourcc(*"avc1")
|
| 78 |
-
except:
|
| 79 |
-
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 80 |
-
|
| 81 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 82 |
if not out.isOpened():
|
| 83 |
return "Error: Could not create output video file."
|
| 84 |
|
| 85 |
-
# Process each frame
|
| 86 |
while cap.isOpened():
|
| 87 |
ret, frame = cap.read()
|
| 88 |
if not ret:
|
| 89 |
break
|
| 90 |
-
|
| 91 |
processed_frame = process(frame)
|
| 92 |
out.write(processed_frame)
|
| 93 |
|
| 94 |
cap.release()
|
| 95 |
out.release()
|
| 96 |
|
| 97 |
-
|
| 98 |
-
if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
|
| 99 |
-
return "Error: Output video file was not created or is empty."
|
| 100 |
-
|
| 101 |
-
# Return only the processed video path
|
| 102 |
-
return output_path
|
| 103 |
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
gr.
|
| 107 |
-
gr.
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
video_input = gr.Video(label="Original Video", interactive=True)
|
| 111 |
-
video_output = gr.Video(label="Processed Video")
|
| 112 |
-
|
| 113 |
-
submit_btn = gr.Button("Submit")
|
| 114 |
-
submit_btn.click(
|
| 115 |
-
fn=lane_detection,
|
| 116 |
-
inputs=video_input,
|
| 117 |
-
outputs=video_output
|
| 118 |
-
)
|
| 119 |
|
| 120 |
-
|
|
|
|
| 13 |
if hough_lines is None:
|
| 14 |
return image
|
| 15 |
|
| 16 |
+
filtered_lines = [line for line in hough_lines if max(line[0][1], line[0][3]) > height * 0.6]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
| 18 |
for line in filtered_lines:
|
| 19 |
x1, y1, x2, y2 = line[0]
|
| 20 |
cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
|
|
| 22 |
|
| 23 |
def process(img):
|
| 24 |
height, width = img.shape[:2]
|
|
|
|
| 25 |
roi_vertices = np.array([
|
| 26 |
+
(width * 0.1, height),
|
| 27 |
+
(width * 0.4, height * 0.6),
|
| 28 |
+
(width * 0.6, height * 0.6),
|
| 29 |
+
(width * 0.9, height)
|
| 30 |
], np.int32)
|
| 31 |
|
| 32 |
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
| 34 |
edges = cv2.Canny(blurred_img, 50, 150)
|
| 35 |
roi_img = roi(edges, roi_vertices)
|
| 36 |
|
| 37 |
+
lines = cv2.HoughLinesP(roi_img, 1, np.pi / 180, 50, minLineLength=30, maxLineGap=50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
return draw_lines(img, lines, height) if lines is not None else img
|
| 40 |
|
| 41 |
def lane_detection(video_path):
|
|
|
|
| 42 |
if not os.path.exists(video_path):
|
| 43 |
return "Error: Video file not found."
|
| 44 |
|
|
|
|
| 48 |
|
| 49 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 50 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 51 |
+
fps = max(1, int(cap.get(cv2.CAP_PROP_FPS))) # Avoid division by zero
|
| 52 |
|
|
|
|
| 53 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 54 |
output_path = temp_file.name
|
| 55 |
temp_file.close()
|
| 56 |
|
| 57 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Use mp4v for broader compatibility
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
| 59 |
if not out.isOpened():
|
| 60 |
return "Error: Could not create output video file."
|
| 61 |
|
|
|
|
| 62 |
while cap.isOpened():
|
| 63 |
ret, frame = cap.read()
|
| 64 |
if not ret:
|
| 65 |
break
|
|
|
|
| 66 |
processed_frame = process(frame)
|
| 67 |
out.write(processed_frame)
|
| 68 |
|
| 69 |
cap.release()
|
| 70 |
out.release()
|
| 71 |
|
| 72 |
+
return output_path if os.path.exists(output_path) and os.path.getsize(output_path) > 0 else "Error: Output video file was not created."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
demo = gr.Interface(
|
| 75 |
+
fn=lane_detection,
|
| 76 |
+
inputs=gr.Video(label="Original Video"),
|
| 77 |
+
outputs=gr.Video(label="Processed Video"),
|
| 78 |
+
allow_flagging="never"
|
| 79 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
demo.launch()
|