gokaygokay commited on
Commit
d11da87
1 Parent(s): 8d84a1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -5,17 +5,6 @@ import scipy.sparse.linalg as splin
5
  from numba import jit
6
  import gradio as gr
7
 
8
- def get_image(img_path: str, mask: bool=False, scale: bool=True) -> np.array:
9
- if mask:
10
- img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
11
- _, binary_mask = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
12
- return np.where(binary_mask == 255, 1, 0)
13
-
14
- if scale:
15
- return cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB).astype('double') / 255.0
16
-
17
- return cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
18
-
19
  @jit(nopython=True)
20
  def build_poisson_sparse_matrix(ys, xs, im2var, img_s, img_t, mask):
21
  nnz = len(ys)
@@ -195,11 +184,30 @@ def laplacian_blend(img1: np.ndarray, img2: np.ndarray, mask: np.ndarray, depth:
195
 
196
  return np.clip(imgs[-1], 0, 1)
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  def blend_images(bg_img, obj_img, mask_img, method):
199
- # Convert images to the correct format
200
- bg_img = cv2.cvtColor(bg_img, cv2.COLOR_RGB2BGR) / 255.0
201
- obj_img = cv2.cvtColor(obj_img, cv2.COLOR_RGB2BGR) / 255.0
202
- mask_img = cv2.cvtColor(mask_img, cv2.COLOR_RGB2GRAY) / 255.0
203
 
204
  if method == "Poisson":
205
  blend_img = np.zeros_like(bg_img)
@@ -210,7 +218,7 @@ def blend_images(bg_img, obj_img, mask_img, method):
210
  for b in range(3):
211
  blend_img[:,:,b] = mixed_blend_fast_jit(obj_img[:,:,b], mask_img, bg_img[:,:,b].copy())
212
  elif method == "Laplacian":
213
- mask_stack = np.stack((mask_img,) * 3, axis=-1)
214
  blend_img = laplacian_blend(obj_img, bg_img, mask_stack, 5, 25.0)
215
 
216
  return (blend_img * 255).astype(np.uint8)
 
5
  from numba import jit
6
  import gradio as gr
7
 
 
 
 
 
 
 
 
 
 
 
 
8
  @jit(nopython=True)
9
  def build_poisson_sparse_matrix(ys, xs, im2var, img_s, img_t, mask):
10
  nnz = len(ys)
 
184
 
185
  return np.clip(imgs[-1], 0, 1)
186
 
187
+ def get_image(img_path: str, mask: bool=False, scale: bool=True) -> np.array:
188
+ """
189
+ Gets image in appropriate format
190
+ """
191
+ if isinstance(img_path, np.ndarray):
192
+ img = img_path
193
+ else:
194
+ img = cv2.imread(img_path)
195
+
196
+ if mask:
197
+ if len(img.shape) == 3:
198
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
199
+ _, binary_mask = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
200
+ return np.where(binary_mask == 255, 1, 0)
201
+
202
+ if scale:
203
+ return cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype('double') / 255.0
204
+
205
+ return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
206
+
207
  def blend_images(bg_img, obj_img, mask_img, method):
208
+ bg_img = get_image(bg_img)
209
+ obj_img = get_image(obj_img)
210
+ mask_img = get_image(mask_img, mask=True)
 
211
 
212
  if method == "Poisson":
213
  blend_img = np.zeros_like(bg_img)
 
218
  for b in range(3):
219
  blend_img[:,:,b] = mixed_blend_fast_jit(obj_img[:,:,b], mask_img, bg_img[:,:,b].copy())
220
  elif method == "Laplacian":
221
+ mask_stack = np.stack((mask_img.astype(float),) * 3, axis=-1)
222
  blend_img = laplacian_blend(obj_img, bg_img, mask_stack, 5, 25.0)
223
 
224
  return (blend_img * 255).astype(np.uint8)