|
""" |
|
Copyright (c) 2019-present NAVER Corp. |
|
MIT License |
|
""" |
|
|
|
|
|
import numpy as np |
|
from skimage import io |
|
import cv2 |
|
|
|
def loadImage(img_file): |
|
img = io.imread(img_file) |
|
if img.shape[0] == 2: img = img[0] |
|
if len(img.shape) == 2 : img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) |
|
if img.shape[2] == 4: img = img[:,:,:3] |
|
img = np.array(img) |
|
|
|
return img |
|
|
|
def normalizeMeanVariance(in_img, mean=(0.485, 0.456, 0.406), variance=(0.229, 0.224, 0.225)): |
|
|
|
img = in_img.copy().astype(np.float32) |
|
|
|
img -= np.array([mean[0] * 255.0, mean[1] * 255.0, mean[2] * 255.0], dtype=np.float32) |
|
img /= np.array([variance[0] * 255.0, variance[1] * 255.0, variance[2] * 255.0], dtype=np.float32) |
|
return img |
|
|
|
def denormalizeMeanVariance(in_img, mean=(0.485, 0.456, 0.406), variance=(0.229, 0.224, 0.225)): |
|
|
|
img = in_img.copy() |
|
img *= variance |
|
img += mean |
|
img *= 255.0 |
|
img = np.clip(img, 0, 255).astype(np.uint8) |
|
return img |
|
|
|
def resize_aspect_ratio(img, square_size, interpolation, mag_ratio=1): |
|
height, width, channel = img.shape |
|
|
|
|
|
target_size = mag_ratio * square_size |
|
|
|
|
|
|
|
|
|
|
|
ratio = target_size / max(height, width) |
|
|
|
target_h, target_w = int(round(height * ratio)), int(round(width * ratio)) |
|
proc = cv2.resize(img, (target_w, target_h), interpolation = interpolation) |
|
|
|
MULT = 256 |
|
|
|
|
|
target_h32, target_w32 = target_h, target_w |
|
pad_h = 0 |
|
pad_w = 0 |
|
if target_h % MULT != 0: |
|
pad_h = (MULT - target_h % MULT) |
|
target_h32 = target_h + pad_h |
|
if target_w % MULT != 0: |
|
pad_w = (MULT - target_w % MULT) |
|
target_w32 = target_w + pad_w |
|
resized = np.zeros((target_h32, target_w32, channel), dtype=np.uint8) |
|
resized[0:target_h, 0:target_w, :] = proc |
|
target_h, target_w = target_h32, target_w32 |
|
|
|
size_heatmap = (int(target_w/2), int(target_h/2)) |
|
|
|
return resized, ratio, size_heatmap, pad_w, pad_h |
|
|
|
def cvt2HeatmapImg(img): |
|
img = (np.clip(img, 0, 1) * 255).astype(np.uint8) |
|
img = cv2.applyColorMap(img, cv2.COLORMAP_JET) |
|
return img |
|
|