(data_root: str,
marker_path: str,
scene_path:str,
src_path:str,
niid_net,
estimator,
poster_brightness=1/2.5,
save_decomposed=False
)
| 73 | edit single image |
| 74 | ''' |
| 75 | def image_editing(data_root: str, |
| 76 | marker_path: str, |
| 77 | scene_path:str, |
| 78 | src_path:str, |
| 79 | niid_net, |
| 80 | estimator, |
| 81 | poster_brightness=1/2.5, |
| 82 | save_decomposed=False |
| 83 | ): |
| 84 | # =========== Decompose Scene Image =========== |
| 85 | print('===> Decompose Scene Image') |
| 86 | resized_scene_name = os.path.split(scene_path)[-1] |
| 87 | if save_decomposed: |
| 88 | decompose_dir = os.path.join(data_root, 'decompose') |
| 89 | os.mkdir(decompose_dir, exist_ok=True) |
| 90 | print(f"save decompose scene images to {decompose_dir}") |
| 91 | else: |
| 92 | decompose_dir = None |
| 93 | |
| 94 | decompose_result = decompose_image( data_root = data_root, |
| 95 | img_name = resized_scene_name, |
| 96 | model = niid_net, |
| 97 | save = save_decomposed, |
| 98 | decompose_dir = decompose_dir, |
| 99 | **{ 'pretrained_file': './third_party/NIID/pretrained_model/final.pth.tar', |
| 100 | 'offline': True, |
| 101 | 'gpu_devices': [0], |
| 102 | } |
| 103 | ) |
| 104 | |
| 105 | # =========== Load Image =========== |
| 106 | print('===> Load Image') |
| 107 | starget = cv2.imread(marker_path, cv2.IMREAD_UNCHANGED) #bgr |
| 108 | src = cv2.imread(src_path, cv2.IMREAD_UNCHANGED) #bgr |
| 109 | if not src.shape[2] == 3: |
| 110 | raise ValueError("replace image should have 3 channels") |
| 111 | |
| 112 | dst = np.float64(decompose_result['rgb']) |
| 113 | dst = dst[:,:,::-1] #rgb to bgr |
| 114 | sdst = (dst*255.0).astype(np.uint8) |
| 115 | |
| 116 | # =========== Estimate & Warp ============= |
| 117 | print('===> Estimate Flow & Warp Image') |
| 118 | ori_H, ori_W = dst.shape[:2] |
| 119 | flow = estimator.estimate(sdst, starget) |
| 120 | src = cv2.GaussianBlur(src,(3,3),1,borderType=cv2.BORDER_CONSTANT) |
| 121 | out = image_flow_warp(src, flow[0].permute([1,2,0]), padding_mode='border') |
| 122 | mask_origin = (np.ones(shape=(src.shape[0], src.shape[1], 1)) * 255).astype(np.uint8) |
| 123 | mask_origin = image_flow_warp(mask_origin, flow[0].permute([1,2,0]),padding_mode='zeros') |
| 124 | mask = (255 - mask_origin).astype(np.float64) / 255.0 |
| 125 | mask = cv2.GaussianBlur(mask,(3,3),1, borderType=cv2.BORDER_REPLICATE) |
| 126 | mask = mask[:,:,np.newaxis] |
| 127 | |
| 128 | result = (out*(1-mask) + sdst*mask).astype(np.uint8) |
| 129 | replace_result = cv2.resize(result, (ori_W, ori_H)) |
| 130 | mask = cv2.resize(mask, (ori_W, ori_H)) |
| 131 | |
| 132 | # =========== Render Light ============ |
nothing calls this directly
no test coverage detected