Exemplo n.º 1
0
def distort_like_page(dst_dir,
                      start_index,
                      x_min,
                      x_max,
                      y_min,
                      y_max,
                      w_res,
                      h_res,
                      write_npy=True):
    move_maps = []
    start_time = time.time()
    Check_dir_exist_and_build(data_access_path + dst_dir + "/" +
                              "distort_mesh_visuals")
    Check_dir_exist_and_build(data_access_path + dst_dir + "/" + "move_maps")
    Check_dir_exist_and_build(data_access_path + dst_dir + "/" +
                              "distort_infos")

    ### 可以用step2_d去試 我想要的參數喔!
    distort_time = 1
    vert_y = 0
    move_x = 0
    dis_type = "curl"
    alpha = 1.5
    index = start_index

    for go_move_y in range(-13, 13):  ### 可以用step2_d去試 我想要的參數喔!
        for go_vert_x in range(120, 180):  ### 可以用step2_d去試 我想要的參數喔!
            dis_start_time = time.time()
            result_move_f = np.zeros(shape=(h_res * w_res, 2),
                                     dtype=np.float64)  ### 初始化 move容器
            move_f, _ = get_dis_move_map(x_min,
                                         x_max,
                                         y_min,
                                         y_max,
                                         w_res,
                                         h_res,
                                         go_vert_x,
                                         vert_y,
                                         move_x,
                                         go_move_y,
                                         dis_type,
                                         alpha,
                                         debug=False)  ### 用參數去得到 扭曲move_f
            result_move_f = result_move_f + move_f  ### 把我要的move_f加進去容器內~
            distrore_info_log(data_access_path + dst_dir + "/" +
                              "distort_infos", index, h_res, w_res,
                              distort_time, go_vert_x, vert_y, move_x,
                              go_move_y, dis_type, alpha)  ### 紀錄扭曲參數
            result_move_map = result_move_f.reshape(
                h_res, w_res, 2)  ### (..., 2)→(h_res, w_res, 2)
            result_move_map = result_move_map.astype(np.float32)
            # save_distort_mesh_visual(h_res, w_res, result_move_f, index)   ### 紀錄扭曲視覺化的結果
            np.save(data_access_path + dst_dir + "/" +
                    "move_maps/%06i" % index,
                    result_move_map)  ### 把move_map存起來,記得要轉成float32!
            print("%06i process 1 mesh cost time:" % index,
                  "%.3f" % (time.time() - dis_start_time), "total_time:",
                  time_util(time.time() - start_time))
            index += 1
            move_maps.append(result_move_map)
    return np.array(move_maps.astype(np.float32))
