def print_funked_scenes(data):
    subsampling = data[data["samples"] != data["samples_reference"]]
    # scene_names = [ should_accept.get_scene_name( row("image").decode('UTF-8') ) for row in data ]
    for scene in should_accept.ok_thresholds:

        filtered_ok = subsampling[(
            subsampling["samples"] == should_accept.ok_thresholds[scene]) |
                                  (subsampling["samples_reference"] ==
                                   should_accept.ok_thresholds[scene])]
        filtered_ok_not_ok = filtered_ok[
            (filtered_ok["samples"] == should_accept.not_ok_thresholds[scene])
            | (filtered_ok["samples_reference"] ==
               should_accept.not_ok_thresholds[scene])]

        if scene == "atom":
            for row in filtered_ok_not_ok:
                if should_accept.get_scene_name(
                        row["image"].decode('UTF-8')) == scene:
                    # print ( str( row["crop_x"] ) + ", " + str( row["crop_y"] ) )
                    print(row)

        rows = sum(1 for row in filtered_ok_not_ok
                   if should_accept.get_scene_name(row["image"].decode(
                       'UTF-8')) == scene)
        print("Scene: " + scene + ", rows: " + str(rows))
def split_by_scenes(data, scenes):

    if "scene" in data.dtype.names:

        test_mask = data["scene"] == "not_existing_scene_to_create_empty_mask"
        for scene in scenes:
            test_mask = test_mask | (data["scene"] == scene)

        train_mask = ~test_mask

        test_set = data[test_mask]
        train_set = data[train_mask]

        return train_set, test_set
    else:
        test_set = [
            row for row in data
            if accept.get_scene_name(row["image"].decode('UTF-8')) in scenes
        ]
        train_set = [
            row for row in data if accept.get_scene_name(row["image"].decode(
                'UTF-8')) not in scenes
        ]

        return train_set, test_set
def main_loop(config):
    global screen
    global esc_pressed
    global scene_names

    data = loading.load_dataset(config.dataset)
    full_images = select_rows(data, config)

    scene_names = [
        should_accept.get_scene_name(row["image"].decode('UTF-8'))
        for row in data
    ]

    load_next_row(data, full_images)

    screen = numpy.zeros(image.shape, numpy.uint8)

    cv2.namedWindow('Crops labeling')
    cv2.setMouseCallback('Crops labeling', mouse_select)

    while (1):

        screen = apply_mask(image, selected_crops)
        draw_grid_lines(screen)

        cv2.imshow('Crops labeling', screen)

        key = cv2.waitKey(20)
        if key == ord('m'):
            show_hide_mask()
        elif key == ord('d'):
            update_crops_selection(data, config)
            load_next_row(data, full_images)
        elif key == ord('a'):
            update_crops_selection(data, config)
            load_previous_row(data, full_images)
        elif key == ord("h"):
            print_help()
        elif key == ord("l"):
            show_grid_lines()
        elif key == ord("\r"):
            update_crops_selection(data, config)
            data_filtering.save_binary(data, config.dataset)
        elif key == ord("y"):
            if esc_pressed:
                update_crops_selection(data, config)
                data_filtering.save_binary(data, config.dataset)
                break
            else:
                pass
        elif key == ord("n"):
            if esc_pressed:
                break
            else:
                pass
        elif key == 27:
            print("Do you want to save your dataset labeling? (y/n)")
            esc_pressed = True

    cv2.destroyAllWindows()
def update_crops_selection(data, config):
    if config.filter_threshold:
        indicies = compute_row_and_all_crops_idx(data)
    else:
        indicies = compute_row_and_same_samples_crops_idx(data)

    scene = should_accept.get_scene_name(current_row["image"].decode('UTF-8'))

    for idx in indicies:

        row = data[idx]
        if row["is_cropped"]:

            tile_x = row["crop_x"]
            tile_y = row["crop_y"]

            label = selected_crops[tile_x][tile_y]
            if label == FalseLabel:
                data[idx]["label"] = get_label_string(
                    should_accept.tell_from_samples(
                        row["image"].decode('UTF-8'),
                        row["reference_image"].decode('UTF-8')).value)
            elif label == TrueLabel:
                threshold = should_accept.not_ok_thresholds[scene]
                if row["samples"] >= threshold and row[
                        "samples_reference"] >= threshold:
                    data[idx]["label"] = b"TRUE"
                else:
                    data[idx]["label"] = b"IGNORE"
            else:
                pass
