You are currently viewing Object Detection API using Python Tutorial Based on Tensorflow
Tensorflow Object detection API Tutorial using Python

Object Detection API using Python Tutorial Based on Tensorflow

Loading

Introduction

Tensorflow object detection API using Python is a powerful Open-Source API for Object Detection developed by Google. This is a ready to use API with variable number of classes. It provides a large number of model which is trained on various data-sets.

According to various data-sets the number of predictable classes are different. We will discuss here how to run “Tensorflow Object Detection API” with 90 classes. Here all the files and models are replicated from github. You can visit this link and can try by yourself.

Also, many versions and updation may continuously go on, so for past and future this article don’t take responsibility that it will run. It is tested on date 21 Oct 2018. It is an open source API on git-hub so you can also make contribution to provide improvements in the same. Now, we will focus on its installation and running of code in steps.

We will make experiment on image, video and live web-cam.

  • In image prediction, we will gather 5 static images and will make prediction on it. Predicted rectangular box with their label will also be shown.
  • In video prediction, we will use a static video downloaded from YouTube and show prediction on video with rectangle box.
  • In real-time web-cam prediction, we will use a separate web-cam. The prediction in rectangle box with label will show as camera video feed.

In this complete tutorial of Object Detection API using Python you will learn following things:

  1. Installation using virtual environment
  2. Prediction on Image
  3. Prediction on Video
  4. Prediction on Real-Time Web-Cam
  5. Prediction Program customisation
  6. Run prediction on different pre-trained model

1. Installation of dependencies using virtual environment

In this part we will learn all type of system set-up and library installation. The hardware and software list is as below:

OS: Ubuntu 16.04 LTS

RAM: 4 GB

Processor: Intel® Core™ i5-3210M CPU @ 2.50GHz × 4

Note: You can run this on any hardware and software. For different configuration you can contact us. At nominal charge we will make all type of setup for you.

A. Install virtual Environment

B. Install dependencies library to run Object Detection API

C. Path setup and check its correctness

A. Installation of virtual Environment

Since we are using Ubuntu 16.04 so we will learn how to install virtual environment first.

1. First install pip3 on your system by typing given command in terminal.

sudo apt-get install python3-pip

2. Install virtual environment package on your system by typing command on your system.

sudo pip3 install virtualenv

3. Now create virtual environment by this command.

For python2.7

virtualenv -p /usr/bin/python2.7 venv

or for python 3.5 you can use this command

virtualenv -p /usr/bin/python3.5 venv

4. Activate virtual environment

source venv/bin/activate

or

source venv/bin/activate.fish

Its done now you are installed and activated to work on virtual environment.

5. Deactivate:

If your work is complete then you can deactivate virtual environment.

deactivate

B. Install dependencies library to run Object Detection API

For this firstly clone the repository of github. and extract them. Further rename them as model from model-master

The list of dependent library is:

1. Tensorflow (>=1.9.0):

#for CPU 
pip install tensorflow 
#for GPU 
pip install tensorflow-gpu

2. Python-tk

3. Pillow 1.0

4. lxml

5. tf Slim (which is included in the “tensorflow/models/research/” checkout)

6. Jupyter notebook

7. Matplotlib

8. Cython

9. contextlib2

sudo apt-get install python-tk #for Tkinter
pip install --user pillow #for Pillow
pip install --user lxml #for Lxml
pip install --user jupyter #for Jupyter
pip install --user matplotlib #for matplotlib
pip install --user Cython #for Cython
pip install --user contextlib2 #for Contextlib2

10. Protobuf 3.0.0:

This is most error prone library and without this library configuration of this API is not possible. Protobuf configure the training parameter and model so for both training and prediction it is must to be configured.

For this use this command:

