MCPcopy Create free account
hub / github.com/drinkingcoder/NeuralMarker / blend_life

Function blend_life

evaluation_DVL.py:112–177  ·  view source on GitHub ↗
(scene_images, marker, estimator, source=None, blend_type='D', warp='grid_sample', use_colormap=True)

Source from the content-addressed store, hash-verified

110 return blends, masks, blends_colormap
111
112def blend_life(scene_images, marker, estimator, source=None, blend_type='D', warp='grid_sample', use_colormap=True):
113 print('blend with our model')
114 blends = []
115 masks = []
116 blends_colormap = []
117 for id, scene in tqdm(enumerate(scene_images), total=len(scene_images)):
118 marker = cv2.resize(marker, (scene.shape[1], scene.shape[0]))
119 if blend_type == "mix":
120 source_id = 3 * int(id / 3)
121 source = scene_images[source_id]
122
123 flow = estimator.estimate(scene, marker)
124
125 if use_colormap:
126 colormap = get_colormap(flow, scene.shape[0], scene.shape[1])
127
128 mask = None
129 if warp == 'grid_sample':
130 out = image_flow_warp(marker, flow[0].permute([1,2,0]))
131 mask_origin = np.ones(shape=(marker.shape[0], marker.shape[1], 1)).astype(np.float64)
132 mask_origin = image_flow_warp(mask_origin, flow[0].permute([1,2,0]),padding_mode='zeros')
133 mask = (1 - mask_origin)
134 if use_colormap:
135 out_colormap = image_flow_warp(colormap, flow[0].permute([1,2,0]))
136 out_colormap = ((out_colormap + 256) / 2)
137 mask_colormap = np.ones_like(colormap).astype(np.float32) / 2
138 mask_colormap = image_flow_warp(mask_colormap, flow[0].permute([1,2,0]))
139 mask_colormap = (1 - mask_colormap)
140
141 elif warp == 'homography':
142 # RANSAC homography
143 flow = flow[0].permute([1,2,0])
144 image = marker
145 image = torch.from_numpy(image)
146 if image.ndim == 2:
147 image = image[None].permute([1,2,0])
148 H, W, _ = image.shape
149 coords = coords_grid(1, H, W).cuda().float().contiguous()
150 flow = flow[None].repeat(1, 1, 1, 1).permute([0, 3, 1, 2]).float().contiguous()
151 grid = (flow + coords).permute([0, 2, 3, 1]).contiguous() # (1, H, W, 2)
152 grid = grid[0].cpu()
153 src_pts = []
154 dst_pts = []
155 for y in range(H):
156 for x in range(W):
157 if grid[y,x,0]>=0 and grid[y,x,0]<W and grid[y,x,1]>=0 and grid[y,x,1]<H:
158 src_pts.append((grid[y,x,0], grid[y,x,1]))
159 dst_pts.append((x, y))
160 src_pts = np.float32(src_pts)
161 dst_pts = np.float32(dst_pts)
162
163 M, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
164 if M is None:
165 blends.append(None)
166 continue
167 out = cv2.warpPerspective(marker, M, (scene.shape[1], scene.shape[0]))
168
169 blend_i, mask_i = blend(out, source, scene, blend_type, mask=mask)

Callers 1

evalFunction · 0.85

Calls 5

get_colormapFunction · 0.90
estimateMethod · 0.80
image_flow_warpFunction · 0.70
coords_gridFunction · 0.70
blendFunction · 0.70

Tested by

no test coverage detected