text: import cv2
  import os
  from io import BytesIO
  import tarfile
  import tempfile
  from six.moves import urllib
  #from matplotlib import gridspec
  #from matplotlib import pyplot as plt
  import numpy as np
  from PIL import Image
  import tensorflow as tf
  
  
  ###### OpenCV position of click
  point = []
  def click_mouse(event,x,y,flags,params):
    # grab references to the global variables
  	global point
   
  	# if the left mouse button was clicked, record the starting
  	# (x, y) coordinates and indicate that cropping is being
  	# performed
  	if event == cv2.EVENT_LBUTTONDOWN:
  		point = [(x, y)]
  
  
  
  ###### model
  class DeepLabModel(object):
    """Class to load deeplab model and run inference."""
  
    INPUT_TENSOR_NAME = 'ImageTensor:0'
    OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
    INPUT_SIZE = 513
    FROZEN_GRAPH_NAME = 'frozen_inference_graph'
  
    def __init__(self, tarball_path):
      """Creates and loads pretrained deeplab model."""
      self.graph = tf.Graph()
  
      graph_def = None
      # Extract frozen graph from tar archive.
      tar_file = tarfile.open(tarball_path)
      for tar_info in tar_file.getmembers():
        if self.FROZEN_GRAPH_NAME in os.path.basename(tar_info.name):
          file_handle = tar_file.extractfile(tar_info)
          graph_def = tf.GraphDef.FromString(file_handle.read())
          break
  
      tar_file.close()
  
      if graph_def is None:
        raise RuntimeError('Cannot find inference graph in tar archive.')
  
      with self.graph.as_default():
        tf.import_graph_def(graph_def, name='')
  
      self.sess = tf.Session(graph=self.graph)
  
  
      
  
    def run(self, image):
      """Runs inference on a single image.
  
      Args:
        image: A PIL.Image object, raw input image.
  
      Returns:
        resized_image: RGB image resized from original input image.
        seg_map: Segmentation map of `resized_image`.
      """
      width, height = image.size
      resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
      target_size = (int(resize_ratio * width), int(resize_ratio * height))
      resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
      batch_seg_map = self.sess.run(
          self.OUTPUT_TENSOR_NAME,
          feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
      seg_map = batch_seg_map[0]
      return resized_image, seg_map
  
  
  def create_pascal_label_colormap():
    """Creates a label colormap used in PASCAL VOC segmentation benchmark.
  
    Returns:
      A Colormap for visualizing segmentation results.
    """
    colormap = np.zeros((256, 3), dtype=int)
    ind = np.arange(256, dtype=int)
  
    for shift in reversed(range(8)):
      for channel in range(3):
        colormap[:, channel] |= ((ind >> channel) & 1) << shift
      ind >>= 3
  
    return colormap
  
  
  def label_to_color_image(label):
    """Adds color defined by the dataset colormap to the label.
  
    Args:
      label: A 2D array with integer type, storing the segmentation label.
  
    Returns:
      result: A 2D array with floating type. The element of the array
        is the color indexed by the corresponding element in the input label
        to the PASCAL color map.
  
    Raises:
      ValueError: If label is not of rank 2 or its value is larger than color
        map maximum entry.
    """
    if label.ndim != 2:
      raise ValueError('Expect 2-D input label')
  
    colormap = create_pascal_label_colormap()
  
    if np.max(label) >= len(colormap):
      raise ValueError('label value too large.')
  
    return colormap[label]
  
  
  
  
  ################## model
  #@title Select and download models {display-mode: "form"}
  
  """ MODEL_NAME = 'mobilenetv2_coco_voctrainaug'  # @param ['mobilenetv2_coco_voctrainaug', 'mobilenetv2_coco_voctrainval', 'xception_coco_voctrainaug', 'xception_coco_voctrainval']
  
  _DOWNLOAD_URL_PREFIX = 'http://download.tensorflow.org/models/'
  _MODEL_URLS = {
      'mobilenetv2_coco_voctrainaug':
          'deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz',
      'mobilenetv2_coco_voctrainval':
          'deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz',
      'xception_coco_voctrainaug':
          'deeplabv3_pascal_train_aug_2018_01_04.tar.gz',
      'xception_coco_voctrainval':
          'deeplabv3_pascal_trainval_2018_01_04.tar.gz',
  }
  _TARBALL_NAME = 'deeplab_model.tar.gz'
  
  model_dir = tempfile.mkdtemp()
  tf.gfile.MakeDirs(model_dir)
  
  download_path = os.path.join(model_dir, _TARBALL_NAME)
  print('downloading model, this might take a while...')
  urllib.request.urlretrieve(_DOWNLOAD_URL_PREFIX + _MODEL_URLS[MODEL_NAME],
                     download_path)
  print('download completed! loading DeepLab model...') """
  
  download_path='/app/models/deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz'
  
  MODEL = DeepLabModel(download_path)
  print('model loaded successfully!')
  
  
  ##################### run
  
  
  
  
  # webcam
  # Create a VideoCapture object
  cap = cv2.VideoCapture("/dev/video0")
   
  # Check if camera opened successfully
  if (cap.isOpened() == False): 
    print("Unable to read camera feed")
   
  # Default resolutions of the frame are obtained.The default resolutions are system dependent.
  # We convert the resolutions from float to integer.
  frame_width = int(cap.get(3))
  frame_height = int(cap.get(4))
  
  
  while(True):
    ret, frame = cap.read()
    cv2.namedWindow('frame')
    cv2.setMouseCallback("frame", click_mouse)
  
    if ret == True: 
  # From cv2 to PIL
      cv2_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
      pil_im = Image.fromarray(cv2_im)
    # running inference
      resized_im, seg_map = MODEL.run(pil_im)
  
      seg_image = label_to_color_image(seg_map).astype(np.uint8)
  
      res=cv2.resize(seg_image,(frame_width,frame_height),interpolation=cv2.INTER_CUBIC)
  
      #print(np.shape(seg_image))
      dst = cv2.addWeighted(frame,0.7,res,0.3,0)
  
      retval,mask_img = cv2.threshold(res, 0,1,cv2.THRESH_BINARY)
      img=frame*mask_img
      #img=img and res
      # Display the resulting frame    
      cv2.imshow('frame',dst)
  
      cv2.imshow('frame2',img)
      if point not None:
        print(point)
      
  
  
      # Press Q on keyboard to stop recording
      if cv2.waitKey(1) & 0xFF == ord('q'):
        break
   
    # Break the loop
    else:
      break 
   
  # When everything done, release the video capture and video write objects
  cap.release()
  
   
  # Closes all the frames
  cv2.destroyAllWindows() 
  
  