core.py 8.4 KB
Newer Older
H
hypox64 已提交
1 2 3 4 5 6 7 8
import os
import numpy as np
import cv2

from models import runmodel,loadmodel
from util import mosaic,util,ffmpeg,filt,data
from util import image_processing as impro

H
hypox64 已提交
9 10 11 12 13 14 15 16 17
def video_init(opt,path):
    util.clean_tempfiles()
    fps = ffmpeg.get_video_infos(path)[0]
    ffmpeg.video2voice(path,'./tmp/voice_tmp.mp3')
    ffmpeg.video2image(path,'./tmp/video2image/output_%05d.'+opt.tempimage_type)
    imagepaths=os.listdir('./tmp/video2image')
    imagepaths.sort()
    return fps,imagepaths

H
hypox64 已提交
18
def addmosaic_img(opt,netS):
H
hypox64 已提交
19 20 21
    path = opt.media_path
    print('Add Mosaic:',path)
    img = impro.imread(path)
H
hypox64 已提交
22
    mask = runmodel.get_ROI_position(img,netS,opt)[0]
H
hypox64 已提交
23
    img = mosaic.addmosaic(img,mask,opt)
24
    impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.jpg'),img)
H
hypox64 已提交
25

H
hypox64 已提交
26
def addmosaic_video(opt,netS):
H
hypox64 已提交
27
    path = opt.media_path
H
hypox64 已提交
28
    fps,imagepaths = video_init(opt,path)
H
hypox64 已提交
29 30
    # get position
    positions = []
H
hypox64 已提交
31
    for i,imagepath in enumerate(imagepaths,1):
H
hypox64 已提交
32
        img = impro.imread(os.path.join('./tmp/video2image',imagepath))
H
hypox64 已提交
33
        mask,x,y,area = runmodel.get_ROI_position(img,netS,opt)
H
hypox64 已提交
34 35
        positions.append([x,y,area])      
        cv2.imwrite(os.path.join('./tmp/ROI_mask',imagepath),mask)
H
hypox64 已提交
36 37
        print('\r','Find ROI location:'+str(i)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='')
    print('\nOptimize ROI locations...')
H
hypox64 已提交
38 39 40 41
    mask_index = filt.position_medfilt(np.array(positions), 7)

    # add mosaic
    for i in range(len(imagepaths)):
H
hypox64 已提交
42
        mask = impro.imread(os.path.join('./tmp/ROI_mask',imagepaths[mask_index[i]]),'gray')
H
hypox64 已提交
43
        img = impro.imread(os.path.join('./tmp/video2image',imagepaths[i]))
H
hypox64 已提交
44 45
        if impro.mask_area(mask)>100:    
            img = mosaic.addmosaic(img, mask, opt)
H
hypox64 已提交
46
        cv2.imwrite(os.path.join('./tmp/addmosaic_image',imagepaths[i]),img)
H
hypox64 已提交
47 48
        print('\r','Add Mosaic:'+str(i+1)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='')
    print()
H
hypox64 已提交
49 50 51 52 53
    ffmpeg.image2video( fps,
                        './tmp/addmosaic_image/output_%05d.'+opt.tempimage_type,
                        './tmp/voice_tmp.mp3',
                         os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.mp4'))

H
hypox64 已提交
54 55 56 57 58
def styletransfer_img(opt,netG):
    print('Style Transfer_img:',opt.media_path)
    img = impro.imread(opt.media_path)
    img = runmodel.run_styletransfer(opt, netG, img)
    suffix = os.path.basename(opt.model_path).replace('.pth','').replace('style_','')
59
    impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(opt.media_path))[0]+'_'+suffix+'.jpg'),img)
H
hypox64 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

