Python code for obstacle detection

Robots are machines which reduce the human efforts in heavy works by automating the tasks in industries, factories, hospitals etc.

Selective Search for Object Detection (C++ / Python)

Most of the robots are run by using some control unit or components like a push button, remote, joystick, PC, gestures and by executing some command by using controller or processor.

But today we are here with a Automatic Robot which moves autonomously without any external events avoiding all the obstacle in its path, yes we talking about Obstacle Avoiding Robot.

In this project, we have used Raspberry Pi and Motor driver to drive the robot and Ultrasonic sensor for detecting objects in the path of Robot. Previously we have covered many useful Robots, you can find them in our Robotics projects section. Here we have done Obstacle Detection using Ultrasound Signals. For this purpose, we have used Ultrasonic Sensor Module. Ultrasonic Sensors are commonly used to detect objects and determine the distance of the obstacle from the sensor.

This is a great tool to measure the distance without any physical contact, like as Water Level Measurement in tankdistance measurementObstacle avoider robot etc. So here, we have detected the object and measured the distance by using Ultrasonic Sensor and Raspberry Pi. Ultrasonic sensor HC-SR04 is used to measure distance in the range of 2cmcm with an accuracy of 3mm. The sensor module consists of an ultrasonic transmitter, receiver, and the control circuit.

We can calculate the distance of the object based on the time taken by ultrasonic wave to return back to the sensor. Since the time and speed of sound is known we can calculate the distance by the following formulae.

The value is divided by two since the wave travels forward and backward covering the same distance. Thus the time to reach obstacle is just half the total time taken. So we have calculated the distance in centimetre from the obstacle like below:.

Working of this Autonomous Robot is very easy. When the Robot is powered on and starts running, Raspberry Pi measures the distances of objects, in front of it, by using Ultrasonic Sensor Module and stores in a variable.

python code for obstacle detection

Then RPi compares this value with predefined values and take decisions accordingly to move the Robot Left, Right, Forward, or backward.

Here in this project, we have selected 15cm distance for taking any decision by Raspberry Pi. Now whenever Raspberry Pi gets less than the 15cm distance from any object then Raspberry Pi stops the robot and moves it back and then turns it left or right. Now before moving it forward again, Raspberry Pi again checks whether any obstacle is present within the range of 15 cm distance, if yes then again repeats the previous process, else move the robot forward until it will detect any obstacle or object again.

Before coding, user needs to configure Raspberry Pi. The programming part of this project plays a very important role to perform all the operations.

First of all, we include required libraries, initialize variables and define pins for ultrasonic sensor, motor and components. After it, we have created some functions def forwarddef backdef leftdef right to move robot in forward, backward, left or right direction respectively and def stop to stop the robot, check the functions in Code given below. Then, in the main program, we have initiated Ultrasonic Sensor and read time between transmission and reception of the signal and calculated the distance.

Here we have repeated this process for 5 times for better accuracy. We have already explained the process of calculating the distance using Ultrasonic sensor.

Arduino Ultrasonic Sensor Obstacle detection using Arduino & ”HC SR04

Finally if Robot finds any obstacle in front of it then after getting distance from the obstacle, we have programmed the Robot to take different route.

Import time library GPIO.Part 2 of this tutorial for detecting your custom objects is available via this link. One of the important fields of Artificial Intelligence is Computer Vision. Computer Vision is the science of computers and software systems that can recognize and understand images and scenes. Computer Vision is also composed of various aspects such as image recognition, object detection, image generation, image super-resolution and more. Object detection is probably the most profound aspect of computer vision due the number practical use cases.

In this tutorial, I will briefly introduce the concept of modern object detection, challenges faced by software developers, the solution my team has provided as well as code tutorials to perform high performance object detection. Object detection has been widely used for face detection, vehicle detection, pedestrian counting, web images, security systems and driverless cars. There are many ways object detection can be used as well in many fields of practice.

