snair94 commited on
Commit
3d4810a
·
verified ·
1 Parent(s): 0e293d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -6
app.py CHANGED
@@ -30,6 +30,9 @@ def parse_color(color_str):
30
  raise ValueError(f"Invalid color format: {color_str}. Use hex like '#ff0000', color name like 'red', or rgba format.")
31
 
32
  def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
 
 
 
33
  # Process the input image and prompt
34
  inputs = processor(text=prompt, images=image, return_tensors="pt")
35
  outputs = model(**inputs)
@@ -41,11 +44,9 @@ def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
41
  mask = cv2.GaussianBlur(mask, (15, 15), 0)
42
  mask_bin = (mask > 0.4).astype(np.uint8)
43
 
44
- # Convert image to RGBA
45
- image_np = np.array(image.convert("RGBA"))
46
 
47
  # Resize mask to match image size
48
- mask_resized = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]))
49
  mask_3d = np.stack([mask_bin] * 4, axis=-1) # Extend mask to 3D
50
 
51
  # Convert the color string to an RGB tuple
@@ -54,10 +55,11 @@ def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
54
 
55
  # Create an overlay with the selected color
56
  overlay = np.zeros_like(image_np, dtype=np.uint8)
57
- # overlay[:] = overlay_color
 
58
  masked_image = image_np.copy()
59
- masked_image[mask_bin == 1] = (
60
- 0.5 * masked_image[mask_bin == 1] + 0.5 * overlay[mask_bin == 1]).astype(np.units)
61
 
62
  # Apply the mask to the image
63
  masked_image = np.where(mask_3d == 1, overlay, image_np)
 
30
  raise ValueError(f"Invalid color format: {color_str}. Use hex like '#ff0000', color name like 'red', or rgba format.")
31
 
32
  def apply_mask(image: Image.Image, prompt: str, color: str) -> Image.Image:
33
+ # Convert image to RGBA
34
+ image_np = np.array(image.convert("RGBA"))
35
+
36
  # Process the input image and prompt
37
  inputs = processor(text=prompt, images=image, return_tensors="pt")
38
  outputs = model(**inputs)
 
44
  mask = cv2.GaussianBlur(mask, (15, 15), 0)
45
  mask_bin = (mask > 0.4).astype(np.uint8)
46
 
 
 
47
 
48
  # Resize mask to match image size
49
+ # mask_resized = cv2.resize(mask, (image_np.shape[1], image_np.shape[0]))
50
  mask_3d = np.stack([mask_bin] * 4, axis=-1) # Extend mask to 3D
51
 
52
  # Convert the color string to an RGB tuple
 
55
 
56
  # Create an overlay with the selected color
57
  overlay = np.zeros_like(image_np, dtype=np.uint8)
58
+ overlay[:] = overlay_color
59
+
60
  masked_image = image_np.copy()
61
+ # masked_image[mask_bin == 1] = (
62
+ # 0.5 * masked_image[mask_bin == 1] + 0.5 * overlay[mask_bin == 1]).astype(np.units)
63
 
64
  # Apply the mask to the image
65
  masked_image = np.where(mask_3d == 1, overlay, image_np)