def process_frame( frame: numpy.ndarray, width: int = None, height: int = None, mode: str = "RGB", ) -> numpy.ndarray: """ Use PIL to resize an RGB frame to a specified height and width \ or changing it to a different mode. Args: frame: Target numpy array representing the image that will be resized. width: Width of the resized image. height: Height of the resized image. mode: Passed to Image.convert. Returns: The resized frame that matches the provided width and height. """ height = height or frame.shape[0] width = width or frame.shape[1] frame = Image.fromarray(frame) frame = frame.convert(mode).resize(size=(width, height)) return numpy.array(frame)
def visualize_image( image_name: str, image: np.ndarray, visualization_dir: pathlib.Path, targets: List[inference_types.Target], clf_tiles: List[Tuple[int, int]], ) -> None: """Function used to draw boxes and information onto image for visualizing the output of inference. Args: image_name: The original image name used for saving the visualization. image: The image array. visualization_dir: Where to save the visualizations. targets: A list of the targets that were found during inference. clf_tiles: Which tiles were classified as having targets in them. """ # Create a PIL drawable object. image = Image.fromarray(image).convert("RGBA") image_draw = ImageDraw.Draw(image) # Draw the target rectangles. for target in targets: top_left = (target.x, target.y) bottom_right = (target.x + target.width, target.y + target.height) image_draw.rectangle([top_left, bottom_right], outline=(0, 255, 0), width=2) image_draw.text((target.x, target.y - 10), target.shape.name.lower(), (0, 255, 0)) # Draw an overlay onto the tiles which were classified as having targets. w, h = config.CROP_SIZE overlay = Image.new("RGBA", image.size, (0, 0, 0, 0)) draw = ImageDraw.Draw(overlay) for group in clf_tiles: x, y = group draw.rectangle([(x, y), (x + w, y + h)], fill=(0, 0, 0, 40)) image = Image.alpha_composite(image, overlay) image.convert("RGB").save((visualization_dir / image_name))
def flatten_alpha(img: np.ndarray) -> np.ndarray: """paste an image with alpha onta a solid background""" # convert and prepare PIL images img = Image.fromarray(img) im_flat = Image.new("RGB", img.size, BG_COLOR) # paste using PIL paste im_flat.paste(img.convert("RGB"), (0, 0), img.split()[3]) # return numpy array version return np.array(im_flat)
def hue(im: np.ndarray, hue_lower: float, hue_upper: float) -> np.ndarray: ''' Randomly disturb the hue of the picture, user can use np.random.seed to fix the random behavior. Args: im(np.ndarray): Input image. hue_lower(float): Lower bound of hue. hue_upper(float): Upper bound of hue. ''' hue_delta = np.random.uniform(hue_lower, hue_upper) im = np.array(im.convert('HSV')) im[:, :, 0] = im[:, :, 0] + hue_delta im = PIL.Image.fromarray(im, mode='HSV').convert('RGB') return im
def resize_frame(frame: np.ndarray, height: int, width: int) -> np.ndarray: """ Use PIL to resize an RGB frame to an specified height and width. Args: frame: Target numpy array representing the image that will be resized. height: Height of the resized image. width: Width of the resized image. Returns: The resized frame that matches the provided width and height. """ frame = Image.fromarray(frame) frame = frame.convert("L").resize((height, width)) return np.array(frame)[:, :, None]
def resize_frame(frame: numpy.ndarray, width: int, height: int, mode: str = "RGB") -> numpy.ndarray: """ Use PIL to resize an RGB frame to an specified height and width. Args: frame: Target numpy array representing the image that will be resized. width: Width of the resized image. height: Height of the resized image. mode: Passed to Image.convert. Returns: The resized frame that matches the provided width and height. """ frame = Image.fromarray(frame) frame = frame.convert(mode).resize(size=(width, height)) return numpy.array(frame)