Like every other computer technology, a wide range of creative and amazing uses of object detection will definitely come from the efforts of computer programmers and software developers. Getting to use modern object detection methods in applications and systems, as well as building new applications based on these methods is not a straight forward task.

Early implementations of object detection involved the use of classical algorithms, like the ones supported in OpenCV, the popular computer vision library. However, these classical algorithms could not achieve enough performance to work under different conditions. Using these methods and algorithms, based on deep learning which is also based on machine learning require lots of mathematical and deep learning frameworks understanding.

There are millions of expert computer programmers and software developers that want to integrate and create new products that uses object detection. But this technology is kept out of their reach due to the extra and complicated path to understanding and making practical use of it.

My team realized this problem months ago, which is why I and John Olafenwa built ImageAIa python library that lets programmers and software developers easily integrate state-of-the-art computer vision technologies into their existing and new applications, using just few lines of code. To perform object detection using ImageAI, all you need to do is.

Download the Object Detection model file. Run the sample codes which is as few as 10 lines. Now that you have installed the dependencies, you are ready to write your first object detection code. Create a Python file and give it a name For example, FirstDetection. Copy the RetinaNet model file and the image you want to detect to the folder that contains the python file.

Then run the code and wait while the results prints in the console. Once the result is printed to the console, go to the folder in which your FirstDetection. Take a look at a 2 image samples below and the new images saved after detection. Before Detection:. After Detection:. Console result for above image:. Now let us explain how the line code works. In the above 3 lines, we imported the ImageAI object detection class in the first line, imported the python os class in the second line and defined a variable to hold the path to the folder where our python file, RetinaNet model file and images are in the third line.

In the 5 lines of code above, we defined our object detection class in the first line, set the model type to RetinaNet in the second line, set the model path to the path of our RetinaNet model in the third line, load the model into the object detection class in the fourth line, then we called the detection function and parsed in the input image path and the output image path in the fifth line. In the above 2 lines of code, we iterate over all the results returned by the detector. ImageAI supports many powerful customization of the object detection process.

One of it is the ability to extract the image of each object detected in the image.If you could do a tutorial on real-time object detection with deep learning and OpenCV I would really appreciate it. Luckily, extending our previous tutorial on object detection with deep learning and OpenCV to real-time video streams is fairly straightforward — we simply need to combine some efficient, boilerplate code for real-time video access and then add in our object detection.

We begin by importing packages on Lines For this tutorial, you will need imutils and OpenCV 3. Note: Make sure to download and install opencv and and opencv-contrib releases for OpenCV 3.

This will ensure that the deep neural network dnn module is installed. You must have OpenCV 3. We load our serialized model, providing the references to our prototxt and model files on Line 30 — notice how easy this is in OpenCV 3.

Since we will need the width and height later, we grab these now on Line At this point, we have detected objects in the input frame. We also apply a check to the confidence i. If the confidence is high enough i. Then, we extract the x, y -coordinates of the box Line 70 which we will will use shortly for drawing a rectangle and displaying text. The remaining steps in the frame capture loop involve 1 displaying the frame, 2 checking for a quit key, and 3 updating our frames per second counter:.

The above code block is pretty self-explanatory — first we display the frame Line We close the open window Line 98 followed by stopping the video stream Line Provided that OpenCV can access your webcam you should see the output video frame with any detected objects. I have included sample results of applying deep learning object detection to an example video below:.

python code for obstacle detection

Notice how our deep learning object detector can detect not only myself a personbut also the sofa I am sitting on and the chair next to me — all in real-time! The end result is a deep learning-based object detector that can process approximately FPS depending on the speed of your system, of course. Enter your email address below to get a. All too often I see developers, students, and researchers wasting their time, studying the wrong things, and generally struggling to get started with Computer Vision, Deep Learning, and OpenCV.

I created this website to show you what I believe is the best possible way to get your start.

obstacle-detection

I have done cascade training for object detection. Can you explain how you generate the three models protext caffe etc used in your project. You should read this post to understand the fundamentals of deep learning object detection. I then cover how to train your own custom deep learning object detections inside Deep Learning for Computer Vision with Python.

