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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.