|
import cv2 |
|
|
|
|
|
from gan import DataLoader, DeepModel, tensor2im |
|
|
|
|
|
from opencv_transform.mask_to_maskref import create_maskref |
|
from opencv_transform.maskdet_to_maskfin import create_maskfin |
|
from opencv_transform.dress_to_correct import create_correct |
|
from opencv_transform.nude_to_watermark import create_watermark |
|
|
|
""" |
|
run.py |
|
|
|
This script manage the entire transormation. |
|
|
|
Transformation happens in 6 phases: |
|
0: dress -> correct [opencv] dress_to_correct |
|
1: correct -> mask: [GAN] correct_to_mask |
|
2: mask -> maskref [opencv] mask_to_maskref |
|
3: maskref -> maskdet [GAN] maskref_to_maskdet |
|
4: maskdet -> maskfin [opencv] maskdet_to_maskfin |
|
5: maskfin -> nude [GAN] maskfin_to_nude |
|
6: nude -> watermark [opencv] nude_to_watermark |
|
|
|
""" |
|
|
|
phases = ["dress_to_correct", "correct_to_mask", "mask_to_maskref", "maskref_to_maskdet", "maskdet_to_maskfin", "maskfin_to_nude", "nude_to_watermark"] |
|
|
|
class Options(): |
|
|
|
|
|
def __init__(self): |
|
|
|
|
|
self.norm = 'batch' |
|
self.use_dropout = False |
|
self.data_type = 32 |
|
|
|
|
|
self.batchSize = 1 |
|
self.input_nc = 3 |
|
self.output_nc = 3 |
|
|
|
|
|
self.serial_batches = True |
|
self.nThreads = 1 |
|
self.max_dataset_size = 1 |
|
|
|
|
|
self.netG = 'global' |
|
self.ngf = 64 |
|
self.n_downsample_global = 4 |
|
self.n_blocks_global = 9 |
|
self.n_blocks_local = 0 |
|
self.n_local_enhancers = 0 |
|
self.niter_fix_global = 0 |
|
|
|
|
|
self.checkpoints_dir = "" |
|
self.dataroot = "" |
|
|
|
|
|
def updateOptions(self, phase): |
|
|
|
if phase == "correct_to_mask": |
|
self.checkpoints_dir = "checkpoints/cm.lib" |
|
|
|
elif phase == "maskref_to_maskdet": |
|
self.checkpoints_dir = "checkpoints/mm.lib" |
|
|
|
elif phase == "maskfin_to_nude": |
|
self.checkpoints_dir = "checkpoints/mn.lib" |
|
|
|
|
|
|
|
|
|
def process(cv_img): |
|
|
|
|
|
dress = cv_img |
|
correct = None |
|
mask = None |
|
maskref = None |
|
maskfin = None |
|
maskdet = None |
|
nude = None |
|
watermark = None |
|
|
|
for index, phase in enumerate(phases): |
|
|
|
print("Executing phase: " + phase) |
|
|
|
|
|
if (phase == "correct_to_mask") or (phase == "maskref_to_maskdet") or (phase == "maskfin_to_nude"): |
|
|
|
|
|
opt = Options() |
|
|
|
|
|
opt.updateOptions(phase) |
|
|
|
|
|
if (phase == "correct_to_mask"): |
|
data_loader = DataLoader(opt, correct) |
|
elif (phase == "maskref_to_maskdet"): |
|
data_loader = DataLoader(opt, maskref) |
|
elif (phase == "maskfin_to_nude"): |
|
data_loader = DataLoader(opt, maskfin) |
|
|
|
dataset = data_loader.load_data() |
|
|
|
|
|
model = DeepModel() |
|
model.initialize(opt) |
|
|
|
|
|
for i, data in enumerate(dataset): |
|
|
|
generated = model.inference(data['label'], data['inst']) |
|
|
|
im = tensor2im(generated.data[0]) |
|
|
|
|
|
if (phase == "correct_to_mask"): |
|
mask = cv2.cvtColor(im, cv2.COLOR_RGB2BGR) |
|
|
|
elif (phase == "maskref_to_maskdet"): |
|
maskdet = cv2.cvtColor(im, cv2.COLOR_RGB2BGR) |
|
|
|
elif (phase == "maskfin_to_nude"): |
|
nude = cv2.cvtColor(im, cv2.COLOR_RGB2BGR) |
|
|
|
|
|
elif (phase == 'dress_to_correct'): |
|
correct = create_correct(dress) |
|
|
|
|
|
elif (phase == "mask_to_maskref"): |
|
maskref = create_maskref(mask, correct) |
|
|
|
|
|
elif (phase == "maskdet_to_maskfin"): |
|
maskfin = create_maskfin(maskref, maskdet) |
|
|
|
|
|
elif (phase == "nude_to_watermark"): |
|
watermark = create_watermark(nude) |
|
|
|
return watermark |