Example #1
0
 def testFirstColorInADE20KColorMap(self):
     label = np.array([[1, 3], [10, 20]])
     expected_result = np.array([[[120, 120, 120], [6, 230, 230]],
                                 [[4, 250, 7], [204, 70, 3]]])
     colored_label = get_dataset_colormap.label_to_color_image(
         label, get_dataset_colormap.get_ade20k_name())
     self.assertTrue(np.array_equal(colored_label, expected_result))
Example #2
0
def image_inference(model_name, picure, label_color, output_name,
                    output_name_color):
    #run single image on model
    MODEL = DeepLabModel(model_name)
    print('model loaded successfully!')

    image = Image.open(picure)
    resized_im, seg_map = MODEL.run(image)
    seg_image = get_dataset_colormap.label_to_color_image(
        seg_map, label_color).astype(np.uint8)

    resized_im = np.array(resized_im)
    color_and_mask = cv2.addWeighted(resized_im, 0.4, seg_image, 0.6, 0.0)

    resized_im = Image.fromarray(resized_im, 'RGB')
    seg_image = Image.fromarray(seg_image, 'RGB')
    color_and_mask = Image.fromarray(color_and_mask, 'RGB')

    #resized_im = cv2.cvtColor(resized_im, cv2.COLOR_RGB2BGR)
    #seg_image.show()
    global start_t
    duration = time.time() - start_t
    print(duration)
    duration = format(duration, '.4f')

    seg_image.save('static/' + output_name, format='PNG')
    color_and_mask.save('static/' + output_name_color, format='PNG')

    return duration
Example #3
0
def save_annotation(label,
                    save_dir,
                    filename,
                    add_colormap=True,
                    colormap_type=get_dataset_colormap.get_pascal_name()):
    """Saves the given label to image on disk.

  Args:
    label: The numpy array to be saved. The data will be converted
      to uint8 and saved as png image.
    save_dir: The directory to which the results will be saved.
    filename: The image filename.
    add_colormap: Add color map to the label or not.
    colormap_type: Colormap type for visualization.
  """
    # Add colormap for visualizing the prediction.
    if add_colormap:
        colored_label = get_dataset_colormap.label_to_color_image(
            label, colormap_type)
    else:
        colored_label = label

    pil_image = img.fromarray(colored_label.astype(dtype=np.uint8))
    with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f:
        pil_image.save(f, 'PNG')
Example #4
0
def save_annotation(label,
                    save_dir,
                    filename,
                    add_colormap=True,
                    colormap_type=get_dataset_colormap.get_pascal_name()):
  """Saves the given label to image on disk.

  Args:
    label: The numpy array to be saved. The data will be converted
      to uint8 and saved as png image.
    save_dir: The directory to which the results will be saved.
    filename: The image filename.
    add_colormap: Add color map to the label or not.
    colormap_type: Colormap type for visualization.
  """
  # Add colormap for visualizing the prediction.
  if add_colormap:
    colored_label = get_dataset_colormap.label_to_color_image(
        label, colormap_type)
  else:
    colored_label = label

  pil_image = img.fromarray(colored_label.astype(dtype=np.uint8))
  with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f:
    pil_image.save(f, 'PNG')
Example #5
0
 def testLabelToPASCALColorImage(self):
     """Test the value of the converted label value."""
     label = np.array([[0, 16, 16], [52, 7, 52]])
     expected_result = np.array([[[0, 0, 0], [0, 64, 0], [0, 64, 0]],
                                 [[0, 64, 192], [128, 128, 128],
                                  [0, 64, 192]]])
     colored_label = get_dataset_colormap.label_to_color_image(
         label, get_dataset_colormap.get_pascal_name())
     self.assertTrue(np.array_equal(expected_result, colored_label))
 def testLabelToPASCALColorImage(self):
   """Test the value of the converted label value."""
   label = np.array([[0, 16, 16], [52, 7, 52]])
   expected_result = np.array([
       [[0, 0, 0], [0, 64, 0], [0, 64, 0]],
       [[0, 64, 192], [128, 128, 128], [0, 64, 192]]
   ])
   colored_label = get_dataset_colormap.label_to_color_image(
       label, get_dataset_colormap.get_pascal_name())
   self.assertTrue(np.array_equal(expected_result, colored_label))