def compute_row_and_all_crops_idx(data):

    filtered_mask = data["samples"] != data["samples_reference"]

    scene = should_accept.get_scene_name(current_row["image"].decode('UTF-8'))

    scene_mask = [scene_name == scene for scene_name in scene_names]

    return numpy.where(filtered_mask & scene_mask)[0]
def data_sceneing(data):

    print("Sceneing data")
    scene_names = [
        should_accept.get_scene_name(row["image"].decode('UTF-8'))
        for row in data
    ]
    data["scene"] = scene_names

    return data
def save_all_crops(data, compared_dir, reference_dir):
    data = data[data["is_cropped"] == True]

    num_crops = 10

    for i, scene in enumerate(should_accept.ok_thresholds):
        print("Scene number: " + str(i) + ", name: " + scene)
        filtered = data[data["samples"] ==
                        should_accept.not_ok_thresholds[scene]]
        filtered = filtered[filtered["samples_reference"] ==
                            should_accept.ok_thresholds[scene]]
        for row in filtered:
            if should_accept.get_scene_name(
                    row["image"].decode('UTF-8')) == scene:
                reference_file = os.path.normpath(
                    row["reference_image"].decode('UTF-8'))
                compared_file = os.path.normpath(row["image"].decode('UTF-8'))

                reference_file = reference_file.replace(
                    "D:\\GolemData", "e:\\golem")
                compared_file = compared_file.replace("D:\\GolemData",
                                                      "e:\\golem")

                print("Processing files: reference [" + reference_file +
                      "] and compared [" + compared_file + "].")

                (reference_image,
                 compared_image) = load_images(reference_file, compared_file)

                (cropped_image,
                 cropped_reference) = extract_features.crop_image(
                     row["crop_x"], row["crop_y"], num_crops, compared_image,
                     reference_image)

                cropped_image.save(
                    os.path.join(compared_dir,
                                 gen_file_name(compared_file, row, False)),
                    "PNG")
                cropped_reference.save(
                    os.path.join(reference_dir,
                                 gen_file_name(reference_file, row, False)),
                    "PNG")
def analyze_wavelets(data):
    mask = [
        should_accept.get_scene_name(
            row["image"].decode('UTF-8')) == "glass_material" for row in data
    ]
    filtered = data[mask]
    filtered = filtered[filtered["is_cropped"] == True]
    filtered = filtered[filtered["crop_x"] >= 1]
    filtered = filtered[filtered["crop_x"] <= 1]
    filtered = filtered[filtered["crop_y"] >= 4]
    filtered = filtered[filtered["crop_y"] <= 4]

    filtered = filtered[filtered["samples"] != filtered["samples_reference"]]
    filtered = filtered[filtered["samples_reference"] == 8325]

    plot_data("samples", "wavelet_high", filtered)
    plot_data("samples", "wavelet_low", filtered)
    plot_data("samples", "wavelet_mid", filtered)

    matplotlib.pyplot.show()
def select_scenes_with_threshold(data):
    print("Selecting scenes on subsampling threshold.")

    filtered = data[data["samples"] != data["samples_reference"]]

    # Create mask with all values False
    mask = filtered["samples"] == 0

    scene_names_filtered = [
        should_accept.get_scene_name(row["image"].decode('UTF-8'))
        for row in filtered
    ]

    for i, scene in enumerate(should_accept.ok_thresholds):

        print("Finding images on threshold for scene: " + scene)

        compared_ok_mask = (filtered["samples"]
                            == should_accept.ok_thresholds[scene]) & (
                                filtered["samples_reference"]
                                == should_accept.not_ok_thresholds[scene])
        reference_ok_mask = (filtered["samples_reference"]
                             == should_accept.ok_thresholds[scene]) & (
                                 filtered["samples"]
                                 == should_accept.not_ok_thresholds[scene])

        scene_mask = [
            scene_name == scene for scene_name in scene_names_filtered
        ]

        threshold_mask = (compared_ok_mask | reference_ok_mask)
        scene_threshold_mask = (threshold_mask & scene_mask)

        mask = mask | scene_threshold_mask

    return filtered[mask]