DAY 74-100 DAYS MLCODE: Live Object Detection using TensorFlow

My Tech World

DAY 74-100 DAYS MLCODE: Live Object Detection using TensorFlow

January 23, 2019 100-Days-Of-ML-Code blog 0

In the previous blog, we learned how to use the TensorFlow Object detection library to detect the object in the image, in this blog, we’ll implement Live Object Detection using TensorFlow.

Before we proceed with code, we have to download model and the TensorFlow Models can be found here: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md

We’ll use the pre-trained model ssd_mobilenet_v1_coco_2018_01_28 in our program.

Once the model is downloaded, we have to extract the tar file and try to get the frozen object detection graph. Below is the path for the frozen graph. We’ll load the frozen graph into memory.

MODEL_NAME + ‘/frozen_inference_graph.pb’

# Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_CKPT, ‘rb’) as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name=”)

Let’s change the previous code to allow the same program to detect the live object using webcam of laptop.

We have to import the CV2 library to first and start capturing the webcam live feed.

Below code will help to register the first webcam and then we can read the feed later.

import cv2
cap=cv2.VideoCapture(0) # 0 means first webcam device

in the previous code , we pass the images , here we’ll read the webcam feed and then pass the frame to the graph.

# Read frame from camera
ret, image_np = cap.read()
#Extend the image_np so that it can be fed to Network.Network have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)

Use the below code to detect the object:

# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})

Now we can draw the boxes and we can again use the CV2.imshow to display the live Object detection.

Below is the one of the frame of live output.

Object Detection using TensorFlow
Object Detection using TensorFlow

And the output of the live cam was like below:

Live Object Detection

So basically code wise it is the same code which was used for the Image, only instead of passing single image, we are passing the frame of live video. We can also add the code to download and store the video.

In conclusion, the powerful object detection library of TensorFlow is easy to implement and we can use to detect the object in live videos or images. You can find today’s code here.