def styletransfer_video(opt,netG):
    path = opt.media_path
    positions = []
    fps,imagepaths = video_init(opt,path)

    for i,imagepath in enumerate(imagepaths,1):
        img = impro.imread(os.path.join('./tmp/video2image',imagepath))
        img = runmodel.run_styletransfer(opt, netG, img)
        cv2.imwrite(os.path.join('./tmp/style_transfer',imagepath),img)
        print('\r','Transfer:'+str(i)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='')
    print()
    suffix = os.path.basename(opt.model_path).replace('.pth','').replace('style_','')
    ffmpeg.image2video( fps,
                './tmp/style_transfer/output_%05d.'+opt.tempimage_type,
                './tmp/voice_tmp.mp3',
                 os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_'+suffix+'.mp4'))  

H
hypox64 已提交
78 79
def cleanmosaic_img(opt,netG,netM):

H
hypox64 已提交
80 81 82
    path = opt.media_path
    print('Clean Mosaic:',path)
    img_origin = impro.imread(path)
H
hypox64 已提交
83
    x,y,size,mask = runmodel.get_mosaic_position(img_origin,netM,opt)
H
hypox64 已提交
84
    #cv2.imwrite('./mask/'+os.path.basename(path), mask)
H
hypox64 已提交
85 86 87 88 89 90 91
    img_result = img_origin.copy()
    if size != 0 :
        img_mosaic = img_origin[y-size:y+size,x-size:x+size]
        img_fake = runmodel.run_pix2pix(img_mosaic,netG,opt)
        img_result = impro.replace_mosaic(img_origin,img_fake,x,y,size,opt.no_feather)
    else:
        print('Do not find mosaic')
92
    impro.imwrite(os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_clean.jpg'),img_result)
H
hypox64 已提交
93

H
hypox64 已提交
94
def cleanmosaic_video_byframe(opt,netG,netM):
H
hypox64 已提交
95
    path = opt.media_path
H
hypox64 已提交
96
    fps,imagepaths = video_init(opt,path)
H
hypox64 已提交
97 98
    positions = []
    # get position
H
hypox64 已提交
99
    for i,imagepath in enumerate(imagepaths,1):
H
hypox64 已提交
100
        img_origin = impro.imread(os.path.join('./tmp/video2image',imagepath))
H
hypox64 已提交
101
        x,y,size = runmodel.get_mosaic_position(img_origin,netM,opt)[:3]
H
hypox64 已提交
102
        positions.append([x,y,size])
H
hypox64 已提交
103 104 105
        print('\r','Find mosaic location:'+str(i)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='')

    print('\nOptimize mosaic locations...')
H
hypox64 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118
    positions =np.array(positions)
    for i in range(3):positions[:,i] = filt.medfilt(positions[:,i],opt.medfilt_num)

    # clean mosaic
    for i,imagepath in enumerate(imagepaths,0):
        x,y,size = positions[i][0],positions[i][1],positions[i][2]
        img_origin = impro.imread(os.path.join('./tmp/video2image',imagepath))
        img_result = img_origin.copy()
        if size != 0:
            img_mosaic = img_origin[y-size:y+size,x-size:x+size]
            img_fake = runmodel.run_pix2pix(img_mosaic,netG,opt)
            img_result = impro.replace_mosaic(img_origin,img_fake,x,y,size,opt.no_feather)
        cv2.imwrite(os.path.join('./tmp/replace_mosaic',imagepath),img_result)
H
hypox64 已提交
119 120
        print('\r','Clean Mosaic:'+str(i+1)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='')
    print()
H
hypox64 已提交
121 122 123 124 125
    ffmpeg.image2video( fps,
                './tmp/replace_mosaic/output_%05d.'+opt.tempimage_type,
                './tmp/voice_tmp.mp3',
                 os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_clean.mp4'))  

H
hypox64 已提交
126
def cleanmosaic_video_fusion(opt,netG,netM):
H
hypox64 已提交
127 128
    path = opt.media_path
    N = 25
H
hypox64 已提交
129
    INPUT_SIZE = 128
H
hypox64 已提交
130
    fps,imagepaths = video_init(opt,path)
H
hypox64 已提交
131 132
    positions = []
    # get position
H
hypox64 已提交
133
    for i,imagepath in enumerate(imagepaths,1):
H
hypox64 已提交
134 135
        img_origin = impro.imread(os.path.join('./tmp/video2image',imagepath))
        # x,y,size = runmodel.get_mosaic_position(img_origin,net_mosaic_pos,opt)[:3]
H
hypox64 已提交
136
        x,y,size,mask = runmodel.get_mosaic_position(img_origin,netM,opt)
H
hypox64 已提交
137 138
        cv2.imwrite(os.path.join('./tmp/mosaic_mask',imagepath), mask)
        positions.append([x,y,size])
H
hypox64 已提交
139 140
        print('\r','Find mosaic location:'+str(i)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='')
    print('\nOptimize mosaic locations...')
H
hypox64 已提交
141 142 143 144 145 146 147 148 149 150 151 152
    positions =np.array(positions)
    for i in range(3):positions[:,i] = filt.medfilt(positions[:,i],opt.medfilt_num)

    # clean mosaic
    for i,imagepath in enumerate(imagepaths,0):
        x,y,size = positions[i][0],positions[i][1],positions[i][2]
        img_origin = impro.imread(os.path.join('./tmp/video2image',imagepath))
        mask = cv2.imread(os.path.join('./tmp/mosaic_mask',imagepath),0)
        
        if size==0:
            cv2.imwrite(os.path.join('./tmp/replace_mosaic',imagepath),img_origin)
        else:
H
hypox64 已提交
153
            mosaic_input = np.zeros((INPUT_SIZE,INPUT_SIZE,3*N+1), dtype='uint8')
H
hypox64 已提交
154 155 156
            for j in range(0,N):
                img = impro.imread(os.path.join('./tmp/video2image',imagepaths[np.clip(i+j-12,0,len(imagepaths)-1)]))
                img = img[y-size:y+size,x-size:x+size]
H
hypox64 已提交
157
                img = impro.resize(img,INPUT_SIZE)
H
hypox64 已提交
158 159 160
                mosaic_input[:,:,j*3:(j+1)*3] = img
            mask = impro.resize(mask,np.min(img_origin.shape[:2]))
            mask = mask[y-size:y+size,x-size:x+size]
H
hypox64 已提交
161
            mask = impro.resize(mask, INPUT_SIZE)
H
hypox64 已提交
162
            mosaic_input[:,:,-1] = mask
H
hypox64 已提交
163 164
            mosaic_input = data.im2tensor(mosaic_input,bgr2rgb=False,use_gpu=opt.use_gpu,use_transform = False,is0_1 = False)
            unmosaic_pred = netG(mosaic_input)
H
hypox64 已提交
165
            
H
hypox64 已提交
166 167 168
            #unmosaic_pred = (unmosaic_pred.cpu().detach().numpy()*255)[0]
            #img_fake = unmosaic_pred.transpose((1, 2, 0))
            img_fake = data.tensor2im(unmosaic_pred,rgb2bgr = False ,is0_1 = False)
H
hypox64 已提交
169 170
            img_result = impro.replace_mosaic(img_origin,img_fake,x,y,size,opt.no_feather)
            cv2.imwrite(os.path.join('./tmp/replace_mosaic',imagepath),img_result)
H
hypox64 已提交
171 172
        print('\r','Clean Mosaic:'+str(i+1)+'/'+str(len(imagepaths)),util.get_bar(100*i/len(imagepaths),num=40),end='')
    print()
H
hypox64 已提交
173 174 175 176
    ffmpeg.image2video( fps,
                './tmp/replace_mosaic/output_%05d.'+opt.tempimage_type,
                './tmp/voice_tmp.mp3',
                 os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_clean.mp4'))