Line Following Robot Using OpenCV

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hello guys it's me sarwash back again with another video last week i just randomly checked my youtube channel and i found people who are actually watching my videos so here i am today with a new fun project the last project i uploaded about a year ago was a line following robot using only one ir sensor so it made sense to make a line following robot without any air sensors instead we will be using a usb camera and opencv for the line following so let's jump straight into it this project will be made using a raspberry pi zero you can use any other single board computer you have at your disposal let's continue the other things you will need to make this robot are a metal chassis with wheels a l298n motor driver and a usb camera again you are free to use rpi camera or any wired or wireless ip camera for powering the robot use a 12 volts battery do note that you will need a step down converter as the raspberry pi will only work with 5 volts or you can simply use a power bank to power the raspberry pi as i did in this project before getting into connections let's write a basic line follower script on our computer to test the black line detection if you are here just for the code then click the link in the description it will take you to my instructables page and you can download the code from there ok back to programming first we import the opencv and numpy libraries i am using the opencv contrib library as i had it installed on my system already if you don't have opencv then just type pip install opencv contrib python one thing to note here is that you do not need the contrib library for this project but if you still wish to install it then don't forget to uninstall the opencv library if you have it installed on your system already as opencv and opencv country library tends to conflict with each other next thing we will do is to create a video capture object and set the resolution to 160 cross 120 now we are ready to get our first frame from our camera for that we use the dot read method and for displaying the frame on our screen we use the cv2.iamshow method but with this we can only read one frame now to do this process repeatedly we make use of a while loop now as soon as you run the program the display window freezes and this is because there is no delay in the while loop so to solve this we use the weight key function we enter the delay in milliseconds in the parenthesis we also need to assign a key to exit the while loop for that we use the ord function it gives us the unicode value of the character we intend to use to stop the loop we use an if statement to check if the key is pressed if the key is pressed we simply break the loop to release the camera and to close the windows we use the cab dot release and cv2.destroy all windows instructions now comes the fun part detecting the line and tracking it for that we first need to differentiate the black line from the rest of the frame we achieve that by creating a mask with low and high black values as the range for tracking a line with different color you need to change these values accordingly i will leave a link in the description to murthur hassan's youtube channel he has some amazing videos related to opencv do check them out if you want to learn more once the mask is created we can easily see that only the black line is visible and the rest of the area is blackened out after this we find the contours on the mask image and draw them on the original frame using the draw contours method everything seems fine until we introduce another black object into the window now the contour is drawn around both the black line and the new black object this will make the robot to shift its focus from the line and we do not want that so we need to find the contour with the largest area but for that we need to at least have one contour detected in our image we use the max function to find the largest contour now save and run the program and you can easily see that we only draw the contours around the black line and not the black object once we draw the contour around the black line we need to find the center of the contour and for this we use the moments method using this we get the x and y position of the center of the contour for better representation we draw a circle at the center point now the only part that remains in image processing is to figure out whether the line is in the center of the window or not we know that the width of the window is 160 as we assigned it in the start so we will consider the center of the window in between 40 and 120 pixels and anything below 40 will be considered left and above 120 will be considered right i have written print statements to know the exact alignment of the black line last step to complete our program is to add instructions for actually controlling the robot we import the raspberry pi gpio library and set the mode to bcm we then declare the output pins of the rpi and set all the initial values to low we also need to initialize two pwm pins for the enable pins of the motor driver module in order to control the speed of the motors set the pwm values to 50. you can experiment with these values while testing the robot later now in the if statements in the last we add the left and right moment code by turning the gpio pins high and low according to our connections we also need to set the output pins to low before the break statement so that our robot stops once the exit k is pressed yes now this brings us to the part of assembling the robot refer this circuit diagram to make all the connections once all the connections are done copy and paste the program onto a raspberry pi and run it there for remotely accessing the raspberry pi i used the vnc weaver if you do not know how to set it up leave a comment in the description i will do my best to help you out and now place the robot on a black line and let it do its thing [Music] foreign [Music] here we go [Music] down [Music] i hope you enjoyed this video if you did then drop a like also we are only one subscriber shy of 200 so do subscribe i will start working on the next video as soon as we hit 200 subs so please help me reach the goal and thanks for watching the video you
Info
Channel: Engineer Kid
Views: 22,333
Rating: undefined out of 5
Keywords: install opencv, Line Following Robot Using OpenCV, opencv pyhton tutorials, opencv python install, opencv, computer vision basics, image processing, learn computer vision, self driving car with lane detection using raspberry pi, autonomous raspberry pi, opencv projects, opencv object detection, python tutorial for beginners, python scripting tutorial, python for beginners, pycharm ide, raspberry pi projects, electronics project ideas for engineering students, line follower
Id: tlkWX7R-NHE
Channel Id: undefined
Length: 8min 57sec (537 seconds)
Published: Mon May 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.