deepmosaic.py 5.3 KB
Newer Older
H
hypox64 已提交
1 2 3 4 5
import os
import numpy as np
import cv2

from models import runmodel,loadmodel
H
hypox64 已提交
6
from util import mosaic,util,ffmpeg,filt
H
hypox64 已提交
7 8 9 10
from util import image_processing as impro
from options import Options

opt = Options().getparse()
H
hypox64 已提交
11
util.file_init(opt)
H
hypox64 已提交
12

H
hypox64 已提交
13 14
def main():
    if opt.mode == 'add':
H
hypox64 已提交
15

H
hypox64 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
        net = loadmodel.unet(opt)
        path = opt.media_path
        if util.is_img(path):
            print('Add Mosaic:',path)
            img = impro.imread(path)
            mask = runmodel.get_ROI_position(img,net,opt)[0]
            img = mosaic.addmosaic(img,mask,opt)
            cv2.imwrite(os.path.join(opt.result_dir,os.path.basename(path)),img)
        elif util.is_video(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()
H
hypox64 已提交
31

H
hypox64 已提交
32 33 34 35 36 37 38 39 40 41 42 43
            # get position
            positions = []
            for imagepath in imagepaths:
                imagepath = os.path.join('./tmp/video2image',imagepath)
                print('Find ROI location:',imagepath)
                img = impro.imread(imagepath)
                mask,x,y,area = runmodel.get_ROI_position(img,net,opt)
                positions.append([x,y,area])      
                cv2.imwrite(os.path.join('./tmp/ROI_mask',
                                          os.path.basename(imagepath)),mask)
            print('Optimize ROI locations...')
            mask_index = filt.position_medfilt(np.array(positions), 7)
H
hypox64 已提交
44

H
hypox64 已提交
45 46 47 48 49 50 51 52 53
            # add mosaic
            print('Add mosaic to images...')
            for i in range(len(imagepaths)):
                mask_path = os.path.join('./tmp/ROI_mask',imagepaths[mask_index[i]])
                mask = impro.imread(mask_path)
                img = impro.imread(os.path.join('./tmp/video2image',imagepaths[i]))
                img = mosaic.addmosaic(img, mask, opt)
                cv2.imwrite(os.path.join('./tmp/addmosaic_image',
                                            os.path.basename(imagepaths[i])),img)
H
hypox64 已提交
54

H
hypox64 已提交
55 56 57 58
            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 已提交
59

H
hypox64 已提交
60 61 62 63 64 65 66
    elif opt.mode == 'clean':
        netG = loadmodel.pix2pix(opt)
        net_mosaic_pos = loadmodel.unet_clean(opt)
        path = opt.media_path
        if util.is_img(path):
            print('Clean Mosaic:',path)
            img_origin = impro.imread(path)
H
hypox64 已提交
67 68
            x,y,size = runmodel.get_mosaic_position(img_origin,net_mosaic_pos,opt)
            img_result = img_origin.copy()
H
hypox64 已提交
69
            if size != 0 :
H
hypox64 已提交
70
                img_mosaic = img_origin[y-size:y+size,x-size:x+size]
H
hypox64 已提交
71
                img_fake = runmodel.run_pix2pix(img_mosaic,netG,opt)
H
hypox64 已提交
72
                img_result = impro.replace_mosaic(img_origin,img_fake,x,y,size,opt.no_feather)
H
hypox64 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
            cv2.imwrite(os.path.join(opt.result_dir,os.path.basename(path)),img_result)

        elif util.is_video(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)
            positions = []
            imagepaths=os.listdir('./tmp/video2image')
            imagepaths.sort()

            # get position
            for imagepath in imagepaths:
                imagepath=os.path.join('./tmp/video2image',imagepath)
                img_origin = impro.imread(imagepath)
                x,y,size = runmodel.get_mosaic_position(img_origin,net_mosaic_pos,opt)
                positions.append([x,y,size])
                print('Find mosaic location:',imagepath)
            print('Optimize mosaic locations...')
            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):
                imagepath=os.path.join('./tmp/video2image',imagepath)
                x,y,size = positions[i][0],positions[i][1],positions[i][2]
                img_origin = impro.imread(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',os.path.basename(imagepath)),img_result)
                print('Clean Mosaic:',imagepath)
            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'))                      
    util.clean_tempfiles(tmp_init = False)
H
hypox64 已提交
112

H
hypox64 已提交
113 114 115 116 117 118 119 120
if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        print('Error:',e)
        input('Please press any key to exit.\n')
        util.clean_tempfiles(tmp_init = False)
        exit(0)