text: import numpy as np
  import os
  import six.moves.urllib as urllib
  import sys
  import tarfile
  import tensorflow as tf
  import zipfile
  import cv2
  
  from collections import defaultdict
  from io import StringIO
  import matplotlib
  from matplotlib import pyplot as plt
  from PIL import Image
  
  # This is needed since the notebook is stored in the object_detection folder.
  sys.path.append("..")
  #from object_detection.utils import ops as utils_ops
  
  import random
  import math
  import skimage.io
  
  # Root directory of the project
  ROOT_DIR = os.path.abspath("../")
  
  # Import Mask RCNN
  sys.path.append(ROOT_DIR)  # To find local version of the library
  from mrcnn import utils
  import mrcnn.model as modellib
  from mrcnn import visualize
  # Import COCO config
  sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
  import coco
  
  #%matplotlib inline 
  
  image_name = 'OriginalM.jpg'
  
  # Directory to save logs and trained model
  MODEL_DIR = os.path.join(ROOT_DIR, "logs")
  
  # Local path to trained weights file
  COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
  # Download COCO trained weights from Releases if needed
  if not os.path.exists(COCO_MODEL_PATH):
      utils.download_trained_weights(COCO_MODEL_PATH)
  
  # Directory of images to run detection on
  IMAGE_DIR = os.path.join(ROOT_DIR, "images")
  
  class InferenceConfig(coco.CocoConfig):
      # Set batch size to 1 since we'll be running inference on
      # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
      GPU_COUNT = 1
      IMAGES_PER_GPU = 1
  
  config = InferenceConfig()
  config.display()
  
  # Create model object in inference mode.
  model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
  
  # Load weights trained on MS-COCO
  model.load_weights(COCO_MODEL_PATH, by_name=True)
  
  def display_instances(image, boxes, masks, ids, names, scores):
      """
          take the image and results and apply the mask, box, and Label
      """
      n_instances = boxes.shape[0]
      colors = random_colors(n_instances)
  
      if not n_instances:
          print('NO INSTANCES TO DISPLAY')
      else:
          assert boxes.shape[0] == masks.shape[-1] == ids.shape[0]
  
      for i, color in enumerate(colors):
          if not np.any(boxes[i]):
              continue
  
          y1, x1, y2, x2 = boxes[i]
          label = names[ids[i]]
          score = scores[i] if scores is not None else None
          caption = '{} {:.2f}'.format(label, score) if score else label
          mask = masks[:, :, i]
  
          image = apply_mask(image, mask, color)
          image = cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
          image = cv2.putText(
              image, caption, (x1, y1), cv2.FONT_HERSHEY_COMPLEX, 0.7, color, 2
          )
  
      return image
  
  def random_colors(N):
      np.random.seed(1)
      colors = [tuple(255 * np.random.rand(3)) for _ in range(N)]
      return colors
  
  def apply_mask(image, mask, color, alpha=0.5):
      """apply mask to image"""
      for n, c in enumerate(color):
          image[:, :, n] = np.where(
              mask == 1,
              image[:, :, n] * (1 - alpha) + alpha * c,
              image[:, :, n]
          )
      return image
  
  # COCO Class names
  # Index of the class in the list is its ID. For example, to get ID of
  # the teddy bear class, use: class_names.index('teddy bear')
  class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
                 'bus', 'train', 'truck', 'boat', 'traffic light',
                 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
                 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
                 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
                 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
                 'kite', 'baseball bat', 'baseball glove', 'skateboard',
                 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
                 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
                 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
                 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
                 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
                 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
                 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
                 'teddy bear', 'hair drier', 'toothbrush']
  
  
  capture = cv2.VideoCapture(os.path.join(IMAGE_DIR, 'video.mp4'))
  
  # these 2 lines can be removed if you dont have a 1080p camera.
  #capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
  #capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
  
  #frame_width = int(capture.get(3))
  #frame_height = int(capture.get(4))
  
  frame_width = int( capture.get(cv2.CAP_PROP_FRAME_WIDTH))
  frame_height =int( capture.get( cv2.CAP_PROP_FRAME_HEIGHT)) 
  
  fourcc = cv2.VideoWriter_fourcc(*'XVID')
  
  out = cv2.VideoWriter(os.path.join(IMAGE_DIR,'outpy.avi'),fourcc, 1, (frame_width,frame_height))
  
  
  while capture.isOpened():
      ret, frame = capture.read()
      if(ret):
          results = model.detect([frame], verbose=1)
          r = results[0]
          image = display_instances(
          frame, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
          cv2.imshow('frame', image)
          out.write(image)
          if cv2.waitKey(1) & 0xFF == ord('q'):
              break
  
  capture.release()
  out.release()
  cv2.destroyAllWindows()
  
  
  # Load a random image from the images folder
  #file_names = next(os.walk(IMAGE_DIR))[2]
  #image = skimage.io.imread(os.path.join(IMAGE_DIR, '1045023827_4ec3e8ba5c_z.jpg'))
  
  # Run detection
  #results = model.detect([image], verbose=1)
  
  # Visualize results
  #r = results[0]
  #visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'],
                              #class_names, r['scores'])
  
  #image = skimage.io.imread(os.path.join(IMAGE_DIR,image_name))
  #results = model.detect([image], verbose=1)
  #r = results[0]
  
  #visualize.save_image(image, image_name, r['rois'], r['masks'],
  #    r['class_ids'],r['scores'],coco_class_names,
  #    filter_classs_names=['bottle', 'wine glass'],scores_thresh=0.9,mode=0)
  