cd tensorflow/models/research/ 
protoc object_detection/protos/*.proto --python_out=.

If you are getting error on this command then you have to install it manual. This is compiler error of protobuf file for its incompatibility. For its manual installation type this command:

# From tensorflow/models/research/ 
wget -O protobuf.zip https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip 
unzip protobuf.zip

Then use this command:

./bin/protoc object_detection/protos/*.proto --python_out=.

The installation part is completed.

C. Path setup and check correctness of Tensorflow object detection API

We have to add slim directory path with PYTHONPATH. There is many API file as library, saved in slim directory. For this we need to type this command:

# From tensorflow/models/research/ 
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim

This command locally add slim path with PYTHONPATH so when you will restart the system it will lost. To avoid it add it on ~/.bashrc file and provide absolute path of slim directory.

Now all installation is done further we have to check that all installation is right or not. For this run following file:

#from “tensorflow/models/research/” 
python object_detection/builders/model_builder_test.py

On successful installation it will print “OK” massage on terminal. If there is some issue then reinstall particular Dependent library.

2. Prediction on Image using Tensorflow object detection API

In this part, we will learn how to predict image using Object detection API using Python. We have been already installed virtual environment, Activated them and working on same, Installed dependency library for object detection API and also setup all the relevant path to execute the prediction model. If any part is not happened please go on relevant section and complete them first otherwise program will not work.

Now please visit our Github link and download all code and put them inside tensorflow/models/research/object_detection/”. After getting code you have to download pre-trained model. For this click here, the model will automatically download. Put this model inside directory “tensorflow/models/research/”.

Now run the file “Object_Detection_Image.py” by typing this command on terminal.

#from tensorflow/models/research/ 
python object_detection/Object_Detection_Image.py

It will execute the prediction and show you predicted image with building box and label.

3. Prediction on Video using Tensorflow on object detection API

After running code on image prediction, video prediction is too easy. We will take help of OpenCV to process video and extract the frame. One by one frame will be provided to the prediction model throughout the video. For run prediction on video follow these steps:

Step 1: Open “Object_Detection_Video.py” file in editor.

Step 2: On line number 77 Change VideoFilePath with any sample static video.

Step 3: Run the command on terminal.

#from tensorflow/models/research/ 
python object_detection/Object_Detection_Video.py

Video prediction will be start. If there is error of OpenCV, Then install OpenCV And then run this command.

4. Prediction on Real-Time Web-Cam using Tensorflow object detection API using Python

After successfully run prediction on video you only have to change one line code and program will be start prediction on live web-cam. For this follow these steps:

Steps1: Run the file “Object_Detection_WebCam.py” by typing this command on terminal.

#from tensorflow/models/research/ 
python object_detection/Object_Detection_WebCam.py

It will start prediction on Webcam in real-time.

5. Prediction Program customization of Tensorflow object detection API

You can perform some optimization on this code according to our need.

A. Predict on only some label

For this you have to type some code on either image or video or webcam file. Lets take example of video file.

Step 1: open file “Object_Detection_WebCam.py” in editor.

Step 2: on line 99 append code:

if classes!==2 or classes!==3: 
    continue

it the prediction will only show for class index 2 and 3.

B. Line thickness of box:

You can adjust the box line width in code. For this in line number 107 adjust the “line_thickness” value in integer.

6. Run prediction on different pre-trained model of Tensorflow object detection API

Here we used SSD Mobilenet v1 model of COCO data-set. You can change it by downloading different model from this link. And follow this step.

Step 1: Download any boxed pre-trained model.

Step 2: Copy zip file in directory “tensorflow/models/research/”.

Step 3: Extract zip file there.

Step 4: Copy the name of model file.

Step 5: Open any prediction file on editor. Lets do it on “Object_Detection_WebCam.py”.

Step 6: Pest the file name on line 24 as MODEL_NAME.

Step 7. Run the file.

It will now predict according to newly added model. Accuracy will be differ. So choose the model which is providing you the best accuracy.

Also Read:

Understanding Keras LSTM Architecture in a simple way

Leave a Reply