Exemplo n.º 2
0
def I_Generate_M_see(model_G, phase, index, in_img, in_img_pre, gt_mask_coord, _4, rec_hope=None, exp_obj=None, training=True, see_reset_init=True, postprocess=False, add_loss=False, bgr2rgb=False):
    current_ep = exp_obj.current_ep
    current_time = exp_obj.current_time
    if  (phase == "see"):  used_sees = exp_obj.result_obj.sees
    elif(phase == "test"): used_sees = exp_obj.result_obj.tests
    '''
    bgr2rgb: tf2 讀出來是 rgb, 但 cv2 存圖是bgr, 所以此狀況記得要轉一下ch 把 bgr2rgb設True!
    '''
    in_img, pred_mask, pred_mask_visual, gt_mask = I_Generate_M_basic_data(model_G, in_img, in_img_pre, gt_mask_coord, exp_obj, training, bgr2rgb=True)
    private_write_dir      = used_sees[index].see_write_dir   ### 每個 see 都有自己的資料夾 存 in/gt 之類的 輔助檔案 ,先定出位置
    private_mask_write_dir = used_sees[index].mask_write_dir  ### 每個 see 都有自己的資料夾 存 model生成的結果,先定出位置
    public_write_dir       = "/".join(used_sees[index].see_write_dir.replace("\\", "/").split("/")[:-1])  ### private 的上一層資料夾
    # print('public_write_dir:', public_write_dir)

    if(current_ep == 0 or see_reset_init):                                              ### 第一次執行的時候,建立資料夾 和 寫一些 進去資料夾比較好看的東西
        Check_dir_exist_and_build(private_write_dir)                                   ### 建立 放輔助檔案 的資料夾
        Check_dir_exist_and_build(private_mask_write_dir)                                  ### 建立 model生成的結果 的資料夾
        cv2.imwrite(private_write_dir  + "/" + "0a_u1a0-dis_img(in_img).jpg", in_img)                ### 寫一張 in圖進去,進去資料夾時比較好看,0a是為了保證自動排序會放在第一張
        cv2.imwrite(private_write_dir  + "/" + "0b_u1b1-gt_mask.bmp", gt_mask)            ### 寫一張 gt圖進去,進去資料夾時比較好看,0b是為了保證自動排序會放在第二張
    cv2.imwrite(    private_mask_write_dir + "/" + "epoch_%04i_u1b1_mask.bmp" % current_ep, pred_mask_visual)  ### 我覺得不可以直接存npy,因為太大了!但最後為了省麻煩還是存了,相對就減少see的數量來讓總大小變小囉~

    if(postprocess):
        current_see_name = used_sees[index].see_name.replace("/", "-")  ### 因為 test 會有多一層 "test_db_name"/test_001, 所以把 / 改成 - ,下面 Save_fig 才不會多一層資料夾
        from matplot_fig_ax_util import Matplot_single_row_imgs
        imgs = [ in_img ,   pred_mask_visual , gt_mask]
        img_titles = ["in_img", "pred_mask", "gt_mask"]

        single_row_imgs = Matplot_single_row_imgs(
                                imgs      =imgs,         ### 把要顯示的每張圖包成list
                                img_titles=img_titles,               ### 把每張圖要顯示的字包成list
                                fig_title ="%s, epoch=%04i" % (current_see_name, int(current_ep)),  ### 圖上的大標題
                                add_loss  =add_loss,
                                bgr2rgb   =bgr2rgb)
        single_row_imgs.Draw_img()
        single_row_imgs.Save_fig(dst_dir=public_write_dir, name=current_see_name)  ### 如果沒有要接續畫loss,就可以存了喔!

        '''
        Fake_F 的部分
        '''
        gather_mask_dir   = public_write_dir + "/pred_mask"
        Check_dir_exist_and_build(gather_mask_dir)
        cv2.imwrite(f"{gather_mask_dir}/{current_see_name}.bmp", pred_mask_visual)

        h, w = pred_mask.shape[:2]
        fake_name = current_see_name.split(".")[0]
        print("")
        ###############################################################################
        gather_fake_F_dir = public_write_dir + "/pred_mask/fake_F"
        Check_dir_exist_and_build(gather_fake_F_dir)
        fake_C = np.zeros(shape=(h, w, 2), dtype=np.float32)
        fake_F = np.concatenate((pred_mask, fake_C), axis=-1)
        fake_F = fake_F.astype(np.float32)

        gather_fake_F_npy_dir  = gather_fake_F_dir + "/1 npy"
        gather_fake_F_knpy_dir = gather_fake_F_dir + "/2 knpy"
        Check_dir_exist_and_build(gather_fake_F_npy_dir)
        Check_dir_exist_and_build(gather_fake_F_knpy_dir)
        fake_F_npy_path  = f"{gather_fake_F_npy_dir}/{fake_name}.npy"
        fake_F_knpy_path = f"{gather_fake_F_knpy_dir}/{fake_name}.knpy"
        np.save(fake_F_npy_path, fake_F)
        Save_npy_path_as_knpy(fake_F_npy_path, fake_F_knpy_path)
        print("fake_F_npy_path :", fake_F_npy_path)
        print("fake_F_knpy_path:", fake_F_knpy_path)
        ###############################################################################
        gather_fake_W_dir = public_write_dir + "/pred_mask/fake_W"
        Check_dir_exist_and_build(gather_fake_W_dir)
        fake_W = np.zeros(shape=(h, w, 3), dtype=np.float32)
        fake_W = np.concatenate((fake_W, pred_mask), axis=-1)
        fake_W = fake_W.astype(np.float32)

        gather_fake_W_npy_dir  = gather_fake_W_dir + "/1 npy"
        gather_fake_W_knpy_dir = gather_fake_W_dir + "/2 knpy"
        Check_dir_exist_and_build(gather_fake_W_npy_dir)
        Check_dir_exist_and_build(gather_fake_W_knpy_dir)
        fake_W_npy_path  = f"{gather_fake_W_npy_dir}/{fake_name}.npy"
        fake_W_knpy_path = f"{gather_fake_W_knpy_dir}/{fake_name}.knpy"
        np.save(fake_W_npy_path, fake_W)
        Save_npy_path_as_knpy(fake_W_npy_path, fake_W_knpy_path)
        print("fake_W_npy_path :", fake_W_npy_path)
        print("fake_W_knpy_path:", fake_W_knpy_path)