This repository show a Lane Detection pipeline part of a Lane Departure Warning System, through the implementation of a computer vision pipeline strategy to detect lane lines in roads. The real gain is that the algorithm parameters can be calibrated on the fly, making it possible to do fine-tuning, and map supervised different image/video datasets.
- Make sure you have Python installed on your system. You can download Python from the official website: Python Downloads.
- Also make sure you have git installed in your machine. You can download git here: https://git-scm.com/downloads
- Open your terminal or command prompt and navigate to the directory where you want to clone this repository. Then run the following command:
# Navigate to the directory where you want to create your project
git clone https://github.com/JCGCosta/LaneDetector.git# Navigate to the directory where you cloned the repository
cd repository/directory/path
# Create a virtual environment named 'venv'
python -m venv venv
# Activate the virtual environment (If you are on Windows)
.\venv\Scripts\activate
# Activate the virtual environment (If you are on Linux)
source venv/bin/activate
# Now inside the environment install the libraries from the requirements.txt
pip install -r requirements.txt- To make your own image processing pipeline you must edit the
pipeline_sample.pyfile, which must follow the schema.
{
"Operation": {
"path": "path/to/your/file.py",
"function": "function name to be called",
"parameters": {
"parameter1": [
127, # The initial Value of the parameter
255 # The maximum value of the parameter, which can be changed by the user
],...
}
},...
}- The operations are executed in order, and will receive the image as cv2.Mat of size (width, height, 3) and the parameters defined in the pipeline file, if you are confused about it take a look in the Source/ImageProcessing folder, where there is some examples.
- Before running make sure you have the
pipeline_sample.jsonfile configured with the correct path of your python file from your computer. - As a sample the ImageProcessing folder inside Source already have a full pipeline with 5 operations for the Lane Detection.
- ATTENTION: Also, please change in the three testing.py files the
pipeline.jsontopipeline_sample.jsonin the LaneDetector class instantiation, so it will use your pipeline file.
- To run the application, you can choose between three options:
- Run the
test_image.pyfile with a image file path as the first argument directly, which will run the pipeline with the default parameters. - Run the
test_video.pyfile with a video file path as the first argument, which will run the pipeline on the video. - Run the
test_camera.pyfile, which will run the pipeline on the camera video.
- Run the
# Navigate to the main directory
cd repository/directory/path
# Run the test_image.py with an image file path
python test_image.py path/to/your/image.jpg
# Run the test_video.py with a video file path
python test_video.py path/to/your/video.mp4
# Run the test_camera.py to run the pipeline on the camera video
python test_camera.py- To build the Lane Detection Object, you can learn by exploring the testing.py files.
- Some functionalities that can be toggled on by adjusting the
test_video.pyfor example:LD.setup_record(get_video_resolution(cap), output_path=f'{filepath.split(".")[0]}_output.avi', record_fps=60)to record the video with the lane detection.record_fpsis the frames per second of the recorded video, adjust this accord with the fps you can achieve.
LD.frame_processor(frame, frame_skip=5)to process the frame with the lane detection pipeline.frame_skipis used to skip frames for performance, you can adjust it according to your needs.
LD.stop_recording()to stop the recording.
import cv2
from Source.LaneDetector import LaneDetector
from Source.Utils import get_video_resolution
import time, sys
filepath = r"Resources/videos/test2.mp4"
if len(sys.argv) > 1:
filepath = sys.argv[1]
cap = cv2.VideoCapture(filepath)
LD = LaneDetector("pipeline.json", controls=True, controls_resolution=(650, 785))
#LD.setup_record(get_video_resolution(cap), output_path=f'{filepath.split(".")[0]}_output.avi', record_fps=60)
while cap.isOpened():
start = time.time()
ret, frame = cap.read()
if not ret:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
continue
output_frame = LD.frame_processor(frame, frame_skip=5)
end = time.time()
totalTime = end - start
fps = 1 / totalTime if totalTime > 0 else 0
cv2.putText(output_frame, f'FPS: {int(fps)}', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2)
cv2.imshow("Lane Detection", output_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
LD.stop_recording()
break
cap.release()
cv2.destroyAllWindows()© Julio César Guimarães Costa 2023. All rights reserved.
