def display_data_scenes(p_scene, p_bits, p_shifted):
    """
    @brief Method which generates all .csv files from scenes photos
    @param p_scene, scene we want to show values
    @param nb_bits, number of bits expected
    @param p_shifted, number of bits expected to be shifted
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            print(folder_scene)
            scene_path = os.path.join(path, folder_scene)

            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            zones_images_data = []
            threshold_info = []

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            start_image_path = scene_images[0]
            end_image_path = scene_images[-1]

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image = dt.get_scene_image_quality(scene_images[-1])

            for id_zone, zone_folder in enumerate(zones_folder):

                zone_path = os.path.join(scene_path, zone_folder)

                # get threshold information
                path_seuil = os.path.join(zone_path, seuil_expe_filename)

                # open treshold path and get this information
                with open(path_seuil, "r") as seuil_file:
                    threshold_learned = int(seuil_file.readline().strip())

                threshold_image_found = False

                # for each images
                for img_path in scene_images:
                    current_quality_image = dt.get_scene_image_quality(
                        img_path)

                    if threshold_learned < int(current_quality_image
                                               ) and not threshold_image_found:

                        threshold_image_found = True
                        threshold_image_path = img_path

                        threshold_image = dt.get_scene_image_postfix(img_path)
                        threshold_info.append(threshold_image)

                # all indexes of picture to plot
                images_path = [
                    start_image_path, threshold_image_path, end_image_path
                ]
                images_data = []

                for img_path in images_path:

                    current_img = Image.open(img_path)
                    img_blocks = segmentation.divide_in_blocks(
                        current_img, (200, 200))

                    # getting expected block id
                    block = img_blocks[id_zone]

                    # get data from mode
                    # Here you can add the way you compute data
                    low_bits_block = transform.rgb_to_LAB_L_bits(
                        block, (p_shifted + 1, p_shifted + p_bits + 1))
                    data = compression.get_SVD_s(low_bits_block)

                    ##################
                    # Data mode part #
                    ##################

                    # modify data depending mode
                    data = utils.normalize_arr(data)
                    images_data.append(data)

                zones_images_data.append(images_data)

            fig = plt.figure(figsize=(8, 8))
            fig.suptitle('Lab SVD ' + str(p_bits) + ' bits shifted by ' +
                         str(p_shifted) + " for " + p_scene + " scene",
                         fontsize=20)

            for id, data in enumerate(zones_images_data):
                fig.add_subplot(4, 4, (id + 1))
                plt.plot(data[0], label='Noisy_' + start_quality_image)
                plt.plot(data[1], label='Threshold_' + threshold_info[id])
                plt.plot(data[2], label='Reference_' + end_quality_image)
                plt.ylabel('Lab SVD ' + str(p_bits) + ' bits shifted by ' +
                           str(p_shifted) + ', ZONE_' + str(id + 1),
                           fontsize=14)
                plt.xlabel('Vector features', fontsize=16)
                plt.legend(bbox_to_anchor=(0.5, 1),
                           loc=2,
                           borderaxespad=0.2,
                           fontsize=14)
                plt.ylim(0, 0.1)
            plt.show()
Exemple #2
0
def main():

    p_custom = False

    parser = argparse.ArgumentParser(
        description="Script which predicts threshold using specific model")

    parser.add_argument('--interval',
                        type=str,
                        help='Interval value to keep from svd',
                        default='"0, 200"')
    parser.add_argument('--model',
                        type=str,
                        help='.joblib or .json file (sklearn or keras model)')
    parser.add_argument('--mode',
                        type=str,
                        help='Kind of normalization level wished',
                        choices=normalization_choices)
    parser.add_argument('--feature',
                        type=str,
                        help='Feature data choice',
                        choices=features_choices)
    parser.add_argument(
        '--limit_detection',
        type=int,
        help='Specify number of same prediction to stop threshold prediction',
        default=2)
    parser.add_argument(
        '--custom',
        type=str,
        help='Name of custom min max file if use of renormalization of data',
        default=False)

    args = parser.parse_args()

    p_interval = list(map(int, args.interval.split(',')))
    p_model_file = args.model
    p_mode = args.mode
    p_feature = args.feature
    p_limit = args.limit
    p_custom = args.custom

    scenes = os.listdir(scenes_path)
    scenes = [s for s in scenes if not min_max_filename in s]

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        print(folder_scene)

        scene_path = os.path.join(scenes_path, folder_scene)

        threshold_expes = []
        threshold_expes_detected = []
        threshold_expes_counter = []
        threshold_expes_found = []

        # get all images of folder
        scene_images = sorted([
            os.path.join(scene_path, img) for img in os.listdir(scene_path)
            if cfg.scene_image_extension in img
        ])

        start_quality_image = dt.get_scene_image_quality(scene_images[0])
        end_quality_image = dt.get_scene_image_quality(scene_images[-1])

        # get zones list info
        for index in zones:
            index_str = str(index)
            if len(index_str) < 2:
                index_str = "0" + index_str
            zone_folder = "zone" + index_str

            threshold_path_file = os.path.join(
                os.path.join(scene_path, zone_folder), threshold_expe_filename)

            with open(threshold_path_file) as f:
                threshold = int(f.readline())
                threshold_expes.append(threshold)

                # Initialize default data to get detected model threshold found
                threshold_expes_detected.append(False)
                threshold_expes_counter.append(0)
                threshold_expes_found.append(
                    end_quality_image)  # by default use max

        check_all_done = False

        # for each images
        for img_path in scene_images:

            current_img = Image.open(img_path)
            current_quality_image = dt.get_scene_image_quality(img_path)
            current_image_potfix = dt.get_scene_image_postfix(img_path)

            img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))
            current_img = Image.open(img_path)
            img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))

            check_all_done = all(d == True for d in threshold_expes_detected)

            if check_all_done:
                break

            for id_block, block in enumerate(img_blocks):

                # check only if necessary for this scene (not already detected)
                if not threshold_expes_detected[id_block]:

                    tmp_file_path = tmp_filename.replace(
                        '__model__',
                        p_model_file.split('/')[-1].replace('.joblib', '_'))
                    block.save(tmp_file_path)

                    python_cmd = "python prediction/predict_noisy_image_svd.py --image " + tmp_file_path + \
                                    " --interval '" + p_interval + \
                                    "' --model " + p_model_file  + \
                                    " --mode " + p_mode + \
                                    " --feature " + p_feature

                    # specify use of custom file for min max normalization
                    if p_custom:
                        python_cmd = python_cmd + ' --custom ' + p_custom

                    ## call command ##
                    p = subprocess.Popen(python_cmd,
                                         stdout=subprocess.PIPE,
                                         shell=True)

                    (output, err) = p.communicate()

                    ## Wait for result ##
                    p_status = p.wait()

                    prediction = int(output)

                    if prediction == 0:
                        threshold_expes_counter[
                            id_block] = threshold_expes_counter[id_block] + 1
                    else:
                        threshold_expes_counter[id_block] = 0

                    if threshold_expes_counter[id_block] == p_limit:
                        threshold_expes_detected[id_block] = True
                        threshold_expes_found[id_block] = current_quality_image

                    print(
                        str(id_block) + " : " + current_image_potfix + "/" +
                        str(threshold_expes[id_block]) + " => " +
                        str(prediction))

            print("------------------------")
            print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
            print("------------------------")

        # end of scene => display of results

        # construct path using model name for saving threshold map folder
        model_treshold_path = os.path.join(
            threshold_map_folder,
            p_model_file.split('/')[-1].replace('.joblib', ''))

        # create threshold model path if necessary
        if not os.path.exists(model_treshold_path):
            os.makedirs(model_treshold_path)

        abs_dist = []

        map_filename = os.path.join(model_treshold_path,
                                    threshold_map_file_prefix + folder_scene)
        f_map = open(map_filename, 'w')

        line_information = ""

        # default header
        f_map.write('|  |    |    |  |\n')
        f_map.write('---|----|----|---\n')
        for id, threshold in enumerate(threshold_expes_found):

            line_information += str(threshold) + " / " + str(
                threshold_expes[id]) + " | "
            abs_dist.append(abs(threshold - threshold_expes[id]))

            if (id + 1) % 4 == 0:
                f_map.write(line_information + '\n')
                line_information = ""

        f_map.write(line_information + '\n')

        min_abs_dist = min(abs_dist)
        max_abs_dist = max(abs_dist)
        avg_abs_dist = sum(abs_dist) / len(abs_dist)

        f_map.write('\nScene information : ')
        f_map.write('\n- BEGIN : ' + str(start_quality_image))
        f_map.write('\n- END : ' + str(end_quality_image))

        f_map.write('\n\nDistances information : ')
        f_map.write('\n- MIN : ' + str(min_abs_dist))
        f_map.write('\n- MAX : ' + str(max_abs_dist))
        f_map.write('\n- AVG : ' + str(avg_abs_dist))

        f_map.write('\n\nOther information : ')
        f_map.write('\n- Detection limit : ' + str(p_limit))

        # by default print last line
        f_map.close()

        print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)) +
              " Done..")
        print("------------------------")
Exemple #3
0
def display_data_scenes(data_type, p_scene, p_kind):
    """
    @brief Method which displays data from scene
    @param data_type,  feature choice
    @param scene, scene choice
    @param mode, normalization choice
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            print(folder_scene)
            scene_path = os.path.join(path, folder_scene)

            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            zones_images_data = []
            threshold_info = []

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            start_image_path = scene_images[0]
            end_image_path = scene_images[-1]

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image = dt.get_scene_image_quality(scene_images[-1])

            for id_zone, zone_folder in enumerate(zones_folder):

                zone_path = os.path.join(scene_path, zone_folder)

                # get threshold information
                path_seuil = os.path.join(zone_path, seuil_expe_filename)

                # open treshold path and get this information
                with open(path_seuil, "r") as seuil_file:
                    threshold_learned = int(seuil_file.readline().strip())

                threshold_image_found = False

                for img_path in scene_images:
                    current_quality_image = dt.get_scene_image_quality(
                        img_path)

                    if threshold_learned < int(current_quality_image
                                               ) and not threshold_image_found:

                        threshold_image_found = True
                        threshold_image_path = img_path

                        threshold_image = dt.get_scene_image_postfix(img_path)
                        threshold_info.append(threshold_image)

                # all indexes of picture to plot
                images_path = [
                    start_image_path, threshold_image_path, end_image_path
                ]
                images_data = []

                for img_path in images_path:

                    current_img = Image.open(img_path)
                    img_blocks = segmentation.divide_in_blocks(
                        current_img, (200, 200))

                    # getting expected block id
                    block = img_blocks[id_zone]

                    data = get_image_features(data_type, block)

                    ##################
                    # Data mode part #
                    ##################

                    # modify data depending mode

                    if p_kind == 'svdn':
                        data = utils.normalize_arr(data)

                    if p_kind == 'svdne':
                        path_min_max = os.path.join(
                            path, data_type + min_max_filename)

                        with open(path_min_max, 'r') as f:
                            min_val = float(f.readline())
                            max_val = float(f.readline())

                        data = utils.normalize_arr_with_range(
                            data, min_val, max_val)

                    # append of data
                    images_data.append(data)

                zones_images_data.append(images_data)

            fig = plt.figure(figsize=(8, 8))
            fig.suptitle(data_type + " values for " + p_scene +
                         " scene (normalization : " + p_kind + ")",
                         fontsize=20)

            for id, data in enumerate(zones_images_data):
                fig.add_subplot(4, 4, (id + 1))
                plt.plot(data[0], label='Noisy_' + start_quality_image)
                plt.plot(data[1], label='Threshold_' + threshold_info[id])
                plt.plot(data[2], label='Reference_' + end_quality_image)
                plt.ylabel(data_type + ' SVD, ZONE_' + str(id + 1),
                           fontsize=18)
                plt.xlabel('Vector features', fontsize=18)
                plt.legend(bbox_to_anchor=(0.5, 1),
                           loc=2,
                           borderaxespad=0.2,
                           fontsize=18)
                plt.ylim(0, 0.1)
            plt.show()
