def convert_segmentation_images(input_path, output_path, classes_array):

    filenames = os.listdir(str(data_folder / input_path))

    output_folder = str(data_folder / output_path)
    verify_folder_exists(output_folder)

    i = 0
    for filename in filenames:

        # In BGR format
        img = cv2.imread(str(data_folder / input_path / filename))
        output_img = np.zeros(img.shape)

        # For each pixel:
        #  If pixel.color in road colors: set pixel to (0,0,0)
        #  If pixel.color in lane marking colors: set pixel to (1,0,0)
        #  Else: set pixel to (2,0,0)

        for j, class_colors in enumerate(classes_array):
            for color in class_colors:
                mask = (img == color[::-1]).all(axis=2)
                output_img[mask] = [j + 1, 0, 0]

        write_path = str(Path(data_folder) / output_path / filename)

        cv2.imwrite(write_path, output_img)

        i += 1
        if i % 100 == 0:
            print("Progress: ", i, " of ", len(filenames))
def convert_carla_depth_imgs(input_path, output_path):

    filenames = os.listdir(str(data_folder / input_path))

    output_folder = str(data_folder / output_path)
    verify_folder_exists(output_folder)

    i = 0
    for filename in filenames:

        img = cv2.imread(str(data_folder / input_path / filename))

        # Carla data is logarithmic. Reverse their algorithm: https://github.com/carla-simulator/driving-benchmarks/blob/master/version084/carla/image_converter.py
        normalized_img = np.exp(
            ((img / 255) - np.ones(img.shape)) * 5.70378) * 255

        normalized_img[:, :, 0] = 255 - normalized_img[:, :, 0]
        normalized_img[:, :, 1] = 0
        normalized_img[:, :, 2] = 0

        write_path = str(Path(data_folder) / output_path / filename)

        cv2.imwrite(write_path, normalized_img)

        i += 1
        if i % 100 == 0:
            print("Progress: ", i, " of ", len(filenames))
def copy_cityscapes_files(input_path, output_path, only_matching=None):
    verify_folder_exists(output_path)
    input_path = Path(input_path)
    for folder in os.listdir(input_path):
        for filename in os.listdir(str(input_path / folder)):
            if only_matching is not None:
                if not only_matching in filename:
                    continue
            output_name = str(output_path) + "/" + str(folder + str(filename))

            copyfile(str(input_path / folder / filename), output_name)
示例#4
0
def segment_images_psp(path_to_checkpoint, folder_name):

    output_folder_name = folder_name + "_segm"
    checkpoint = model_from_checkpoint_path(path_to_checkpoint)
    #checkpoint = pspnet_101_cityscapes()
    output_folder = data_folder / output_folder_name
    verify_folder_exists(output_folder)

    for filename in os.listdir(str(data_folder / folder_name)):
        out_name = str(output_folder / filename).replace("jpg", "png")

        in_name = str(data_folder / folder_name / filename)

        checkpoint.predict_segmentation(out_fname=out_name, inp=in_name)
示例#5
0
def visualize_segm_depth(path_to_checkpoint, folder_name):

    output_segm_folder_name = folder_name + "_depth_segm"
    checkpoint = unet_model_from_checkpoint_path(path_to_checkpoint)

    plot_model(checkpoint, "model.png")
    print(checkpoint.summary())
    output_segm_folder = data_folder / output_segm_folder_name

    verify_folder_exists(output_segm_folder)

    for filename in os.listdir(str(data_folder / folder_name)):
        out_segm_name = str(output_segm_folder / filename).replace(
            "jpg", "png")

        in_name = str(data_folder / folder_name / filename)

        checkpoint.predict_segmentation(out_fname=out_segm_name, inp=in_name)