コード例 #1
0
def segmentation_for_folder(input_folder, output_folder, color=None):
    """ Draws segmentation images for a given folder of labels

    Parameters
    ----------
    input_folder: str
                  path with json files / labels
    output_folder: str
                   folder to store segmentation images, cannot exist
    color: int, gray color value
           (int, int, int), BGR values
           None for default colors

    Returns nothing
    """
    # TODO Add color image option
    # TODO keep input name and folders
    if os.path.exists(output_folder):
        raise IOError(
            'Output folder already exists, stopping to not mess things up')
    os.makedirs(output_folder)

    input_labels = helper_scripts.get_files_from_folder(input_folder, '.json')

    for i, label_path in tqdm.tqdm(enumerate(input_labels)):
        segmentation_image = visualize_labels.create_segmentation_image(
            label_path, image='gray', color=color)
        cv2.imwrite(os.path.join(output_folder,
                                 str(i) + '.png'), segmentation_image)
コード例 #2
0
def load_labels(label_dir):
    """Loads the annotations and its paths
    Each annotation is converted to a list of points (x, y)
    """
    label_paths = helper_scripts.get_files_from_folder(label_dir, '.json')
    annos = [
        [
            add_ys(xs) for xs in
            spline_creator.get_horizontal_values_for_four_lanes(label_path)
            if (np.array(xs) >= 0).sum() > 1
        ]  # lanes annotated with a single point are ignored
        for label_path in label_paths
    ]
    label_paths = [helper_scripts.get_label_base(p) for p in label_paths]
    return np.array(annos, dtype=object), np.array(label_paths, dtype=object)
コード例 #3
0
def fix_names(input_folder, input_string, output_string):
    """ Changes all names within folder according to parameters

    Parameters
    ----------
    input_folder : str
                   folder containing inference images
    input_string : str
                   substring to be replace within each image
    output_string : str
                    what the input_string should be

    Notes
    -----
    This function is only needed if the scripts don't follow the
    expected naming conventions in the first place.
    """
    segmentation_images = helper_scripts.get_files_from_folder(input_folder, '.png')
    for segmentation_image in tqdm.tqdm(segmentation_images, desc='renaming images'):
        output_path = segmentation_image.replace(input_string, output_string)
        os.rename(segmentation_image, output_path)
コード例 #4
0
def folder_inference(checkpoint_file,
                     image_folder,
                     gray=True,
                     binary=True,
                     location=False,
                     suffix='_test'):
    """
    checkpoint_file: str, path to checkpoint, can also be folder
    tfrecord_file: str, path to file
    """
    out_folder = checkpoint_file + suffix

    input_images = helper_scripts.get_files_from_folder(image_folder, '.png')
    if suffix == '_test':
        assert len(input_images) == dataset_constants.NUM_TEST_IMAGES

    nm = NetModel(checkpoint=checkpoint_file)

    if location:
        # grad_x, grad_y = gradient_images()
        # grad_x_batch = numpy.expand_dims(numpy.asarray([grad_x]), -1)
        # grad_y_batch = numpy.expand_dims(numpy.asarray([grad_y]), -1)
        pass

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config):
        for image_path in tqdm.tqdm(input_images):

            base_path = image_path.replace(image_folder, '')
            base_path = base_path[1:] if base_path.startswith(
                '/') else base_path

            camera_image = cv2.imread(image_path, 0 if gray else 1)
            camera_image = numpy.expand_dims(camera_image, axis=0)

            if binary:
                camera_image = numpy.expand_dims(camera_image, axis=-1)
            if location:
                # Currently, there are images stored to file like this
                # So, this one needs to be implemented, should be < 5 lines
                raise NotImplementedError(
                    'Add gradient images to inference input')
            feed_dict = {'image_input:0': camera_image}
            prediction = nm.single_batch_inference(
                feed_dict)  # A bit bigger. Upsampling, padding

            # Multiply by 255 to get an actual image and stuff
            os.makedirs(os.path.dirname(os.path.join(out_folder, base_path)),
                        exist_ok=True)
            if binary:
                prediction = (expit(prediction)[0, :717, :, 0] -
                              1.0) * -1  # == softmax
                output_file = os.path.splitext(
                    os.path.join(out_folder, base_path))[0] + '.json_1.png'
                cv2.imwrite(output_file,
                            (prediction * 255).astype(numpy.uint8))
            else:
                prediction = prediction[0, :717, :, :]
                prediction = softmax(prediction, axis=2)
                for i in range(prediction.shape[-1]):
                    output_file = os.path.splitext(
                        os.path.join(
                            out_folder,
                            base_path))[0] + '.json_' + str(i) + '.png'
                    prediction_image = prediction[:, :, i]
                    cv2.imwrite(output_file,
                                (prediction_image * 255).astype(numpy.uint8))
