runmodel.py 2.9 KB
Newer Older
H
hypox64 已提交
1
import cv2
HypoX64's avatar
preview  
HypoX64 已提交
2 3 4
import sys
sys.path.append("..")
import util.image_processing as impro
H
hypox64 已提交
5
from util import mosaic
HypoX64's avatar
preview  
HypoX64 已提交
6 7
from util import data
import torch
H
hypox64 已提交
8
import numpy as np
HypoX64's avatar
preview  
HypoX64 已提交
9

H
hypox64 已提交
10
def run_segment(img,net,size = 360,gpu_id = '-1'):
HypoX64's avatar
preview  
HypoX64 已提交
11
    img = impro.resize(img,size)
H
hypox64 已提交
12
    img = data.im2tensor(img,gpu_id = gpu_id, bgr2rgb = False, is0_1 = True)
13
    mask = net(img)
H
hypox64 已提交
14
    mask = data.tensor2im(mask, gray=True, is0_1 = True)
HypoX64's avatar
preview  
HypoX64 已提交
15 16
    return mask

H
hypox64 已提交
17 18 19 20 21
def run_pix2pix(img,net,opt):
    if opt.netG == 'HD':
        img = impro.resize(img,512)
    else:
        img = impro.resize(img,128)
22
    img = data.im2tensor(img,gpu_id=opt.gpu_id)
HypoX64's avatar
preview  
HypoX64 已提交
23 24 25
    img_fake = net(img)
    img_fake = data.tensor2im(img_fake)
    return img_fake
H
hypox64 已提交
26

27 28 29 30 31 32 33
def traditional_cleaner(img,opt):
    h,w = img.shape[:2]
    img = cv2.blur(img, (opt.tr_blur,opt.tr_blur))
    img = img[::opt.tr_down,::opt.tr_down,:]
    img = cv2.resize(img, (w,h),interpolation=cv2.INTER_LANCZOS4)
    return img

H
hypox64 已提交
34
def run_styletransfer(opt, net, img):
35

H
hypox64 已提交
36
    if opt.output_size != 0:
37 38 39 40 41 42 43
        if 'resize' in opt.preprocess and 'resize_scale_width' not in opt.preprocess:
            img = impro.resize(img,opt.output_size)
        elif 'resize_scale_width' in opt.preprocess:
            img = cv2.resize(img, (opt.output_size,opt.output_size))
        img = img[0:4*int(img.shape[0]/4),0:4*int(img.shape[1]/4),:]

    if 'edges' in opt.preprocess:
H
hypox64 已提交
44 45 46 47 48 49 50 51 52
        if opt.canny > 100:
            canny_low = opt.canny-50
            canny_high = np.clip(opt.canny+50,0,255)
        elif opt.canny < 50:
            canny_low = np.clip(opt.canny-25,0,255)
            canny_high = opt.canny+25
        else:
            canny_low = opt.canny-int(opt.canny/2)
            canny_high = opt.canny+int(opt.canny/2)
H
hypox64 已提交
53
        img = cv2.Canny(img,canny_low,canny_high)
H
hypox64 已提交
54 55
        if opt.only_edges:
            return img
H
hypox64 已提交
56
        img = data.im2tensor(img,gpu_id=opt.gpu_id,gray=True)
H
hypox64 已提交
57
    else:    
H
hypox64 已提交
58
        img = data.im2tensor(img,gpu_id=opt.gpu_id)
H
hypox64 已提交
59 60 61 62
    img = net(img)
    img = data.tensor2im(img)
    return img

H
hypox64 已提交
63
def get_ROI_position(img,net,opt,keepsize=True):
64
    mask = run_segment(img,net,size=360,gpu_id = opt.gpu_id)
H
hypox64 已提交
65
    mask = impro.mask_threshold(mask,opt.mask_extend,opt.mask_threshold)
H
hypox64 已提交
66 67
    if keepsize:
        mask = impro.resize_like(mask, img)
H
hypox64 已提交
68
    x,y,halfsize,area = impro.boundingSquare(mask, 1)
H
hypox64 已提交
69
    return mask,x,y,halfsize,area
H
hypox64 已提交
70

H
hypox64 已提交
71 72
def get_mosaic_position(img_origin,net_mosaic_pos,opt):
    h,w = img_origin.shape[:2]
73
    mask = run_segment(img_origin,net_mosaic_pos,size=360,gpu_id = opt.gpu_id)
H
hypox64 已提交
74 75
    # mask_1 = mask.copy()
    mask = impro.mask_threshold(mask,ex_mun=int(min(h,w)/20),threshold=opt.mask_threshold)
76 77
    if not opt.all_mosaic_area:
        mask = impro.find_mostlikely_ROI(mask)
H
hypox64 已提交
78
    x,y,size,area = impro.boundingSquare(mask,Ex_mul=opt.ex_mult)
H
hypox64 已提交
79 80
    #Location fix
    rat = min(h,w)/360.0
H
hypox64 已提交
81
    x,y,size = int(rat*x),int(rat*y),int(rat*size)
H
hypox64 已提交
82 83 84
    x,y = np.clip(x, 0, w),np.clip(y, 0, h)
    size = np.clip(size, 0, min(w-x,h-y))
    # print(x,y,size)
H
hypox64 已提交
85
    return x,y,size,mask