Pawel_Mar
refactors_needed
4574f39
raw
history blame contribute delete
No virus
1.52 kB
import numpy as np
from PIL import Image
import cv2
def save_uploadedfile(uploaded_image, save_path):
im = Image.open(uploaded_image)
if im.mode in ("RGBA", "P"):
im = im.convert("RGB")
im.save(save_path)
def overlay(image, mask, color, alpha, resize=None):
"""Combines image and its segmentation mask into a single image.
Params:
image: Training image. np.ndarray,
mask: Segmentation mask. np.ndarray,
color: Color for segmentation mask rendering. tuple[int, int, int] = (255, 0, 0)
alpha: Segmentation mask's transparency. float = 0.5,
resize: If provided, both image and its mask are resized before blending them together.
tuple[int, int] = (1024, 1024))
Returns:
image_combined: The combined image. np.ndarray
"""
colored_mask = np.expand_dims(mask, 0).repeat(3, axis=0)
colored_mask = np.moveaxis(colored_mask, 0, -1)
masked = np.ma.MaskedArray(image, mask=colored_mask, fill_value=color)
image_overlay = masked.filled()
if resize is not None:
image = cv2.resize(image.transpose(1, 2, 0), resize)
image_overlay = cv2.resize(image_overlay.transpose(1, 2, 0), resize)
image_combined = cv2.addWeighted(image, 1 - alpha, image_overlay, alpha, 0)
return image_combined
def apply_masks(img, masks):
for mask in masks:
h, w, _ = img.shape
mask = cv2.resize(mask, (w, h))
img = overlay(img, mask, color=(0, 255, 0), alpha=0.3)
return img