def main():

    parser = argparse.ArgumentParser(
        description="Compute sobel complexity on scene images")

    parser.add_argument(
        '--output',
        type=str,
        help='save complexity for each zone of each scene into file')
    parser.add_argument('--ksize',
                        type=int,
                        help='sobel kernel size',
                        default=3)
    parser.add_argument('--interval',
                        type=str,
                        help='svd interval to use',
                        default="0,200")
    parser.add_argument(
        '--imnorm',
        type=int,
        help="specify if image is normalized before computing something",
        default=0,
        choices=[0, 1])

    args = parser.parse_args()

    p_output = args.output
    p_ksize = args.ksize
    p_interval = tuple(map(int, args.interval.split(',')))
    p_imnorm = args.imnorm

    # create output path if not exists
    p_output_path = os.path.join(cfg.output_data_folder, cfg.data_generated,
                                 p_output)
    if not os.path.exists(cfg.output_data_folder):
        os.makedirs(os.path.join(cfg.output_data_folder, cfg.data_generated))

    zones_list = []

    # construct zones folder
    for index in zones_indices:

        index_str = str(index)

        while len(index_str) < 2:
            index_str = "0" + index_str

        zones_list.append(cfg.zone_folder + index_str)

    thresholds = {}
    images_path = {}
    number_of_images = 0

    # create dictionnary of threshold and get all images path
    for scene in scenes_list:

        scene_path = os.path.join(dataset_folder, scene)

        threshold_list = []

        for zone in zones_list:
            zone_path = os.path.join(scene_path, zone)

            with open(os.path.join(zone_path, cfg.seuil_expe_filename),
                      'r') as f:
                threshold_list.append(int(f.readline()))

        thresholds[scene] = threshold_list
        images_path[scene] = sorted([
            os.path.join(scene_path, img) for img in os.listdir(scene_path)
            if cfg.scene_image_extension in img
        ])
        number_of_images = number_of_images + len(images_path[scene])

    with open(p_output_path, 'w') as f:
        print("Erase", p_output_path, "previous file if exists")

    image_counter = 0
    # compute complexity for each zones of each scene images
    for scene in scenes_list:

        image_indices = [
            dt.get_scene_image_quality(img_path)
            for img_path in images_path[scene]
        ]

        blocks_complexity = []

        # append empty list
        for zone in zones_list:
            blocks_complexity.append([])

        for img_path in images_path[scene]:

            blocks = segmentation.divide_in_blocks(Image.open(img_path),
                                                   (200, 200))
            complexity_list = get_zone_sobel_svd_entropy(
                blocks, p_interval, p_ksize, p_imnorm)

            for index, complexity in enumerate(complexity_list):
                blocks_complexity[index].append(complexity)

            # write progress bar
            write_progress((image_counter + 1) / number_of_images)

            image_counter = image_counter + 1

        # write data into files
        with open(p_output_path, 'a') as f:
            for index, zone in enumerate(zones_list):
                f.write(scene + ';')
                f.write(str(index) + ';')
                f.write(zone + ';')

                f.write(str(thresholds[scene][index]) + ';')

                for index_img, img_quality in enumerate(image_indices):
                    f.write(str(img_quality))

                    if index_img + 1 < len(image_indices):
                        f.write(',')

                f.write(';')

                for index_v, v in enumerate(blocks_complexity[index]):
                    f.write(str(v))

                    if index_v + 1 < len(blocks_complexity[index]):
                        f.write(',')

                f.write(';\n')