Example #7
0
def save_annotation(label,
                    save_dir,
                    filename,
                    add_colormap=True,
                    normalize_to_unit_values=False,
                    scale_values=False,
                    colormap_type=get_dataset_colormap.get_pascal_name()):
    """Saves the given label to image on disk.

  Args:
    label: The numpy array to be saved. The data will be converted
      to uint8 and saved as png image.
    save_dir: String, the directory to which the results will be saved.
    filename: String, the image filename.
    add_colormap: Boolean, add color map to the label or not.
    normalize_to_unit_values: Boolean, normalize the input values to [0, 1].
    scale_values: Boolean, scale the input values to [0, 255] for visualization.
    colormap_type: String, colormap type for visualization.
  """
    # Add colormap for visualizing the prediction.
    if add_colormap:
        colored_label = get_dataset_colormap.label_to_color_image(
            label, colormap_type)
    else:
        colored_label = label
        if normalize_to_unit_values:
            min_value = np.amin(colored_label)
            max_value = np.amax(colored_label)
            range_value = max_value - min_value
            if range_value != 0:
                colored_label = (colored_label - min_value) / range_value

        if scale_values:
            colored_label = 255. * colored_label

    pil_image = img.fromarray(colored_label.astype(dtype=np.uint8))
    #  with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f:
    #    pil_image.save(f, 'PNG')
    pil_image.save('{}{}.png'.format(save_dir, filename))
Example #8
0
 def testUnExpectedLabelDimensionForLabelToADE20KColorImage(self):
     label = np.array([250])
     with self.assertRaises(ValueError):
         get_dataset_colormap.label_to_color_image(
             label, get_dataset_colormap.get_ade20k_name())
Example #9
0
 def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self):
     """Raise ValueError if input dimension is not correct."""
     label = np.array([120])
     with self.assertRaises(ValueError):
         get_dataset_colormap.label_to_color_image(
             label, get_dataset_colormap.get_pascal_name())
Example #10
0
 def testUnExpectedLabelValueForLabelToPASCALColorImage(self):
     """Raise ValueError when input value exceeds range."""
     label = np.array([[120], [300]])
     with self.assertRaises(ValueError):
         get_dataset_colormap.label_to_color_image(
             label, get_dataset_colormap.get_pascal_name())
Example #11
0
def main(_):

    output_dir = os.path.expanduser(FLAGS.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    start = time.time()
    host, port = FLAGS.server.split(':')
    channel = implementations.insecure_channel(host, int(port))
    stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    start = time.time()
    #data, w, h = cvimread(FLAGS.image)
    #print('img load: ',time.time()-start)
    image_paths = []

    if FLAGS.input_dir != '':
        files = FLAGS.input_dir + '/*.jpg'
        for image_path in glob.glob(files):
            image_paths.append(image_path)
    else:
        image_paths.append(FLAGS.image)

    img_length = len(image_paths)
    batch_shape = (img_length, ) + (380, 513, 3)
    #batch_shape = (img_length,)+(INPUT_SIZE, INPUT_SIZE, 3)
    #X_batch = np.zeros(batch_shape, dtype=np.float32)
    X_batch = np.zeros(batch_shape, dtype=np.uint8)
    print(X_batch.shape)

    i = 0
    for image_path in image_paths:

        #print(w,h)
        #print(data.shape)
        print(image_path)

        filename = os.path.splitext(os.path.split(image_path)[1])[0]
        output_filename = os.path.join(output_dir, filename + '.pkl')

        #if os.path.isfile(output_filename):
        #  continue

        data, (w, h), resized = load_input(image_path)
        print(w, h)
        #X_batch[i]=data
        i = i + 1

    request = predict_pb2.PredictRequest()
    request.model_spec.name = FLAGS.model
    request.model_spec.signature_name = 'predict_seg'
    request.inputs['img'].CopyFrom(
        tf.contrib.util.make_tensor_proto(data, shape=(1, h, w, 3)))
    #tf.contrib.util.make_tensor_proto(X_batch, shape=X_batch.shape))

    start = time.time()
    out = stub.Predict(request, 30.0)  # 5 secs timeout
    print('network: ', time.time() - start)

    print(out.outputs['seg'].tensor_shape)
    #import sys
    #sys.exit(1)

    shape = []
    shape.append(out.outputs['seg'].tensor_shape.dim[1].size)
    shape.append(out.outputs['seg'].tensor_shape.dim[2].size)

    seg = np.asarray(out.outputs['seg'].int64_val, np.int64)
    seg = seg.reshape(shape)

    pickle.dump(seg, gzip.open(output_filename, 'wb'))
    #segMap = pickle.load(gzip.open(output_filename,'rb'))

    seg_image = get_dataset_colormap.label_to_color_image(
        seg, get_dataset_colormap.get_pascal_name()).astype(np.uint8)
    cv2.imwrite('deeplab/seg_map.jpg', seg_image)

    img = np.clip(resized, 0, 255).astype('uint8')
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    overlay = cv2.addWeighted(img, 0.3, seg_image, 0.7, 0)
    cv2.imwrite('deeplab/overlay.jpg', overlay)
 def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self):
   """Raise ValueError if input dimension is not correct."""
   label = np.array([120])
   with self.assertRaises(ValueError):
     get_dataset_colormap.label_to_color_image(
         label, get_dataset_colormap.get_pascal_name())
 def testUnExpectedLabelValueForLabelToPASCALColorImage(self):
   """Raise ValueError when input value exceeds range."""
   label = np.array([[120], [300]])
   with self.assertRaises(ValueError):
     get_dataset_colormap.label_to_color_image(
         label, get_dataset_colormap.get_pascal_name())