def score(root: Tk, user_name: str, image_name: str, image_number: str):
    image_dir_path = root_image_dir_path / image_name / image_number / 'random_enhance_10'

    if not image_dir_path.exists():
        raise FileNotFoundError
    elif not image_dir_path.is_dir():
        raise NotADirectoryError

    game = TournamentGame(list(map(str, image_dir_path.iterdir())),
                          ImageGenerator())

    scored_image_dir = get_save_dir_path(root_scored_image_dir_path, user_name,
                                         f'{image_name}/{image_number}')
    scored_param_dir = get_save_dir_path(root_scored_param_dir_path, user_name,
                                         f'{image_name}/{image_number}')
    scored_param_path = get_save_file_path(scored_param_dir,
                                           'scored_param.json')

    data_writer_list = [
        DW_scored_image.ScoredImageWriter(str(scored_image_dir)),
        DW_scored_param.ScoredParamWriter(str(scored_param_path))
    ]

    sub_win = Toplevel(root)
    canvas = CompareCanvasGroupFrame(sub_win,
                                     game,
                                     data_writer_list=data_writer_list)
    canvas.pack()

    canvas.disp_enhanced_image()
    canvas.focus_set()

    sub_win.grab_set()
    sub_win.wait_window()
def make_train_data(root: tk.Tk, user_name: str,
                    image_name: str) -> (bool, str):
    image_dir_path = config.path.root_image_dir_path / \
        image_name/'1'

    if not image_dir_path.exists():
        raise MakeTrainDataException(f'{str(image_dir_path)} is not found')
    elif not image_dir_path.is_dir():
        raise MakeTrainDataException(f'{str(image_dir_path)} is not directory')

    image_path = list(
        itertools.chain(image_dir_path.glob('*.jpg'),
                        image_dir_path.glob('*.png')))[0]

    try:
        scored_param_dir_path = get_save_dir_path(
            config.path.root_scored_param_dir_path, user_name, image_name)
        scored_param_path = get_save_file_path(
            scored_param_dir_path,
            'scored_param' + DataWriter.ScoredParamWriter.SUFFIX)
    except MiscException as e:
        raise MakeTrainDataException(e)

    sub_win = tk.Toplevel(root)
    try:
        compare_num = 100
        is_complete = compare(sub_win, image_path, scored_param_path,
                              compare_num)
        return is_complete, scored_param_path

    except TrainDataMakerException as e:
        sub_win.destroy()
        raise MakeTrainDataException(e)
Example #3
0
 def get_summary_dir_path_func():
     return get_save_dir_path(
         root_summary_dir_path, user_name, image_name)
Example #4
0
    scored_param_list = None
    with open(args.scored_param_path, 'r') as fp:
        scored_param_list = json.load(fp)

    image_list = [{
        'image': Image.open(scored_param['param'])
    } for scored_param in scored_param_list]

    evaluate_list = predict_model.predict(image_list)

    for i in range(len(scored_param_list)):
        scored_param_list[i]['evaluate'] = evaluate_list[i][0]

    rightfulness_dir_path = get_save_dir_path(
        root_rightfulness_dir_path, args.user_name,
        f'{args.image_name}/{args.image_number}')

    scored_param_list.sort(key=lambda x: x['score'], reverse=True)

    for index, scored_param in enumerate(tqdm(scored_param_list)):
        image = Image.open(scored_param['param'])

        new_image = Image.new(image.mode, (image.width, image.height + 100),
                              (255, 255, 255))
        new_image.paste(image, (0, 0))

        draw = ImageDraw.Draw(new_image)

        text_dict = {
            key: f'{key:<10s}:{scored_param[key]:>5,.2f}'
 def save_dir_path_func():
     image_number = self.image_file_path.parent.name
     return get_save_dir_path(root_optimize_dir_path, self.user_name,
                              f'{self.image_name}/{image_number}')
             color='r', label="Average Fitness")
    ax1.plot(log_dict['gen'], log_dict['min'],
             color='g', label="Minimum Fitness")
    ax1.plot(log_dict['gen'], log_dict['max'],
             color='b', label="Maximum Fitness")

    ax1.legend()

    save_file_path = get_save_file_path(log_dir_path, 'graph.png')
    plt.savefig(str(save_file_path))


if __name__ == "__main__":
    args = _get_args()

    optimize_dir_path = get_save_dir_path(
        root_optimize_dir_path, args.user_name, f'{args.image_name}/{args.image_number}')

    image_dir = root_image_dir_path/args.image_name/args.image_number
    image_path = list(image_dir.glob('*.*'))[0]

    enhancer = ResizableEnhancer(str(image_path), IMAGE_SIZE)

    model = RankNet(IMAGE_SHAPE)
    model.load(args.weights_file_path)
    image_generator = EnhanceGenerator(enhancer)
    best_param_list, _ = Optimizer(model, image_generator, EnhanceDecorder()).optimize(20)

    for index, best_param in enumerate(best_param_list):
        save_path = str(Path(optimize_dir_path)/f'best_{index}.png')
        enhancer.enhance(best_param).save(save_path)