def main():

    parser = argparse.ArgumentParser(
        description="Read and compute entropy data file")

    parser.add_argument('--model', type=str, help='model .h5 file')
    parser.add_argument('--folder',
                        type=str,
                        help='folder where scene dataset is available',
                        required=True)
    parser.add_argument(
        '--features',
        type=str,
        help="list of features choice in order to compute data",
        default='svd_reconstruction, ipca_reconstruction',
        required=True)
    parser.add_argument(
        '--params',
        type=str,
        help=
        "list of specific param for each feature choice (See README.md for further information in 3D mode)",
        default='100, 200 :: 50, 25',
        required=True)
    parser.add_argument('--size',
                        type=str,
                        help="specific size of image",
                        default='100, 100',
                        required=True)
    parser.add_argument('--n_stop',
                        type=int,
                        help='number of detection to make sure to stop',
                        default=1)
    parser.add_argument('--save',
                        type=str,
                        help='filename where to save input data')
    parser.add_argument('--label',
                        type=str,
                        help='label to use when saving thresholds')

    args = parser.parse_args()

    p_model = args.model
    p_folder = args.folder
    p_features = list(map(str.strip, args.features.split(',')))
    p_params = list(map(str.strip, args.params.split('::')))
    p_size = args.size
    p_n_stop = args.n_stop
    p_save = args.save
    p_label = args.label

    # 1. Load expected transformations

    # list of transformations
    transformations = []

    for id, feature in enumerate(p_features):

        if feature not in cfg.features_choices_labels or feature == 'static':
            raise ValueError(
                "Unknown feature, please select a correct feature (`static` excluded) : ",
                cfg.features_choices_labels)

        transformations.append(Transformation(feature, p_params[id], p_size))

    # 2. load model and compile it

    # TODO : check kind of model
    model = load_model(p_model)
    # model.compile(loss='binary_crossentropy',
    #               optimizer='rmsprop',
    #               metrics=['accuracy'])

    estimated_thresholds = []
    n_estimated_thresholds = []

    scene_path = p_folder

    if not os.path.exists(scene_path):
        print('Unvalid scene path:', scene_path)
        exit(0)

    # 3. retrieve human_thresholds
    # construct zones folder
    zones_indices = np.arange(16)
    zones_list = []

    for index in zones_indices:

        index_str = str(index)

        while len(index_str) < 2:
            index_str = "0" + index_str

        zones_list.append(cfg.zone_folder + index_str)

    # 4. get estimated thresholds using model and specific method
    images_path = sorted([
        os.path.join(scene_path, img) for img in os.listdir(scene_path)
        if cfg.scene_image_extension in img
    ])
    number_of_images = len(images_path)
    image_indices = [
        dt.get_scene_image_quality(img_path) for img_path in images_path
    ]

    image_counter = 0

    # append empty list
    for _ in zones_list:
        estimated_thresholds.append(None)
        n_estimated_thresholds.append(0)

    for img_i, img_path in enumerate(images_path):

        blocks = segmentation.divide_in_blocks(Image.open(img_path),
                                               (200, 200))

        for index, block in enumerate(blocks):

            if estimated_thresholds[index] is None:

                transformed_list = []
                # compute data here
                for transformation in transformations:
                    transformed = transformation.getTransformedImage(block)
                    transformed_list.append(transformed)

                data = np.array(transformed_list)

                # compute input size
                n_chanels, _, _ = data.shape

                if K.image_data_format() == 'chanels_first':
                    if n_chanels > 1:
                        data = np.expand_dims(data, axis=0)

                else:
                    if n_chanels > 1:
                        data = data.transpose()
                        data = np.expand_dims(data, axis=0)
                    else:
                        data = data.transpose()

                data = np.expand_dims(data, axis=0)

                probs = model.predict(np.array(data))[0]
                prediction = list(probs).index(max(probs))
                #print(index, ':', image_indices[img_i], '=>', prediction)

                if prediction == 0:
                    n_estimated_thresholds[index] += 1

                    # if same number of detection is attempted
                    if n_estimated_thresholds[index] >= p_n_stop:
                        estimated_thresholds[index] = image_indices[img_i]
                else:
                    n_estimated_thresholds[index] = 0

        # write progress bar
        write_progress((image_counter + 1) / number_of_images)

        image_counter = image_counter + 1

    # default label
    for i, _ in enumerate(zones_list):
        if estimated_thresholds[i] == None:
            estimated_thresholds[i] = image_indices[-1]

    # 6. save estimated thresholds into specific file
    print('\nEstimated thresholds', estimated_thresholds)
    if p_save is not None:
        with open(p_save, 'a') as f:
            f.write(p_label + ';')

            for t in estimated_thresholds:
                f.write(str(t) + ';')
            f.write('\n')
