|
|
import os |
|
|
import cv2 |
|
|
import lpips |
|
|
import torch |
|
|
import numpy as np |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
|
|
|
SPATIAL = True |
|
|
ALEXT = lpips.LPIPS(net='alex', spatial=SPATIAL) |
|
|
VGG = lpips.LPIPS(net='vgg', spatial=SPATIAL) |
|
|
|
|
|
def normalize(img, factor=255./2., cent= 1.): |
|
|
r""" |
|
|
Image Normalizagion |
|
|
[H, W, C], scale : [0 - 1] |
|
|
""" |
|
|
img = (img / factor - cent) |
|
|
return img |
|
|
|
|
|
|
|
|
def resize(img, resize=512): |
|
|
r""" |
|
|
Image Resize using cv2 |
|
|
""" |
|
|
h, w, _ = img.shape |
|
|
if h >= w: |
|
|
scale = resize/h |
|
|
else: |
|
|
scale = resize/w |
|
|
return cv2.resize(img, (int(w * scale), int(h * scale))) |
|
|
|
|
|
def to_tensor(img): |
|
|
r""" |
|
|
make numpy to tensor |
|
|
""" |
|
|
img = torch.Tensor(img[..., np.newaxis].transpose((3, 2, 0, 1))) |
|
|
return img |
|
|
|
|
|
|
|
|
|
|
|
def img_preprocessing(img): |
|
|
img = resize(img) |
|
|
img = normalize(img) |
|
|
img = to_tensor(img) |
|
|
return img |
|
|
|
|
|
def check_img(input_img, gt_img): |
|
|
|
|
|
|
|
|
input_img = img_preprocessing(input_img) |
|
|
gt_img = img_preprocessing(gt_img) |
|
|
|
|
|
|
|
|
similarity = ALEXT.forward(gt_img, input_img) |
|
|
|
|
|
if SPATIAL: |
|
|
return f"{(1 - similarity.mean().item()) *100:.4f}%" |
|
|
else: |
|
|
return f"{(1 - similarity.item()) *100:.4f}%" |