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)
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_log(logbook, log_dir_path: str): field_name_list = ['gen', 'avg', 'min', 'max'] log_dict = {key: value for key, value in zip( field_name_list, logbook.select(*field_name_list))} log_file_path = get_save_file_path(log_dir_path, 'fitness.json') with open(log_file_path, 'w') as fp: json.dump(log_dict, fp, indent=4)
def make_graph(log_file_path: str, log_dir_path: str): with open(log_file_path, 'r') as fp: log_dict = json.load(fp) fig, ax1 = plt.subplots() ax1.set_xlabel("Generation") ax1.set_ylabel("Fitness") ax1.plot(log_dict['gen'], log_dict['avg'], 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))
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}' for key in ['score', 'evaluate'] } font = ImageFont.truetype(r"C:\Windows\Fonts\Arial.ttf", size=30) draw.text((0, image.height + 30), f'{text_dict["score"]}\n{text_dict["evaluate"]}', fill=(0, 0, 0), font=font) save_file_path = get_save_file_path(rightfulness_dir_path, f'{index:0=3}.png') new_image.save(str(save_file_path))