def main():

    parser = argparse.ArgumentParser(description="Read and compute entropy data file")

    parser.add_argument('--model', type=str, help='model file')
    parser.add_argument('--method', type=str, help='method name to used', choices=cfg.features_choices_labels, default=cfg.features_choices_labels[0])
    parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
    parser.add_argument('--kind', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
    parser.add_argument('--imnorm', type=int, help="specify if image is normalized before computing something", default=0, choices=[0, 1])
    parser.add_argument('--scene', type=str, help='Scene index to use', choices=cfg.scenes_indices)
    parser.add_argument('--save', type=str, help='filename where to save input data')
    parser.add_argument('--label', type=str, help='label to use when saving thresholds')

    args = parser.parse_args()

    p_model    = args.model
    p_method   = args.method
    p_interval = list(map(int, args.interval.split(',')))
    #p_n_stop   = args.n_stop
    p_imnorm   = args.imnorm
    p_scene    = args.scene
    p_mode     = args.kind
    p_save     = args.save
    p_label    = args.label

    p_n_stop = 1
    begin, end = p_interval

    # 1. get scene name
    scenes_list = cfg.scenes_names
    scenes_indices = cfg.scenes_indices

    scene_index = scenes_indices.index(p_scene.strip())
    scene = scenes_list[scene_index]

    scene_path = os.path.join(cfg.dataset_path, scene)

    # 2. load model and compile it

    # TODO : check kind of model
    model = joblib.load(p_model)
    # model.compile(loss='binary_crossentropy',
    #               optimizer='rmsprop',
    #               metrics=['accuracy'])


    estimated_thresholds = []
    n_estimated_thresholds = []
    human_thresholds = []

    # 3. retrieve human_thresholds
    # construct zones folder
    zones_list = []

    for index in zones_indices:

        index_str = str(index)

        while len(index_str) < 2:
            index_str = "0" + index_str
        
        zones_list.append(cfg.zone_folder + index_str)

    for zone in zones_list:
            zone_path = os.path.join(scene_path, zone)

            with open(os.path.join(zone_path, cfg.seuil_expe_filename), 'r') as f:
                human_thresholds.append(int(f.readline()))

    # 4. get estimated thresholds using model and specific method
    images_path = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
    number_of_images = len(images_path)
    image_indices = [ dt.get_scene_image_quality(img_path) for img_path in images_path ]

    image_counter = 0

    print(human_thresholds)

    # append empty list
    for zone in zones_list:
        estimated_thresholds.append(None)
        n_estimated_thresholds.append(0)

    for img_i, img_path in enumerate(images_path):

        blocks = segmentation.divide_in_blocks(Image.open(img_path), (200, 200))

        for index, block in enumerate(blocks):
            
            if estimated_thresholds[index] is None:
                # normalize if necessary
                if p_imnorm:
                    block = np.array(block) / 255.
                
                # check if prediction is possible
                data = np.array(get_image_features(p_method, np.array(block)))

                if p_mode == 'svdn':
                    data = utils.normalize_arr_with_range(data)

                data = data[begin:end]

                #data = np.expand_dims(data, axis=0)
                #print(data.shape)
                
                prob = model.predict(np.array(data).reshape(1, -1))[0]
                #print(index, ':', image_indices[img_i], '=>', prob)

                if prob < 0.5:
                    n_estimated_thresholds[index] += 1

                    # if same number of detection is attempted
                    if n_estimated_thresholds[index] >= p_n_stop:
                        estimated_thresholds[index] = image_indices[img_i]
                else:
                    n_estimated_thresholds[index] = 0

        # write progress bar
        write_progress((image_counter + 1) / number_of_images)
        
        image_counter = image_counter + 1
    
    # default label
    for i, _ in enumerate(zones_list):
        if estimated_thresholds[i] == None:
            estimated_thresholds[i] = image_indices[-1]

    # 6. save estimated thresholds into specific file
    print(estimated_thresholds)
    print(p_save)
    if p_save is not None:
        with open(p_save, 'a') as f:
            f.write(p_label + ';')

            for t in estimated_thresholds:
                f.write(str(t) + ';')
            f.write('\n')
Exemple #7
0
def display_data_scenes(nb_bits, p_scene):
    """
    @brief Method display shifted values for specific scene
    @param nb_bits, number of bits expected
    @param p_scene, scene we want to show values
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            print(folder_scene)
            scene_path = os.path.join(path, folder_scene)

            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            threshold_info = []

            for zone_folder in zones_folder:

                zone_path = os.path.join(scene_path, zone_folder)

                # get threshold information
                path_seuil = os.path.join(zone_path, seuil_expe_filename)

                # open treshold path and get this information
                with open(path_seuil, "r") as seuil_file:
                    seuil_learned = int(seuil_file.readline().strip())
                    threshold_info.append(seuil_learned)

            # compute mean threshold values
            mean_threshold = sum(threshold_info) / float(len(threshold_info))

            print(mean_threshold, "mean threshold found")
            threshold_image_found = False

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            start_image_path = scene_images[0]
            end_image_path = scene_images[-1]

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image = dt.get_scene_image_quality(scene_images[-1])

            # for each images
            for img_path in scene_images:
                current_quality_image = dt.get_scene_image_quality(img_path)

                if mean_threshold < int(
                        current_quality_image) and not threshold_image_found:

                    threshold_image_found = True
                    threshold_image_path = img_path

                    threshold_image = dt.get_scene_image_quality(img_path)

            # all indexes of picture to plot
            images_path = [
                start_image_path, threshold_image_path, end_image_path
            ]

            low_bits_svd_values = []

            for i in range(0, max_nb_bits - nb_bits + 1):

                low_bits_svd_values.append([])

                for img_path in images_path:

                    current_img = Image.open(img_path)

                    block_used = np.array(current_img)

                    low_bits_block = transform.rgb_to_LAB_L_bits(
                        block_used, (i + 1, i + nb_bits + 1))
                    low_bits_svd = compression.get_SVD_s(low_bits_block)
                    low_bits_svd = [b / low_bits_svd[0] for b in low_bits_svd]
                    low_bits_svd_values[i].append(low_bits_svd)

            fig = plt.figure(figsize=(8, 8))
            fig.suptitle("Lab SVD " + str(nb_bits) +
                         " bits values shifted for " + p_scene + " scene",
                         fontsize=20)

            for id, data in enumerate(low_bits_svd_values):
                fig.add_subplot(3, 3, (id + 1))
                plt.plot(data[0], label='Noisy_' + start_quality_image)
                plt.plot(data[1], label='Threshold_' + threshold_image)
                plt.plot(data[2], label='Reference_' + end_quality_image)
                plt.ylabel('Lab SVD ' + str(nb_bits) +
                           ' bits values shifted by ' + str(id),
                           fontsize=14)
                plt.xlabel('Vector features', fontsize=16)
                plt.legend(bbox_to_anchor=(0.5, 1),
                           loc=2,
                           borderaxespad=0.2,
                           fontsize=14)
                plt.ylim(0, 0.1)
            plt.show()
def display_svd_values(p_scene, p_interval, p_indices, p_feature, p_mode,
                       p_step, p_norm, p_error, p_ylim):
    """
    @brief Method which gives information about svd curves from zone of picture
    @param p_scene, scene expected to show svd values
    @param p_interval, interval [begin, end] of svd data to display
    @param p_interval, interval [begin, end] of samples or minutes from render generation engine
    @param p_feature, feature computed to show
    @param p_mode, normalization's mode
    @param p_norm, normalization or not of selected svd data
    @param p_error, error feature used to display
    @param p_ylim, ylim choice to better display of data
    @return nothing
    """

    max_value_svd = 0
    min_value_svd = sys.maxsize

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    begin_data, end_data = p_interval
    begin_index, end_index = p_indices

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            scene_path = os.path.join(path, folder_scene)

            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            images_data = []
            images_path = []

            threshold_learned_zones = []

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])
            number_scene_image = len(scene_images)

            for id, zone_folder in enumerate(zones_folder):

                # get threshold information

                zone_path = os.path.join(scene_path, zone_folder)
                path_seuil = os.path.join(zone_path, seuil_expe_filename)

                # open treshold path and get this information
                with open(path_seuil, "r") as seuil_file:
                    threshold_learned = int(seuil_file.readline().strip())
                    threshold_learned_zones.append(threshold_learned)

            threshold_mean = np.mean(np.asarray(threshold_learned_zones))
            threshold_image_found = False

            svd_data = []

            # for each images
            for id_img, img_path in enumerate(scene_images):

                current_quality_image = dt.get_scene_image_quality(img_path)

                img = Image.open(img_path)

                svd_values = get_image_features(p_feature, img)

                if p_norm:
                    svd_values = svd_values[begin_data:end_data]

                # update min max values
                min_value = svd_values.min()
                max_value = svd_values.max()

                if min_value < min_value_svd:
                    min_value_svd = min_value

                if max_value > min_value_svd:
                    max_value_svd = max_value

                # keep in memory used data
                if current_quality_image % p_step == 0:
                    if current_quality_image >= begin_index and current_quality_image <= end_index:
                        images_path.append(img_path)
                        svd_data.append(svd_values)

                    if threshold_mean < current_quality_image and not threshold_image_found:

                        threshold_image_found = True
                        threshold_image_zone = dt.get_scene_image_postfix(
                            img_path)

                print('%.2f%%' % ((id_img + 1) / number_scene_image * 100))
                sys.stdout.write("\033[F")

            previous_data = []
            error_data = [0.]

            for id, data in enumerate(svd_data):

                current_data = data

                if not p_norm:
                    current_data = current_data[begin_data:end_data]

                if p_mode == 'svdn':
                    current_data = utils.normalize_arr(current_data)

                if p_mode == 'svdne':
                    current_data = utils.normalize_arr_with_range(
                        current_data, min_value_svd, max_value_svd)

                images_data.append(current_data)

                # use of whole image data for computation of ssim or psnr
                if p_error == 'ssim' or p_error == 'psnr':
                    current_data = np.asarray(Image.open(images_path[id]))

                if len(previous_data) > 0:

                    current_error = get_error_distance(p_error, previous_data,
                                                       current_data)
                    error_data.append(current_error)

                if len(previous_data) == 0:
                    previous_data = current_data

            # display all data using matplotlib (configure plt)
            gridsize = (3, 2)

            # fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(30, 22))
            fig = plt.figure(figsize=(30, 22))
            ax1 = plt.subplot2grid(gridsize, (0, 0), colspan=2, rowspan=2)
            ax2 = plt.subplot2grid(gridsize, (2, 0), colspan=2)

            ax1.set_title(p_scene + ' scene interval information SVD[' +
                          str(begin_data) + ', ' + str(end_data) +
                          '], from scenes indices [' + str(begin_index) +
                          ', ' + str(end_index) + '], ' + p_feature +
                          ' feature, ' + p_mode + ', with step of ' +
                          str(p_step) + ', svd norm ' + str(p_norm),
                          fontsize=20)
            ax1.set_ylabel('Image samples or time (minutes) generation',
                           fontsize=14)
            ax1.set_xlabel('Vector features', fontsize=16)

            for id, data in enumerate(images_data):

                current_quality_image = dt.get_scene_image_quality(
                    images_path[id])
                current_quality_postfix = dt.get_scene_image_postfix(
                    images_path[id])

                if display_error:
                    p_label = p_scene + '_' + current_quality_postfix + " | " + p_error + ": " + str(
                        error_data[id])
                else:
                    p_label = p_scene + '_' + current_quality_postfix

                if current_quality_image == threshold_image_zone:
                    ax1.plot(data,
                             label=p_label + " (threshold mean)",
                             lw=4,
                             color='red')
                else:
                    ax1.plot(data, label=p_label)

            ax1.legend(bbox_to_anchor=(0.7, 1),
                       loc=2,
                       borderaxespad=0.2,
                       fontsize=14)

            start_ylim, end_ylim = p_ylim
            ax1.set_ylim(start_ylim, end_ylim)

            ax2.set_title(p_error + " information for whole step images")
            ax2.set_ylabel(p_error + ' error')
            ax2.set_xlabel('Number of samples per pixels or times')
            ax2.set_xticks(range(len(current_quality_image)))
            ax2.set_xticklabels(
                list(map(dt.get_scene_image_quality, current_quality_image)))
            ax2.plot(error_data)

            plot_name = p_scene + '_' + p_feature + '_' + str(
                p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
            plt.savefig(plot_name)
Exemple #9
0
def display_svd_values(p_interval, p_indices, p_metric, p_mode, p_step, p_norm, p_area, p_ylim):
    """
    @brief Method which gives information about svd curves from zone of picture
    @param p_interval, interval [begin, end] of svd data to display
    @param p_indices, indices to display
    @param p_feature, feature computed to show
    @param p_mode, normalization's mode
    @param p_norm, normalization or not of selected svd data
    @param p_area, area method name to compute area under curve
    @param p_ylim, ylim choice to better display of data
    @return nothing
    """

    image_indices = []

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    begin_data, end_data = p_interval
    begin_index, end_index = p_indices

    # Store all informations about scenes
    scenes_area_data = []
    scenes_images_indices = []
    scenes_threshold_mean = []

    # go ahead each scenes
    for folder_scene in scenes:

        max_value_svd = 0
        min_value_svd = sys.maxsize

        scene_path = os.path.join(path, folder_scene)

        # construct each zones folder name
        zones_folder = []

        # get zones list info
        for index in zones:
            index_str = str(index)
            if len(index_str) < 2:
                index_str = "0" + index_str

            current_zone = "zone"+index_str
            zones_folder.append(current_zone)

        # store data information for current scene
        images_data = []
        images_indices = []
        threshold_learned_zones = []

        # get all images of folder
        scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
        number_scene_image = len(scene_images)

        for id, zone_folder in enumerate(zones_folder):

            # get threshold information
            zone_path = os.path.join(scene_path, zone_folder)
            path_seuil = os.path.join(zone_path, seuil_expe_filename)

            # open treshold path and get this information
            with open(path_seuil, "r") as seuil_file:
                threshold_learned = int(seuil_file.readline().strip())
                threshold_learned_zones.append(threshold_learned)

        threshold_mean = np.mean(np.asarray(threshold_learned_zones))
        threshold_image_found = False
        scenes_threshold_mean.append(int(threshold_mean / p_step))

        svd_data = []

        # for each images
        for id_img, img_path in enumerate(scene_images):
            
            current_quality_image = dt.get_scene_image_quality(img_path)

            img = Image.open(img_path)

            svd_values = get_image_features(p_metric, img)

            if p_norm:
                svd_values = svd_values[begin_data:end_data]

            # update min max values
            min_value = svd_values.min()
            max_value = svd_values.max()

            if min_value < min_value_svd:
                min_value_svd = min_value

            if max_value > min_value_svd:
                max_value_svd = max_value

            # keep in memory used data
            if current_quality_image % p_step == 0:
                if current_quality_image >= begin_index and current_quality_image <= end_index:
                    images_indices.append(dt.get_scene_image_postfix(img_path))
                    svd_data.append(svd_values)

                if threshold_mean < current_quality_image and not threshold_image_found:

                    threshold_image_found = True

            print('%.2f%%' % ((id_img + 1) / number_scene_image * 100))
            sys.stdout.write("\033[F")


            # all indices of picture to plot
        print("Scene %s : %s" % (folder_scene, images_indices))

        scenes_images_indices.append(image_indices)

        area_data = []

        for id, data in enumerate(svd_data):

            current_data = data

            if not p_norm:
                current_data = current_data[begin_data:end_data]

            if p_mode == 'svdn':
                current_data = utils.normalize_arr(current_data)

            if p_mode == 'svdne':
                current_data = utils.normalize_arr_with_range(current_data, min_value_svd, max_value_svd)

            images_data.append(current_data)

            # not use this script for 'sub_blocks_stats'
            current_area = get_area_under_curve(p_area, current_data)
            area_data.append(current_area)

        scenes_area_data.append(area_data)

    # display all data using matplotlib (configure plt)
    plt.title('Scenes area interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + ']' + p_metric + ' metric, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=20)
    plt.ylabel('Image samples or time (minutes) generation', fontsize=14)
    plt.xlabel('Vector features', fontsize=16)

    plt.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2, fontsize=14)

    for id, area_data in enumerate(scenes_area_data):

        threshold_id = 0
        scene_name = scenes[id]
        image_indices = scenes_images_indices[id]

        p_label = scene_name + '_' + str(images_indices[id])

        threshold_id = scenes_threshold_mean[id]

        print(p_label)

        plt.plot(area_data, label=p_label)
        #ax2.set_xticks(range(len(images_indices)))
        #ax2.set_xticklabels(list(map(int, images_indices)))
        if threshold_id != 0:
            print("Plot threshold ", threshold_id)
            plt.plot([threshold_id, threshold_id], [np.min(area_data), np.max(area_data)], 'k-', lw=2, color='red')


    start_ylim, end_ylim = p_ylim
    plt.ylim(start_ylim, end_ylim)

    plt.show()
Exemple #10
0
def display_svd_values(p_scene, p_interval, p_indices, p_zone, p_feature,
                       p_mode, p_step, p_norm, p_ylim):
    """
    @brief Method which gives information about svd curves from zone of picture
    @param p_scene, scene expected to show svd values
    @param p_interval, interval [begin, end] of svd data to display
    @param p_interval, interval [begin, end] of samples or minutes from render generation engine
    @param p_zone, zone's identifier of picture
    @param p_feature, feature computed to show
    @param p_mode, normalization's mode
    @param p_step, step of images indices
    @param p_norm, normalization or not of selected svd data
    @param p_ylim, ylim choice to better display of data
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    begin_data, end_data = p_interval
    begin_index, end_index = p_indices

    data_min_max_filename = os.path.join(path, p_feature + min_max_filename)

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            scene_path = os.path.join(path, folder_scene)
            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            zones_images_data = []
            images_path = []

            zone_folder = zones_folder[p_zone]

            zone_path = os.path.join(scene_path, zone_folder)

            # get threshold information
            path_seuil = os.path.join(zone_path, seuil_expe_filename)

            # open treshold path and get this information
            with open(path_seuil, "r") as seuil_file:
                seuil_learned = int(seuil_file.readline().strip())

            threshold_image_found = False

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            # for each images
            for img_path in scene_images:

                current_quality_image = dt.get_scene_image_quality(img_path)

                if current_quality_image % p_step == 0:
                    if current_quality_image >= begin_index and current_quality_image <= end_index:
                        images_path.append(img_path)

                    if seuil_learned < current_quality_image and not threshold_image_found:

                        threshold_image_found = True
                        threshold_image_zone = dt.get_scene_image_postfix(
                            img_path)

                        if img_path not in images_path:
                            images_path.append(img_path)

            for img_path in images_path:

                current_img = Image.open(img_path)
                img_blocks = segmentation.divide_in_blocks(
                    current_img, (200, 200))

                # getting expected block id
                block = img_blocks[p_zone]

                # get data from mode
                # Here you can add the way you compute data
                data = get_image_features(p_feature, block)

                # TODO : improve part of this code to get correct min / max values
                if p_norm:
                    data = data[begin_data:end_data]

                ##################
                # Data mode part #
                ##################

                if p_mode == 'svdne':

                    # getting max and min information from min_max_filename
                    if not p_norm:
                        with open(data_min_max_filename, 'r') as f:
                            min_val = float(f.readline())
                            max_val = float(f.readline())
                    else:
                        min_val = min_value_interval
                        max_val = max_value_interval

                    data = utils.normalize_arr_with_range(
                        data, min_val, max_val)

                if p_mode == 'svdn':
                    data = utils.normalize_arr(data)

                if not p_norm:
                    zones_images_data.append(data[begin_data:end_data])
                else:
                    zones_images_data.append(data)

            fig, ax = plt.subplots(figsize=(30, 22))
            ax.set_facecolor('#FFFFFF')

            # plt.title(p_scene + ' scene (zone  ' + str(p_zone) + ') interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + '], ' + p_feature + ' feature, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=24)
            ax.set_ylabel('Component values', fontsize=28)
            ax.set_xlabel('Vector features', fontsize=28)

            ax.tick_params(labelsize=22)

            for id, data in enumerate(zones_images_data):

                p_label = p_scene + "_" + dt.get_scene_image_postfix(
                    images_path[id])

                if int(dt.get_scene_image_postfix(
                        images_path[id])) == int(threshold_image_zone):
                    ax.plot(data,
                            label=p_label + ' (zone ' + str(p_zone) +
                            ' threshold)',
                            lw=4,
                            color='red')
                else:
                    ax.plot(data, label=p_label)

            plt.legend(bbox_to_anchor=(0.60, 0.98),
                       loc=2,
                       borderaxespad=0.2,
                       fontsize=24)

            start_ylim, end_ylim = p_ylim
            plt.ylim(start_ylim, end_ylim)

            plot_name = p_scene + '_zone_' + str(
                p_zone) + '_' + p_feature + '_' + str(
                    p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
            plt.savefig(plot_name, facecolor=ax.get_facecolor())
def display_svd_values(p_scene, p_thresholds, p_interval, p_indices, p_feature,
                       p_mode, p_step, p_norm, p_ylim, p_label):
    """
    @brief Method which gives information about svd curves from zone of picture
    @param p_scene, scene expected to show svd values
    @param p_interval, interval [begin, end] of svd data to display
    @param p_interval, interval [begin, end] of samples or minutes from render generation engine
    @param p_feature, feature computed to show
    @param p_mode, normalization's mode
    @param p_norm, normalization or not of selected svd data
    @param p_ylim, ylim choice to better display of data
    @return nothing
    """

    max_value_svd = 0
    min_value_svd = sys.maxsize

    begin_data, end_data = p_interval
    begin_index, end_index = p_indices

    # go ahead selected scene
    scene_path = p_scene

    # construct each zones folder name
    zones_folder = []

    # get zones list info
    for index in zones:
        index_str = str(index)
        if len(index_str) < 2:
            index_str = "0" + index_str

        current_zone = "zone" + index_str
        zones_folder.append(current_zone)

    images_data = []
    images_indices = []

    threshold_learned_zones = []

    # get all images of folder
    scene_images = sorted([
        os.path.join(scene_path, img) for img in os.listdir(scene_path)
        if cfg.scene_image_extension in img
    ])
    number_scene_image = len(scene_images)

    _, scene_name = os.path.split(p_scene)
    threshold_learned_zones = p_thresholds[scene_name]

    threshold_mean = np.mean(np.asarray(threshold_learned_zones))
    threshold_image_found = False

    svd_data = []

    # for each images
    for id_img, img_path in enumerate(scene_images):

        current_quality_image = dt.get_scene_image_quality(img_path)

        img = Image.open(img_path)

        svd_values = get_image_features(p_feature, img)

        if p_norm:
            svd_values = svd_values[begin_data:end_data]

        #svd_values = np.asarray([math.log(x) for x in svd_values])

        # update min max values
        min_value = svd_values.min()
        max_value = svd_values.max()

        if min_value < min_value_svd:
            min_value_svd = min_value

        if max_value > min_value_svd:
            max_value_svd = max_value

        # keep in memory used data
        if current_quality_image % p_step == 0:
            if current_quality_image >= begin_index and current_quality_image <= end_index:

                images_indices.append(dt.get_scene_image_postfix(img_path))
                svd_data.append(svd_values)

        if threshold_mean < current_quality_image and not threshold_image_found:

            threshold_image_found = True
            threshold_image_zone = current_quality_image

            print("Quality mean : ", current_quality_image, "\n")

            if dt.get_scene_image_postfix(img_path) not in images_indices:
                images_indices.append(dt.get_scene_image_postfix(img_path))

        print('%.2f%%' % ((id_img + 1) / number_scene_image * 100))
        sys.stdout.write("\033[F")

    # all indices of picture to plot
    print(images_indices)

    for id, data in enumerate(svd_data):

        # current_data = [ math.log10(d + 1.) for d in data ]
        # print(current_data)

        current_data = data

        if not p_norm:
            current_data = current_data[begin_data:end_data]

        if p_mode == 'svdn':
            current_data = utils.normalize_arr(current_data)

        if p_mode == 'svdne':
            current_data = utils.normalize_arr_with_range(
                current_data, min_value_svd, max_value_svd)

        images_data.append(current_data)

    # display all data using matplotlib (configure plt)
    fig, ax = plt.subplots(figsize=(30, 15))
    ax.set_facecolor('#FFFFFF')
    #fig.patch.set_facecolor('#F9F9F9')

    ax.tick_params(labelsize=26)
    #plt.rc('xtick', labelsize=22)
    #plt.rc('ytick', labelsize=22)

    #plt.title(p_scene + ' scene interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + '], ' + p_feature + ' feature, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=24)
    ax.set_ylabel('Component values', fontsize=36)
    ax.set_xlabel('Singular value component indices', fontsize=36)

    for id, data in enumerate(images_data):

        #p_label = p_scene + "_" + images_indices[id]
        p_label = images_indices[id] + " samples"

        if int(images_indices[id]) == int(threshold_image_zone):
            ax.plot(data,
                    label=p_label + " (threshold mean)",
                    lw=8,
                    color='red')
        else:
            ax.plot(data, label=p_label, lw=4)

    plt.legend(bbox_to_anchor=(0.60, 0.98),
               loc=2,
               borderaxespad=0.2,
               fontsize=32)

    start_ylim, end_ylim = p_ylim
    ax.set_ylim(start_ylim, end_ylim)

    plot_name = scene_name + '_' + p_feature + '_' + str(
        p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
    # plt.title('Tend of Singular values at different samples of ' + p_label + ' scene', fontsize=40)
    plt.savefig(plot_name, transparent=True)
def main():

    parser = argparse.ArgumentParser(description="Script which predicts threshold using specific keras model")

    parser.add_argument('--features', type=str, 
                                     help="list of features choice in order to compute data",
                                     default='svd_reconstruction, ipca_reconstruction',
                                     required=True)
    parser.add_argument('--params', type=str, 
                                    help="list of specific param for each metric choice (See README.md for further information in 3D mode)", 
                                    default='100, 200 :: 50, 25',
                                    required=True)
    parser.add_argument('--model', type=str, help='.json file of keras model', required=True)
    parser.add_argument('--size', type=str, help="Expected output size before processing transformation", default="100,100")
    parser.add_argument('--renderer', type=str, 
                                      help='Renderer choice in order to limit scenes used', 
                                      choices=cfg.renderer_choices, 
                                      default='all', 
                                      required=True)

    args = parser.parse_args()

    p_features   = list(map(str.strip, args.features.split(',')))
    p_params     = list(map(str.strip, args.params.split('::')))
    p_model_file = args.model
    p_size       = args.size
    p_renderer   = args.renderer

    scenes_list = dt.get_renderer_scenes_names(p_renderer)

    scenes = os.listdir(scenes_path)

    print(scenes)

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        # only take in consideration renderer scenes
        if folder_scene in scenes_list:

            print(folder_scene)

            scene_path = os.path.join(scenes_path, folder_scene)

            # get all images of folder
            scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
            number_scene_image = len(scene_images)

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image   = dt.get_scene_image_quality(scene_images[-1])
            # using first two images find the step of quality used
            quality_step_image  = dt.get_scene_image_quality(scene_images[1]) - start_quality_image

            threshold_expes = []
            threshold_expes_found = []
            block_predictions_str = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str
                zone_folder = "zone"+index_str

                threshold_path_file = os.path.join(os.path.join(scene_path, zone_folder), threshold_expe_filename)

                with open(threshold_path_file) as f:
                    threshold = int(f.readline())
                    threshold_expes.append(threshold)

                    # Initialize default data to get detected model threshold found
                    threshold_expes_found.append(int(end_quality_image)) # by default use max

                block_predictions_str.append(index_str + ";" + p_model_file + ";" + str(threshold) + ";" + str(start_quality_image) + ";" + str(quality_step_image))

            # for each images
            for img_path in scene_images:

                current_img = Image.open(img_path)
                img_blocks = divide_in_blocks(current_img, cfg.sub_image_size)

                current_quality_image = dt.get_scene_image_quality(img_path)

                for id_block, block in enumerate(img_blocks):

                    # check only if necessary for this scene (not already detected)
                    #if not threshold_expes_detected[id_block]:

                        tmp_file_path = tmp_filename.replace('__model__',  p_model_file.split('/')[-1].replace('.json', '_'))
                        block.save(tmp_file_path)

                        python_cmd = "python predict_noisy_image.py --image " + tmp_file_path + \
                                        " --features " + p_features + \
                                        " --params " + p_params + \
                                        " --model " + p_model_file + \
                                        " --size " + p_size 

                        ## call command ##
                        p = subprocess.Popen(python_cmd, stdout=subprocess.PIPE, shell=True)

                        (output, err) = p.communicate()

                        ## Wait for result ##
                        p_status = p.wait()

                        prediction = int(output)

                        # save here in specific file of block all the predictions done
                        block_predictions_str[id_block] = block_predictions_str[id_block] + ";" + str(prediction)

                        print(str(id_block) + " : " + str(current_quality_image) + "/" + str(threshold_expes[id_block]) + " => " + str(prediction))

                print("------------------------")
                print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
                print("------------------------")

            # end of scene => display of results

            # construct path using model name for saving threshold map folder
            model_threshold_path = os.path.join(threshold_map_folder, p_model_file.split('/')[-1].replace('.joblib', ''))

            # create threshold model path if necessary
            if not os.path.exists(model_threshold_path):
                os.makedirs(model_threshold_path)

            map_filename = os.path.join(model_threshold_path, simulation_curves_zones + folder_scene)
            f_map = open(map_filename, 'w')

            for line in block_predictions_str:
                f_map.write(line + '\n')
            f_map.close()

            print("Scene " + str(id_scene + 1) + "/" + str(len(maxwell_scenes)) + " Done..")
            print("------------------------")

            print("Model predictions are saved into %s" % map_filename)
def reconstruct_image_human(scene_name, output):
    """
    @brief Method used to display simulation given .csv files
    @param scene_name, scene name used
    @param output, the output filename
    @return nothing
    """

    # compute zone start index
    zones_coordinates = []
    for zone_index in cfg.zones_indices:
        x_zone = (zone_index % nb_x_parts) * zone_width
        y_zone = (math.floor(zone_index / nb_x_parts)) * zone_height

        zones_coordinates.append((x_zone, y_zone))

    scene_folder = os.path.join(cfg.dataset_path, scene_name)

    folder_scene_elements = os.listdir(scene_folder)

    zones_folder = [zone for zone in folder_scene_elements if 'zone' in zone]
    zones_folder = sorted(zones_folder)

    scenes_images = [
        img for img in folder_scene_elements
        if cfg.scene_image_extension in img
    ]
    scenes_images = sorted(scenes_images)

    # 1. find thresholds from scene
    human_thresholds = []

    for zone_folder in zones_folder:
        zone_path = os.path.join(scene_folder, zone_folder)

        with open(os.path.join(zone_path, cfg.seuil_expe_filename)) as f:
            human_thresholds.append(int(f.readline()))

    # 2. find images for each zone which are attached to these human thresholds by the model
    zone_images_index = []

    for threshold in human_thresholds:

        current_image_index = 0

        for image_name in scenes_images:

            image_quality = dt.get_scene_image_quality(image_name)

            if image_quality > threshold:
                current_image_index = image_quality
                break

        str_index = str(current_image_index)
        while len(str_index) < 5:
            str_index = "0" + str_index

        zone_images_index.append(str_index)

    images_zones = []
    line_images_zones = []
    # get image using threshold by zone
    for id, zone_index in enumerate(zone_images_index):
        filtered_images = [img for img in scenes_images if zone_index in img]

        if len(filtered_images) > 0:
            image_name = filtered_images[0]
        else:
            image_name = scenes_images[-1]

        image_path = os.path.join(scene_folder, image_name)
        selected_image = Image.open(image_path)

        x_zone, y_zone = zones_coordinates[id]
        zone_image = np.array(selected_image)[y_zone:y_zone + zone_height,
                                              x_zone:x_zone + zone_width]
        line_images_zones.append(zone_image)

        if int(id + 1) % int(scene_width / zone_width) == 0:
            images_zones.append(np.concatenate(line_images_zones, axis=1))
            line_images_zones = []

    # 3. reconstructed the image using these zones
    reconstructed_image = np.concatenate(images_zones, axis=0)

    # 4. Save the image with generated name based on scene
    reconstructed_pil_img = Image.fromarray(reconstructed_image)

    folders = output.split('/')
    if len(folders) > 1:
        output_folder = '/'.join(folders[:len(folders) - 1])

        if not os.path.exists(output_folder):
            os.makedirs(output_folder)

    reconstructed_pil_img.save(output)
Exemple #14
0
def generate_data(transformation):
    """
    @brief Method which generates all .csv files from scenes
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        print(folder_scene)
        scene_path = os.path.join(path, folder_scene)

        config_file_path = os.path.join(scene_path, config_filename)

        with open(config_file_path, "r") as config_file:
            last_image_name = config_file.readline().strip()
            prefix_image_name = config_file.readline().strip()
            start_index_image = config_file.readline().strip()
            end_index_image = config_file.readline().strip()
            step_counter = int(config_file.readline().strip())

        # construct each zones folder name
        zones_folder = []
        features_folder = []
        zones_threshold = []

        # get zones list info
        for index in zones:
            index_str = str(index)
            if len(index_str) < 2:
                index_str = "0" + index_str

            current_zone = "zone"+index_str
            zones_folder.append(current_zone)
            zone_path = os.path.join(scene_path, current_zone)

            with open(os.path.join(zone_path, cfg.seuil_expe_filename)) as f:
                zones_threshold.append(int(f.readline()))

            # custom path for feature
            feature_path = os.path.join(zone_path, transformation.getName())

            if not os.path.exists(feature_path):
                os.makedirs(feature_path)

            # custom path for interval of reconstruction and feature
            feature_interval_path = os.path.join(zone_path, transformation.getTransformationPath())
            features_folder.append(feature_interval_path)

            if not os.path.exists(feature_interval_path):
                os.makedirs(feature_interval_path)

            # create for each zone the labels folder
            labels = [cfg.not_noisy_folder, cfg.noisy_folder]

            for label in labels:
                label_folder = os.path.join(feature_interval_path, label)

                if not os.path.exists(label_folder):
                    os.makedirs(label_folder)


        # get all images of folder
        scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
        number_scene_image = len(scene_images)

        # for each images
        for id_img, img_path in enumerate(scene_images):

            current_img = Image.open(img_path)
            img_blocks = divide_in_blocks(current_img, cfg.keras_img_size)

            current_quality_index = int(get_scene_image_quality(img_path))

            for id_block, block in enumerate(img_blocks):

                ##########################
                # Image computation part #
                ##########################
                
                # pass block to grey level


                output_block = transformation.getTransformedImage(block)
                output_block = np.array(output_block, 'uint8')
                
                # current output image
                output_block_img = Image.fromarray(output_block)

                label_path = features_folder[id_block]

                # get label folder for block
                if current_quality_index > zones_threshold[id_block]:
                    label_path = os.path.join(label_path, cfg.not_noisy_folder)
                else:
                    label_path = os.path.join(label_path, cfg.noisy_folder)

                # Data augmentation!
                rotations = [0, 90, 180, 270]
                img_flip_labels = ['original', 'horizontal', 'vertical', 'both']

                horizontal_img = output_block_img.transpose(Image.FLIP_LEFT_RIGHT)
                vertical_img = output_block_img.transpose(Image.FLIP_TOP_BOTTOM)
                both_img = output_block_img.transpose(Image.TRANSPOSE)

                flip_images = [output_block_img, horizontal_img, vertical_img, both_img]

                # rotate and flip image to increase dataset size
                for id, flip in enumerate(flip_images):
                    for rotation in rotations:
                        rotated_output_img = flip.rotate(rotation)

                        output_reconstructed_filename = img_path.split('/')[-1].replace('.png', '') + '_' + zones_folder[id_block] + cfg.post_image_name_separator
                        output_reconstructed_filename = output_reconstructed_filename + img_flip_labels[id] + '_' + str(rotation) + '.png'
                        output_reconstructed_path = os.path.join(label_path, output_reconstructed_filename)

                        rotated_output_img.save(output_reconstructed_path)

            print(transformation.getName() + "_" + folder_scene + " - " + "{0:.2f}".format(((id_img + 1) / number_scene_image)* 100.) + "%")
            sys.stdout.write("\033[F")

        print('\n')

    print("%s_%s : end of data generation\n" % (transformation.getName(), transformation.getParam()))
Exemple #15
0
def main():

    p_custom = False

    parser = argparse.ArgumentParser(
        description="Script which predicts threshold using specific model")

    parser.add_argument('--interval',
                        type=str,
                        help='Interval value to keep from svd',
                        default='"0, 200"')
    parser.add_argument('--model',
                        type=str,
                        help='.joblib or .json file (sklearn or keras model)')
    parser.add_argument('--mode',
                        type=str,
                        help='Kind of normalization level wished',
                        choices=normalization_choices)
    parser.add_argument('--feature',
                        type=str,
                        help='feature data choice',
                        choices=features_choices)
    #parser.add_argument('--limit_detection', type=int, help='Specify number of same prediction to stop threshold prediction', default=2)
    parser.add_argument(
        '--custom',
        type=str,
        help='Name of custom min max file if use of renormalization of data',
        default=False)

    args = parser.parse_args()

    # keep p_interval as it is
    p_interval = args.interval
    p_model_file = args.model
    p_mode = args.mode
    p_feature = args.feature
    #p_limit      = args.limit
    p_custom = args.custom

    scenes = os.listdir(scenes_path)
    scenes = [s for s in scenes if s in maxwell_scenes]

    print(scenes)

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        # only take in consideration maxwell scenes
        if folder_scene in maxwell_scenes:

            print(folder_scene)

            scene_path = os.path.join(scenes_path, folder_scene)

            threshold_expes = []
            threshold_expes_found = []
            block_predictions_str = []

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image = dt.get_scene_image_quality(scene_images[-1])
            # using first two images find the step of quality used
            quality_step_image = dt.get_scene_image_quality(
                scene_images[1]) - start_quality_image

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str
                zone_folder = "zone" + index_str

                threshold_path_file = os.path.join(
                    os.path.join(scene_path, zone_folder),
                    threshold_expe_filename)

                with open(threshold_path_file) as f:
                    threshold = int(f.readline())
                    threshold_expes.append(threshold)

                    # Initialize default data to get detected model threshold found
                    threshold_expes_found.append(
                        end_quality_image)  # by default use max

                block_predictions_str.append(index_str + ";" + p_model_file +
                                             ";" + str(threshold) + ";" +
                                             str(start_quality_image) + ";" +
                                             str(quality_step_image))

            # for each images
            for img_path in scene_images:

                current_img = Image.open(img_path)
                current_quality_image = dt.get_scene_image_quality(img_path)

                img_blocks = segmentation.divide_in_blocks(
                    current_img, (200, 200))

                for id_block, block in enumerate(img_blocks):

                    # check only if necessary for this scene (not already detected)
                    #if not threshold_expes_detected[id_block]:

                    tmp_file_path = tmp_filename.replace(
                        '__model__',
                        p_model_file.split('/')[-1].replace('.joblib', '_'))
                    block.save(tmp_file_path)

                    python_cmd_line = "python prediction/predict_noisy_image_svd.py --image {0} --interval '{1}' --model {2} --mode {3} --feature {4}"
                    python_cmd = python_cmd_line.format(
                        tmp_file_path, p_interval, p_model_file, p_mode,
                        p_feature)

                    # specify use of custom file for min max normalization
                    if p_custom:
                        python_cmd = python_cmd + ' --custom ' + p_custom

                    ## call command ##
                    p = subprocess.Popen(python_cmd,
                                         stdout=subprocess.PIPE,
                                         shell=True)

                    (output, err) = p.communicate()

                    ## Wait for result ##
                    p_status = p.wait()

                    prediction = int(output)

                    # save here in specific file of block all the predictions done
                    block_predictions_str[id_block] = block_predictions_str[
                        id_block] + ";" + str(prediction)

                    print(
                        str(id_block) + " : " + str(current_quality_image) +
                        "/" + str(threshold_expes[id_block]) + " => " +
                        str(prediction))

                print("------------------------")
                print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
                print("------------------------")

            # end of scene => display of results

            # construct path using model name for saving threshold map folder
            model_threshold_path = os.path.join(
                threshold_map_folder,
                p_model_file.split('/')[-1].replace('.joblib', ''))

            # create threshold model path if necessary
            if not os.path.exists(model_threshold_path):
                os.makedirs(model_threshold_path)

            map_filename = os.path.join(model_threshold_path,
                                        simulation_curves_zones + folder_scene)
            f_map = open(map_filename, 'w')

            for line in block_predictions_str:
                f_map.write(line + '\n')
            f_map.close()

            print("Scene " + str(id_scene + 1) + "/" +
                  str(len(maxwell_scenes)) + " Done..")
            print("------------------------")

            print("Model predictions are saved into %s" % map_filename)