Hi Adrian, Thank you for helpful post. I am working now with tensorflow model. I already done object detection with tensorflow model but my goal is to create OpenCV tracker using my trained tensorflow model for tracking.

Can you suggest some example or source code. You mean something like this? And how it is different from other models? The method and algorithm used is documented in this post and in in this one.

Hi, Adrian.We will be working with colors and you will get to learn about many concepts throughout this project. Colour detection is necessary to recognize objects, it is also used as a tool in various image editing and drawing apps. I suggest you to bookmark the previous projects:. Keeping you updated with latest technology trends, Join DataFlair on Telegram.

Colour detection is the process of detecting the name of any color. Well, for humans this is an extremely easy task but for computers, it is not straightforward. Human eyes and brains work together to translate light into color. Light receptors that are present in our eyes transmit the signal to the brain. Our brain then recognizes the color. Since childhood, we have mapped certain lights with their color names.

We will be using the somewhat same strategy to detect color names. In this color detection Python project, we are going to build an application through which you can automatically get the name of the color by clicking on them. So for this, we will have a data file that contains the color name and its values. Then we will calculate the distance from each color and find the shortest one. Colors are made up of 3 primary colors; red, green, and blue. In computers, we define each color value within a range of 0 to So in how many ways we can define a color?

There are approximately We will be using a dataset that contains RGB values with their corresponding names. The CSV file for our dataset has been taken from this link:. Colors Dataset. Before starting with this Python project with source code, you should be familiar with the computer vision library of Python that is OpenCV and Pandas. To install them, simply run this pip command in your terminal:. Color Detection Zip File. We are using argparse library to create an argument parser.

We can directly give an image path from the command prompt:. The pandas library is very useful when we need to perform various operations on data files like CSV.In this feature, I look at what it takes to setup object detection and tracking using OpenCV and Python code.

Using the code snippets included, you can easily setup a Raspberry Pi and webcam to make a portable image sensor for object detection. Some projects can include Raspberry Pi robot vehicles for obstacle avoidance or waypoint tracking.

Also, object counting and monitoring included. Object detection builds on my last article where I apply a colour range to allow an area of interest to show through a mask. In this feature, I continue to use colour to use as a method to classify an object.

If I can classify an object by colour, I can track the object from video frame to video frame. However, smaller objects of a similar colour can be filtered out using OpenCV methods.

Therefore, I will be using an example of these round balls to test the code below. Since the plastic balls are a good solid even colour, Object detection by colour is the best choice in this case. All the code snippets below are included in the example OpenCV example program near the end of this article. I use an array to hold the values of the lower and upper colour threshold.

Object Detection Mask. The mask will make it more efficient to find contours around the detected object. The white area is the area of interest that was found within the colour range set above.

I can use various image filters to improve the image mask. However, applying filters to get the perfect mask can be expensive in regards to processing power. Now that I have an image mask to work with I can proceed with finding contours.

All the white blobs in the mask will have contours applied. The array list of found contours will be in the contours variable. If the quality of the mask is noisy, there is going to be a lot of contours — many small contours. To get useful object detection, I need to remove the contours I do not need.

The image above shows a contour circling the plastic ball; I will aim to isolate that in the next step. And then draw the biggest contour on to the original image. Since the ball is the largest blue object, I can reliably detect the ball. However, the ball must remain the dominant blue object and remain in focus to be tracked reliably. So at this point, I was able to improve OpenCV object detection. I am now only detecting one item.

The quality of the object detection is very good in this case. However, moving webcam or object or even changing light conditions can make the quality of detection unpredictable. As you can see from the example image, with very little Python code, I got good OpenCV object detection. The third line of the above Python code reveals how I can pull useful data about the detected object.

Furthermore, I can see how this data is being used to draw a bounding box around the detected object.An object recognition algorithm identifies which objects are present in an image. It takes the entire image as an input and outputs class labels and class probabilities of objects present in that image.

