Lane detection — computer vision in the browser
A classic computer-vision demo: detect road lane lines from a live camera. OpenCV Canny edges + a region-of-interest mask + cv2.HoughLinesP find the lane segments. Point the (rear) camera at a road, press Run, and allow the camera.
Example code (Python / OpenCV)
import cv2
import numpy as np
# Point the camera at a road and press Run.
# Tip: use the flip (⟳) button on the preview to switch to the REAR camera.
cap = cv2.VideoCapture(0)
def region_of_interest(edges):
# Keep only a trapezoid over the road ahead (lower-center of the frame).
h, w = edges.shape
mask = np.zeros_like(edges)
poly = np.array([[
(int(0.05 * w), h),
(int(0.45 * w), int(0.60 * h)),
(int(0.55 * w), int(0.60 * h)),
(int(0.95 * w), h),
]], dtype=np.int32)
cv2.fillPoly(mask, poly, 255)
return cv2.bitwise_and(edges, mask)
while True:
ret, frame = cap.read()
if not ret:
print("No camera frame — allow camera access when prompted.")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blur, 60, 150)
roi = region_of_interest(edges)
lines = cv2.HoughLinesP(roi, 1, np.pi / 180, threshold=40,
minLineLength=30, maxLineGap=60)
overlay = frame.copy()
if lines is not None:
for x1, y1, x2, y2 in lines[:, 0]:
if abs(y2 - y1) > 0.3 * abs(x2 - x1): # drop near-horizontal clutter
cv2.line(overlay, (x1, y1), (x2, y2), (0, 255, 0), 4)
cv2.imshow("lanes", overlay)
cv2.imshow("edges", roi)
if cv2.waitKey(1) == 27: # Esc / Stop
break