//  [python]
def main(unused_argv):
  del unused_argv
  # Load the label map.
  print(' - Loading the label map...')
  label_map_dict = {}
  if FLAGS.label_map_format == 'csv':
    with tf.gfile.Open(FLAGS.label_map_file, 'r') as csv_file:
      reader = csv.reader(csv_file, delimiter=':')
      for row in reader:
        if len(row) != 2:
          raise ValueError('Each row of the csv label map file must be in '
                           '`id:name` format.')
        id_index = int(row[0])
        name = row[1]
        label_map_dict[id_index] = {
            'id': id_index,
            'name': name,
        }
  else:
    raise ValueError(
        'Unsupported label map format: {}.'.format(FLAGS.label_map_format))

  with tf.Session(graph=tf.Graph()) as sess:
    print(' - Loading saved model...')
    meta_graph_def = tf.saved_model.load(
        sess, [tf.saved_model.tag_constants.SERVING], FLAGS.saved_model_dir)
    inputs = dict(meta_graph_def.signature_def['serving_default'].inputs)
    outputs = dict(meta_graph_def.signature_def['serving_default'].outputs)

    image_node = inputs['input'].name

    output_nodes = {
        'num_detections': outputs['num_detections'].name,
        'detection_boxes': outputs['detection_boxes'].name,
        'detection_classes': outputs['detection_classes'].name,
        'detection_scores': outputs['detection_scores'].name,
    }
    if 'detection_masks' in outputs:
      output_nodes['detection_masks'] = outputs['detection_masks'].name
    if not FLAGS.use_normalized_coordinates:
      if 'image_info' not in outputs:
        raise ValueError('If `use_normalized_coordinates` = False, `image_info`'
                         ' node must be included in the SavedModel.')
      output_nodes['image_info'] = outputs['image_info'].name

    image_with_detections_list = []
    image_files = tf.gfile.Glob(FLAGS.image_file_pattern)
    for i, image_file in enumerate(image_files):
      print(' - processing image %d...' % i)

      image = Image.open(image_file)
      image = image.convert('RGB')  # needed for images with 4 channels.
      width, height = image.size

      np_image = (np.array(image.getdata())
                  .reshape(height, width, 3).astype(np.uint8))
      np_image_input = input_utils.normalize_image_np(np_image)
      np_image_input = np.float32(np_image_input.reshape(1, height, width, 3))

      output_results = sess.run(output_nodes,
                                feed_dict={image_node: np_image_input})

      num_detections = int(output_results['num_detections'][0])
      np_boxes = output_results['detection_boxes'][0, :num_detections]
      if not FLAGS.use_normalized_coordinates:
        np_image_info = output_results['image_info'][0]
        np_boxes = np_boxes / np.tile(np_image_info[1:2, :], (1, 2))
      ymin, xmin, ymax, xmax = np.split(np_boxes, 4, axis=-1)
      ymin = ymin * height
      ymax = ymax * height
      xmin = xmin * width
      xmax = xmax * width
      np_boxes = np.concatenate([ymin, xmin, ymax, xmax], axis=-1)
      np_scores = output_results['detection_scores'][0, :num_detections]
      np_classes = output_results['detection_classes'][0, :num_detections]
      np_classes = np_classes.astype(np.int32)
      np_masks = None
      if 'detection_masks' in output_results:
        np_masks = output_results['detection_masks'][0, :num_detections]
        np_masks = mask_utils.paste_instance_masks(
            np_masks, box_utils.yxyx_to_xywh(np_boxes), height, width)

      image_with_detections = (
          visualization_utils.visualize_boxes_and_labels_on_image_array(
              np_image,
              np_boxes,
              np_classes,
              np_scores,
              label_map_dict,
              instance_masks=np_masks,
              use_normalized_coordinates=False,
              max_boxes_to_draw=FLAGS.max_boxes_to_draw,
              min_score_thresh=FLAGS.min_score_threshold))
      image_with_detections_list.append(image_with_detections)

    print(' - Saving the outputs...')
    formatted_image_with_detections_list = [
        Image.fromarray(image.astype(np.uint8))
        for image in image_with_detections_list]
    html_str = '<html>'
    image_strs = []
    for formatted_image in formatted_image_with_detections_list:
      with io.BytesIO() as stream:
        formatted_image.save(stream, format='JPEG')
        data_uri = base64.b64encode(stream.getvalue()).decode('utf-8')
      image_strs.append(
          '<img src="data:image/jpeg;base64,{}", height=800>'
          .format(data_uri))
    images_str = ' '.join(image_strs)
    html_str += images_str
    html_str += '</html>'
    with tf.gfile.GFile(FLAGS.output_html, 'w') as f:
      f.write(html_str)