コード例 #5
0
def create_deeplab_tfrecords(input_folder, tfrecord_file):
    """Creates a tfrecord file for a given folder

    Parameters:
        input_folder: str, path to samples for a given dataset
        tfrecord_file: str, path to tfrecord that will be created

    Flags:
        See docstring for more information
        color_input: whether to use gray or color images
        multi_class: binary or multi-class segmentation
        location_gradients: location information as extra channels
    """
    label_paths = helper_scripts.get_files_from_folder(input_folder, '.json')
    shuffle(label_paths)
    print('{} label files in {}'.format(len(label_paths), input_folder))

    loc_grad_x = list(
        map(lambda z: z / constants.IMAGE_WIDTH * 255,
            range(constants.IMAGE_WIDTH)))
    loc_grad_y = list(
        map(lambda z: z / constants.IMAGE_HEIGHT * 255,
            range(constants.IMAGE_HEIGHT)))
    loc_grad_x = numpy.asarray([loc_grad_x] * constants.IMAGE_HEIGHT)
    loc_grad_y = numpy.asarray([loc_grad_y] *
                               constants.IMAGE_WIDTH).transpose()
    loc_grad_x = numpy.round(loc_grad_x).astype(numpy.uint8)
    loc_grad_y = numpy.round(loc_grad_y).astype(numpy.uint8)

    os.makedirs(os.path.dirname(tfrecord_file), exist_ok=True)
    with tf.python_io.TFRecordWriter(tfrecord_file) as writer:
        for label_path in tqdm.tqdm(label_paths,
                                    total=len(label_paths),
                                    desc='Creating ' + tfrecord_file):

            image_name = os.path.basename(label_path).replace('.json', '')
            if FLAGS.color_input:
                image_data = label_file_scripts.read_image(label_path,
                                                           image_type='color')
            else:
                image_data = label_file_scripts.read_image(label_path,
                                                           image_type='gray')
                if FLAGS.location_gradients:
                    image_data = numpy.stack(
                        [image_data, loc_grad_x, loc_grad_y], -1)
            image_data = cv2.imencode('.png', image_data)[1].tostring()

            if FLAGS.multi_class:
                segmentation_label = segmentation_labels.create_multi_class_segmentation_label(
                    label_path)
                segmentation = numpy.zeros(segmentation_label.shape[0:2],
                                           numpy.uint8)
                for class_index in range(1, 5):
                    segmentation[segmentation_label[:, :, class_index] >
                                 0] = class_index
            else:
                segmentation = visualize_labels.create_segmentation_image(
                    label_path, image='blank')
                segmentation = cv2.cvtColor(segmentation, cv2.COLOR_BGR2GRAY)
                segmentation = segmentation > 0
                segmentation = segmentation.astype(numpy.uint8)

            segmentation = cv2.imencode('.png', segmentation)[1].tostring()

            example = build_data.image_seg_to_tfexample(
                image_data, image_name, constants.IMAGE_HEIGHT,
                constants.IMAGE_WIDTH, segmentation)

            writer.write(example.SerializeToString())
コード例 #6
0
def check_labels(input_folder):
    """ Checks if labels within folder are readable """
    label_files = helper_scripts.get_files_from_folder(input_folder, 'json')
    for label_file in tqdm.tqdm(label_files, desc='checking labels'):
        with open(label_file, 'r') as lf:
            json.load(lf)  # Just to check if json syntax is correct