Self driving cars
Pak Army FanPage Muslim World League - رابطة العالم الإسلامي
𝐒𝐞𝐥𝐟 𝐝𝐫𝐢𝐯𝐢𝐧𝐠 𝐜𝐚𝐫𝐬
import cv2
import numpy as np
def region_of_interest(img, vertices):
mask = np.zeros_like(img)
cv2.fillPoly(mask, vertices, 255)
masked_image = cv2.bitwise_and(img, mask)
return masked_image
def draw_lines(img, lines):
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (255, 0, 0), 10)
def process_frame(frame):
height, width = frame.shape[:2]
region_vertices = [(0, height), (width // 2, height // 2), (width, height)]
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
edges = cv2.Canny(blur, 50, 150)
cropped_edges = region_of_interest(edges, np.array([region_vertices], np.int32))
lines = cv2.HoughLinesP(cropped_edges, 2, np.pi / 180, 100, minLineLength=40, maxLineGap=5)
if lines is not None:
draw_lines(frame, lines)
return frame
# Initialize Video Capture (e.g., camera or video file)
cap = cv2.VideoCapture("test_video.mp4") # Replace with 0 for webcam
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
processed_frame = process_frame(frame)
cv2.imshow("Self-Driving Car Simulation", processed_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Comments
Post a Comment