AI B.Tech Advanced

Autonomous Lane Detection Car Using Raspberry Pi and OpenCV - Part 1

In this project article we are going to build a Lane Detection Car using Raspberry Pi and OpenCV. Before proceeding with this project, make sure that you have the basic idea of how to use OpenCV using python in Raspberry Pi.

  • Suitable forB.Tech AI students
  • Build time4-6 weeks
  • LevelAdvanced
  • Available atAjvika Labs, Kakinada

About this project

In this project, we are going to build a Lane Detection Car using Raspberry Pi and OpenCV. Before proceeding with this project, make sure that you have a basic idea of how to use OpenCV using python in Raspberry Pi. The lane detection problem is typically framed as a semantic or instance segmentation problem to identify the pixels that belong to the lane class. We need some image processing techniques to implement lane detection on the Raspberry Pi. The OpenCV library has the required image processing techniques to perform lane detection. As the project is really big, I have decided to divide it into two parts. In this part, we will assemble the hardware and take the first drive of our car. You need to follow the steps which are mentioned below. Discover practical AI projects and tutorials designed to help beginners and makers build real-world artificial intelligence applications.

Steps required to make an Autonomous Lane Detection Car

I have divided the whole process into some easy steps that are mentioned below. In this part, we are going to work with the first four steps to drive our car. You need to go through every step very carefully because this seems to be easy while reading the steps but when you will implement those steps in a practical scenario, you will definitely be going to face some issues.

Installing the prerequisite software

Preparing the Driving Code

Detecting lane lines using OpenCV

Calibrating the Motors with the Lane Detection

So these are the steps you need to follow. Let’s discuss every step one by one. I hope you will not jump any step because it is required to follow every step very carefully. Before getting started with this section, make sure that you have the required components mentioned below. You can check out the Part 2 here.

Components required to make an Autonomous Lane Detection Car

To build the autonomous Lane detection car, make sure you have the following components.

I used my RC car which I purchased from Amazon. It has 3 DC motors (2 for throttling and 1 for steering). But this car cannot steer at a specific angle like servo-steering RC Cars.

I used the Raspberry Pi 4 as the main controlling unit of the car. You can use the Raspberry Pi 3 b+ but it may have some frame lagging issues.

Raspberry Pi Camera Module

It can record in 1080p at 30 frames per second, 720p at 60 frames per second, and 640x480p at 60/90 frames per second. Although it is not the ideal solution for image processing applications, it is enough for our purpose and is relatively inexpensive.

Camera Mounting Stand

I used a mounting stand because it is required to keep the Raspberry Pi camera at a precise angle to get a proper video feed of the complete track. I created a 3D model of the stand by using the 3D printer.

I have used the L298N motor driver in this project which is utilized to regulate DC motors' directions and speeds. It can operate two DC motors on a single board and can sustain 1.5A of current.

I have used a power bank to power up our Raspberry Pi. If you don’t have a power bank then you can use a 5V battery or you can build a power circuit to convert voltage into 5V with 2.2A current.

18650 2S Battery Pack

To power up our motor driver (L298N), I used 18650 2 Cell battery pack with a voltage of up to 7.4 Volts. Lithium-ion batteries are well-known for their outstanding performance in the realm of robotics and are utilized to supply electricity to the motor driver.

Black Chart Paper and White Tape

I used some black chart paper and white tape to prepare the track. Make sure that you are using the same technique to make the track because it will impact our Lane detection algorithm.

So, these all are the primary components to make your Lane detection Car. I am assuming that you have the common components like connecting wires, screw drivers, etc. which are required in almost every electronics project. Now, let’s understand the steps to make our Lane Detection Car.

Hardware Assembling of Autonomous Lane Detection Car

You need to prepare your car chassis. In my case, I used the RC car so I removed the circuitry, battery pack, and the upper header of the car as you can see in the image below.

Then I designed a mounting stand for the Raspberry Pi 4 in the Fusion 360. And I have already downloaded a mounting stand for the Raspberry Pi camera module. Then I set up the 3D printer to print both of the mounting stands one by one. It took me more than 3 hours to print both stands. In your case, it might be different.

Meanwhile, I have made the wiring connections between the three motors, the motor driver, and the Raspberry Pi 4. The connection is so simple. You can refer to the schematic and images below to make the wiring connection. Make sure that you have properly connected the motors that are used for the throttle with the one side of the motor driver and the motor that is used for the steering is connected to the opposite terminal of the motor driver.

Installing OpenCV on Raspberry Pi 4

Now you need to install the required libraries. I am assuming that you know the basic setup for the Raspberry Pi 4. You can follow the Raspberry Pi tutorials on our website . Before installing the OpenCV and other dependencies, Raspberry Pi needs to be fully updated. Use the below commands to update the Raspberry Pi to its latest version:

Then use the following commands to install the required dependencies for installing OpenCV on your Raspberry Pi.

After that, use the command given below to install the OpenCV on your Raspberry Pi.

Preparing the Driving Code

You can download the code from our Autonomous Car Github repository. There are three files under the folder named “ Lane Detection ”. In this part, we will be focusing on the “ motors.py ”, “ keyboardmodule.py ”, and the “ testdrivemodule.py ” files. By using these three files we control our car with keyboard inputs.

In the motors.py file, you will find all functions to control the motor directions and drive the motors forward or backward. We have imported the RPi. GPIO as GPIO to control the GPIO pins of the Raspberry Pi 4.

The following functions are used to drive the car into the “ forward ”, “ Backward ”, “ forward left ”, “ forward right ”, “ backward right ”, “ backward left ” direction. The stop function can be used to stop the car.

“keyboardmodule.py” file:

The keyboardmodule.py consists of the getKey() function to get the pressed key of the keyboard. I used the pygame library to get the pressed key of the keyboard on run time.

“tesdrivemodule.py” file:

We need to import the motors.py and keyboardmodule.py in the testdrivemodule.py file. The km.init() is used to initialize the keyboardmodule.py. Under the following, while loop you can find the “ km.getKey() ” which is used to get the following ‘w’, ‘s’, ‘q’, ‘e’, ’a’ and ’d’ input keys when we will pressed those keys. And we used the following mot.forward(), mot.backward(), mot.fleft(), mot.fright(), mot.bright() and mot.bleft() for the “ forward ”, “ backward ”, “ forwardleft ”, “ forwardright ”, “ bakcwardright ”, “ backwardleft ” respectively.

Autonomous Lane Detection Car Testing

You need to upload the code to your Raspberry Pi 4. I have used the mobaxterm software to open the Raspberry Pi terminal. Then you need to run the “ testdrivemodule.py ” by using the following command in the terminal. Make sure that you have the “ motors.py ” and the “ keyboardmodule.py ” in the same folder.

You can refer to the following video for more details. In the video, you can see that the car can be controlled with the keyboard keys. In the next part, we will take the video feed from the camera and prepare our Lane Detection algorithm for the video.

Complete Project Code

can you share the 3D model file for the camera stand

Technologies used

PythonTensorFlowOpenCVYOLOKerasRaspberry PiCNN

Hardware components

  • Raspberry Pi
  • USB Camera
  • Pi Camera
  • Jetson Nano

Software & tools

  • Python
  • TensorFlow
  • OpenCV
  • Jupyter Notebook
  • Google Colab

Project demo video

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.