deepmosaic.py 2.3 KB
Newer Older
H
hypox64 已提交
1
import os
H
HypoX64 已提交
2 3
import sys
import traceback
H
hypox64 已提交
4 5
from cores import Options,core
from util import util
H
hypox64 已提交
6
from models import loadmodel
H
hypox64 已提交
7

H
HypoX64 已提交
8
opt = Options().getparse(test_flag = True)
H
hypox64 已提交
9
util.file_init(opt)
H
hypox64 已提交
10

H
hypox64 已提交
11
def main():
H
hypox64 已提交
12
    
H
hypox64 已提交
13 14 15 16
    if os.path.isdir(opt.media_path):
        files = util.Traversal(opt.media_path)
    else:
        files = [opt.media_path]        
H
hypox64 已提交
17
    if opt.mode == 'add':
H
hypox64 已提交
18
        netS = loadmodel.bisenet(opt,'roi')
H
hypox64 已提交
19 20 21 22 23 24 25 26
        for file in files:
            opt.media_path = file
            if util.is_img(file):
                core.addmosaic_img(opt,netS)
            elif util.is_video(file):
                core.addmosaic_video(opt,netS)
            else:
                print('This type of file is not supported')
H
hypox64 已提交
27

H
hypox64 已提交
28
    elif opt.mode == 'clean':
H
hypox64 已提交
29
        netM = loadmodel.bisenet(opt,'mosaic')
30 31 32
        if opt.traditional:
            netG = None
        elif opt.netG == 'video':
H
hypox64 已提交
33
            netG = loadmodel.video(opt)
H
hypox64 已提交
34
        else:
H
hypox64 已提交
35 36 37 38 39 40 41
            netG = loadmodel.pix2pix(opt)
        
        for file in files:
            opt.media_path = file
            if util.is_img(file):
                core.cleanmosaic_img(opt,netG,netM)
            elif util.is_video(file):
42
                if opt.netG == 'video' and not opt.traditional:            
H
hypox64 已提交
43 44 45 46 47
                    core.cleanmosaic_video_fusion(opt,netG,netM)
                else:
                    core.cleanmosaic_video_byframe(opt,netG,netM)
            else:
                print('This type of file is not supported')
H
hypox64 已提交
48

H
hypox64 已提交
49
    elif opt.mode == 'style':
H
hypox64 已提交
50
        netG = loadmodel.style(opt)
H
hypox64 已提交
51 52 53 54 55 56 57 58 59
        for file in files:
            opt.media_path = file
            if util.is_img(file):
                core.styletransfer_img(opt,netG)
            elif util.is_video(file):
                core.styletransfer_video(opt,netG)
            else:
                print('This type of file is not supported')

60
    util.clean_tempfiles(opt, tmp_init = False)
H
hypox64 已提交
61
        
H
HypoX64 已提交
62 63 64
if __name__ == '__main__':
    try:
        main()
65
        print('Finished!')
H
HypoX64 已提交
66 67 68 69 70 71 72 73
    except Exception as ex:
        print('--------------------ERROR--------------------')
        ex_type, ex_val, ex_stack = sys.exc_info()
        print('Error Type:',ex_type)
        print(ex_val)
        for stack in traceback.extract_tb(ex_stack):
            print(stack)
        input('Please press any key to exit.\n')
H
hypox64 已提交
74
        #util.clean_tempfiles(tmp_init = False)
H
HypoX64 已提交
75 76
        exit(0)