Live Object Detection

Live Object DetectionLive Object Detection using the Tensorflow Object Detection APIGilbert TannerBlockedUnblockFollowFollowingJan 31Figure 1: Live Object DetectionObject detection deals with detecting instances of a certain class, like humans, cars or animals in an image or video.

It can achieve this by learning the special features each object possesses.

The Tensorflow Object Detection API is an open source framework that allows you to use pretrained object detection models, or create and train new models by making use of transfer learning.

This is extremely useful because building an object detection model from scratch can be difficult and can take a very long time to train.

In this article, we will go through the process of rewriting the existing example code to detect objects in real time.

InstallationFor getting the video stream we will use the OpenCV( Open Source Computer Vision) library, which can be installed by typing:pip install opencv-python==3.



19orconda install opencvIf you don’t have the Tensorflow Object Detection API installed yet you can watch my video that goes through the installation step-by-step and at the end tests the functionality by executing the example notebook.

Object DetectionAs said above the example notebook can be reused for our new application.

This is because the main part of the notebook is importing the needed libraries, downloading the model and specifying useful helper code.

The only section we need to modify is the detection section, which comprises of the last three cells and at the moment is detection objects in two manually loaded images.

The first cell isn’t needed at all anymore since its only purpose was to get the paths to the test images.

In order to create a live object detection application we need to make minor changes to the second and third cell.

First we need to remove all code that only needs to be executed once from the run_inference_for_single_images method.

This is because if we need to call this method multiple times per second it is really computationally expensive to execute redundant code.

The code statements to remove include everything from the with statements, that open both the graph and session, until the start of the if statement.

The removed lines will be copied into the next cell.

The finished function looks like:In the last cell we will first of include all the code we removed from the cell above.

Now we will import OpenCV, create a VideoCapture object and change the for loop that loops through the test images to a while True loop.

Inside the loop we won’t load the images using Image.

open anymore and rather use the read function from the VideoCapture object to get the current frame.

Lastly we also need to change the visualization part to use cv2.

imshow , which creates a gui that shows the live video instead of the plt.

imshow function that just shows a static image.

We also will define an if statement that checks if the q button was pressed and if it was closes the window and releases the webcam.

After running this a new window will open, which can be used to detect objects in real time.

Figure 2: Object Detection ExampleRecommended ReadingIntroduction to Deep Learning with KerasHow to use the Keras Deep Learning librarytowardsdatascience.

comConclusionObject detection deals with detecting instances of a certain class, like inside an certain image or video.

It can achieve this by learning the special features each object possesses.

The Tensorflow Object Detection API allows you to easily create or use a object detection model by making use of pretrained models and transfer learning.

If you liked this article consider subscribing on my Youtube Channel and following me on social media.

The code covered in this article is available as a Github Repository.

If you have any questions, recommendations or critiques, I can be reached via Twitter or the comment section.. More details

Leave a Reply