deepmosaic.py 4.8 KB
Newer Older
H
hypox64 已提交
1 2 3 4 5 6 7 8 9
import sys
import os
import random

import numpy as np
import cv2
import torch

from models import runmodel,loadmodel
H
hypox64 已提交
10
from util import mosaic,util,ffmpeg,filt
H
hypox64 已提交
11 12 13 14 15
from util import image_processing as impro
from options import Options


opt = Options().getparse()
H
hypox64 已提交
16
util.file_init(opt)
H
hypox64 已提交
17 18 19 20 21 22 23

if opt.mode == 'add':

    net = loadmodel.unet(opt)
    path = opt.media_path
    if util.is_img(path):
        print('Add Mosaic:',path)
24
        img = impro.imread(path)
H
hypox64 已提交
25 26 27 28 29 30 31
        img = runmodel.add_mosaic_to_image(img,net,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)
H
hypox64 已提交
32 33
        imagepaths=os.listdir('./tmp/video2image')
        imagepaths.sort()
H
hypox64 已提交
34 35 36

        # get position
        positions = []
H
hypox64 已提交
37
        for imagepath in imagepaths:
H
hypox64 已提交
38
            imagepath = os.path.join('./tmp/video2image',imagepath)
H
hypox64 已提交
39
            print('Find ROI location:',imagepath)
40
            img = impro.imread(imagepath)
H
hypox64 已提交
41 42 43 44
            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)
H
hypox64 已提交
45
        print('Optimize ROI locations...')
H
hypox64 已提交
46 47 48 49 50 51 52 53 54
        mask_index = filt.position_medfilt(np.array(positions), 7)

        # 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)
H
hypox64 已提交
55
            cv2.imwrite(os.path.join('./tmp/addmosaic_image',
H
hypox64 已提交
56 57
                                        os.path.basename(imagepaths[i])),img)

H
hypox64 已提交
58 59 60
        ffmpeg.image2video( fps,
                            './tmp/addmosaic_image/output_%05d.'+opt.tempimage_type,
                            './tmp/voice_tmp.mp3',
H
hypox64 已提交
61
                             os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_add.mp4'))
H
hypox64 已提交
62 63 64 65 66 67 68

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)
69
        img_origin = impro.imread(path)
H
hypox64 已提交
70 71 72 73
        x,y,size = runmodel.get_mosaic_position(img_origin,net_mosaic_pos,opt)
        img_result = img_origin.copy()
        if size != 0 :
            img_mosaic = img_origin[y-size:y+size,x-size:x+size]
H
hypox64 已提交
74
            img_fake=runmodel.run_pix2pix(img_mosaic,netG,opt)
H
hypox64 已提交
75 76 77 78 79 80 81 82 83 84 85
            img_result = impro.replace_mosaic(img_origin,img_fake,x,y,size,opt.no_feather)
        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()
H
hypox64 已提交
86 87

        # get position
H
hypox64 已提交
88 89
        for imagepath in imagepaths:
            imagepath=os.path.join('./tmp/video2image',imagepath)
90
            img_origin = impro.imread(imagepath)
H
hypox64 已提交
91 92
            x,y,size = runmodel.get_mosaic_position(img_origin,net_mosaic_pos,opt)
            positions.append([x,y,size])
H
hypox64 已提交
93 94
            print('Find mosaic location:',imagepath)
        print('Optimize mosaic locations...')
H
hypox64 已提交
95
        positions =np.array(positions)
H
hypox64 已提交
96
        for i in range(3):positions[:,i] = filt.medfilt(positions[:,i],opt.medfilt_num)
H
hypox64 已提交
97

H
hypox64 已提交
98
        # clean mosaic
H
hypox64 已提交
99 100 101
        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]
102
            img_origin = impro.imread(imagepath)
H
hypox64 已提交
103 104 105
            img_result = img_origin.copy()
            if size != 0:
                img_mosaic = img_origin[y-size:y+size,x-size:x+size]
H
hypox64 已提交
106
                img_fake = runmodel.run_pix2pix(img_mosaic,netG,opt)
H
hypox64 已提交
107 108 109 110 111 112
                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',
H
hypox64 已提交
113
                     os.path.join(opt.result_dir,os.path.splitext(os.path.basename(path))[0]+'_clean.mp4'))                      
H
hypox64 已提交
114

H
hypox64 已提交
115
util.clean_tempfiles(tmp_init = False)