Object Detection

Object detection will take an image and identify and label specific objects within the image. For a complex image with multiple objects in view, object detection will provide a bounding box around each detected object, as well as a label identifying the class to which the object belongs.

../_images/object_detection.png

Object detection can be performed on an image using the ObjectDetection class. The first step is to instantiate an ObjectDetection object with the ID of the model to use. For example:

obj_detect = edgeiq.ObjectDetection("alwaysai/mobilenet_ssd")

If the model is not an object detection model, the instantiation will fail with an error message indicating that the model can’t be used. Next, call the object’s load() function to initialize the inference engine and accelerator.

obj_detect.load(engine=edgeiq.Engine.DNN)

Unless directly specified, the accelerator chosen will be the default for the provided Engine. Now the object detector is ready. Use the detect_objects() function to detect objects in an image:

results = obj_detect.detect_objects(image, confidence_level=.5)

The results object is of type ObjectDetectionResults and contains the duration of the inference, in seconds, and a list of predictions. Each prediction is of type ObjectDetectionPrediction and contains the coordinates of a box around the object, the confidence, the label, and the index of the label in the master label list of the model for cross-referencing with that list.

It can be useful to visualize the detections. The image can be marked with the results of the detection using the markup_image() function:

new_image = edgeiq.markup_image(image, object_detection_predictions)

The markup_image() function takes as its input an image (the one used for object detection) and a list of ObjectDetectionPrediction objects and draws the bounding boxes and labels on the image. Head to the Understanding the Results Objects section to learn more about how the results objects can be used.