On the other hand, an object detection algorithm not only tells you which objects are present in the image, it also outputs bounding boxes x, y, width, height to indicate the location of the objects inside the image.

At the heart of all object detection algorithms is an object recognition algorithm. Suppose we trained an object recognition model which identifies dogs in image patches. This model will tell whether an image has a dog in it or not. It does not tell where the object is located. To localize the object, we have to select sub-regions patches of the image and then apply the object recognition algorithm to these image patches. The location of the objects is given by the location of the image patches where the class probability returned by the object recognition algorithm is high.

The most straightforward way to generate smaller sub-regions patches is called the Sliding Window approach. However, the sliding window approach has several limitations. Selective Search is one of the most popular Region Proposal algorithms. In the sliding window approach, we slide a box or window over an image to select a patch and classify each image patch covered by the window using the object recognition model.

It is an exhaustive search for objects over the entire image. Not only do we need to search all possible locations in the image, we have to search at different scales. This is because object recognition models are generally trained at a specific scale or range of scales. This results into classifying tens of thousands of image patches.

Object Detection with 10 lines of code

Sliding window approach is good for fixed aspect ratio objects such as faces or pedestrians. Images are 2D projections of 3D objects. Object features such as aspect ratio and shape vary significantly based on the angle at which image is taken. The sliding window approach because computationally very expensive when we search for multiple aspect ratios.

The problems we have discussed so far can be solved using region proposal algorithms. These methods take an image as the input and output bounding boxes corresponding to all patches in an image that are most likely to be objects. These region proposals can be noisy, overlapping and may not contain the object perfectly but amongst these region proposals, there will be a proposal which will be very close to the actual object in the image.

We can then classify these proposals using the object recognition model.Part 2 of this tutorial for detecting your custom objects is available via this link.

One of the important fields of Artificial Intelligence is Computer Vision. Computer Vision is the science of computers and software systems that can recognize and understand images and scenes.

Computer Vision is also composed of various aspects such as image recognition, object detection, image generation, image super-resolution and more. Object detection is probably the most profound aspect of computer vision due the number practical use cases.

In this tutorial, I will briefly introduce the concept of modern object detection, challenges faced by software developers, the solution my team has provided as well as code tutorials to perform high performance object detection. Object detection has been widely used for face detection, vehicle detection, pedestrian counting, web images, security systems and driverless cars.

There are many ways object detection can be used as well in many fields of practice. Like every other computer technology, a wide range of creative and amazing uses of object detection will definitely come from the efforts of computer programmers and software developers. Getting to use modern object detection methods in applications and systems, as well as building new applications based on these methods is not a straight forward task.

Early implementations of object detection involved the use of classical algorithms, like the ones supported in OpenCV, the popular computer vision library. However, these classical algorithms could not achieve enough performance to work under different conditions. Using these methods and algorithms, based on deep learning which is also based on machine learning require lots of mathematical and deep learning frameworks understanding.

There are millions of expert computer programmers and software developers that want to integrate and create new products that uses object detection.

But this technology is kept out of their reach due to the extra and complicated path to understanding and making practical use of it. My team realized this problem months ago, which is why I and John Olafenwa built ImageAIa python library that lets programmers and software developers easily integrate state-of-the-art computer vision technologies into their existing and new applications, using just few lines of code.

To perform object detection using ImageAI, all you need to do is. Download the Object Detection model file. Run the sample codes which is as few as 10 lines. Now that you have installed the dependencies, you are ready to write your first object detection code.

python code for obstacle detection

Create a Python file and give it a name For example, FirstDetection. Copy the RetinaNet model file and the image you want to detect to the folder that contains the python file. Then run the code and wait while the results prints in the console. Once the result is printed to the console, go to the folder in which your FirstDetection. Take a look at a 2 image samples below and the new images saved after detection. Before Detection:. After Detection:. Console result for above image:.

Now let us explain how the line code works.


thoughts on “Python code for obstacle detection

Leave a Reply

Your email address will not be published. Required fields are marked *