diff --git a/README.md b/README.md index 2d22336..cb63b7d 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ More information on contributing and the general code of conduct for discussion | QR Code with logo | [QR code with Logo](https://github.com/DhanushNehru/Python-Scripts/tree/master/QR%20with%20Logo) | QR Code Customization Feature | | Random Color Generator | [Random Color Generator](https://github.com/DhanushNehru/Python-Scripts/tree/master/Random%20Color%20Generator) | A random color generator that will show you the color and values! | | Remove Background | [Remove Background](https://github.com/DhanushNehru/Python-Scripts/tree/master/Remove%20Background) | Removes the background of images. | +| Road-Lane-Detection | [Road-Lane-Detection](https://github.com/NotIncorecc/Python-Scripts/tree/master/Road-Lane-Detection) | Detects the lanes of the road | | Rock Paper Scissor 1 | [Rock Paper Scissor 1](https://github.com/DhanushNehru/Python-Scripts/tree/master/Rock%20Paper%20Scissor%201) | A game of Rock Paper Scissors. | | Rock Paper Scissor 2 | [Rock Paper Scissor 2](https://github.com/DhanushNehru/Python-Scripts/tree/master/Rock%20Paper%20Scissor%202) | A new version game of Rock Paper Scissors. | | Run Then Notify | [Run Then Notify](https://github.com/DhanushNehru/Python-Scripts/tree/master/Run%20Then%20Notify) | Runs a slow command and emails you when it completes execution. | diff --git a/Road-Lane-Detection/README.md b/Road-Lane-Detection/README.md new file mode 100644 index 0000000..57c3d01 --- /dev/null +++ b/Road-Lane-Detection/README.md @@ -0,0 +1,18 @@ +# Road Lane Detection + +This project is a Python program that uses OpenCV to detect lanes on roads. Unlike other lane detection programs, this one dynamically calculates the mask based on the color of the road, making it more adaptable to different environments. + +## Features + +- **Dynamic Mask Calculation**: Adjusts the mask according to the road color. +- **Lane Detection**: Identifies and highlights lanes on the road. + +## Requirements + +- Python 3.x +- OpenCV + +## Acknowledgements + +- OpenCV documentation +- Various online tutorials and resources diff --git a/Road-Lane-Detection/dik2_1.py b/Road-Lane-Detection/dik2_1.py new file mode 100644 index 0000000..f2406d8 --- /dev/null +++ b/Road-Lane-Detection/dik2_1.py @@ -0,0 +1,84 @@ +import cv2 +import numpy as np + +def canny_edge_detection(frame): + gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) + blur = cv2.GaussianBlur(gray, (5, 5), 0) + edges = cv2.Canny(blur, 50, 150) + return edges + +def detect_road_region(frame): + hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) + + lower_gray = np.array([0, 0, 50]) # Lower bound for gray + upper_gray = np.array([180, 50, 200]) # Upper bound for gray + + mask = cv2.inRange(hsv, lower_gray, upper_gray) + + contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + + if contours: + largest_contour = max(contours, key=cv2.contourArea) + + epsilon = 0.02 * cv2.arcLength(largest_contour, True) + polygon = cv2.approxPolyDP(largest_contour, epsilon, True) + + road_mask = np.zeros_like(mask) + if len(polygon) >= 3: + cv2.fillPoly(road_mask, [polygon], 255) + + return road_mask + return None + +def hough_transform(masked_edges): + lines = cv2.HoughLinesP(masked_edges, 2, np.pi / 180, 100, np.array([]), minLineLength=40, maxLineGap=5) + return lines + +def display_lines(frame, lines): + line_image = np.zeros_like(frame) + if lines is not None: + for line in lines: + x1, y1, x2, y2 = line.reshape(4) + cv2.line(line_image, (x1, y1), (x2, y2), (0, 255, 0), 10) + return line_image + +def combine_images(frame, line_image): + combined_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1) + return combined_image + +def process_frame(frame): + edges = canny_edge_detection(frame) + + road_mask = detect_road_region(frame) + + if road_mask is not None: + masked_edges = cv2.bitwise_and(edges, edges, mask=road_mask) + else: + masked_edges = edges + + lines = hough_transform(masked_edges) + + line_image = display_lines(frame, lines) + + result = combine_images(frame, line_image) + + return result + +vds = ['lane5.mp4']#add more of your own videos here + +cap = cv2.VideoCapture('videos/'+vds[0]) +while(cap.isOpened()): + ret, frame = cap.read() + if not ret: + break + + result = process_frame(frame) + cv2.imshow('Lane Detection', result) + road_cont_mask = detect_road_region(frame) + cv2.imshow('Lane Detection2', road_cont_mask) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +cap.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/Road-Lane-Detection/requirements.txt b/Road-Lane-Detection/requirements.txt new file mode 100644 index 0000000..1db7aea --- /dev/null +++ b/Road-Lane-Detection/requirements.txt @@ -0,0 +1 @@ +opencv-python \ No newline at end of file diff --git a/Road-Lane-Detection/videos/lane5.mp4 b/Road-Lane-Detection/videos/lane5.mp4 new file mode 100644 index 0000000..b60d11c Binary files /dev/null and b/Road-Lane-Detection/videos/lane5.mp4 differ