How to do it...

Perform the following steps to implement object detection with the Mask R-CNN pretrained model using opencv-python functions:

  1. Initialize the parameters (for example, thresholds for Confidence and Mask), using the following code snippet:
conf_threshold = 0.5 # Confidence thresholdmask_threshold = 0.3 # Mask threshold
  1. Define the following function to plot the predicted bounding box for the detected object, colorize it as per the object's class predicted, and overlay the mask computed on top of the input image:
def draw_box(img, class_id, conf, left, top, right, bottom, class_mask):    cv2.rectangle(img, (left, top), (right, bottom), \        (255, 178, 50), 3)        label = '%.2f' % conf    if classes:        assert(class_id < len(classes)) label ...

Get Python Image Processing Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.