deepmosaic.py 3.1 KB
Newer Older
H
hypox64 已提交
1
import os
H
HypoX64 已提交
2 3
import sys
import traceback
H
hypox64 已提交
4
try:
H
hypox64 已提交
5
    from cores import Options,add,clean,style
H
hypox64 已提交
6 7 8 9 10 11
    from util import util
    from models import loadmodel
except Exception as e:
    print(e)
    input('Please press any key to exit.\n')
    sys.exit(0)
H
hypox64 已提交
12

H
HypoX64 已提交
13
opt = Options().getparse(test_flag = True)
14 15
if not os.path.isdir(opt.temp_dir):
    util.file_init(opt)
H
hypox64 已提交
16

H
hypox64 已提交
17
def main():
H
hypox64 已提交
18
    
H
hypox64 已提交
19 20 21 22
    if os.path.isdir(opt.media_path):
        files = util.Traversal(opt.media_path)
    else:
        files = [opt.media_path]        
H
hypox64 已提交
23
    if opt.mode == 'add':
H
hypox64 已提交
24
        netS = loadmodel.bisenet(opt,'roi')
H
hypox64 已提交
25 26 27
        for file in files:
            opt.media_path = file
            if util.is_img(file):
H
hypox64 已提交
28
                add.addmosaic_img(opt,netS)
H
hypox64 已提交
29
            elif util.is_video(file):
H
hypox64 已提交
30
                add.addmosaic_video(opt,netS)
31
                util.clean_tempfiles(opt, tmp_init = False)
H
hypox64 已提交
32 33
            else:
                print('This type of file is not supported')
34
            util.clean_tempfiles(opt, tmp_init = False)
H
hypox64 已提交
35

H
hypox64 已提交
36
    elif opt.mode == 'clean':
H
hypox64 已提交
37
        netM = loadmodel.bisenet(opt,'mosaic')
38 39 40
        if opt.traditional:
            netG = None
        elif opt.netG == 'video':
H
hypox64 已提交
41
            netG = loadmodel.video(opt)
H
hypox64 已提交
42
        else:
H
hypox64 已提交
43 44 45 46 47
            netG = loadmodel.pix2pix(opt)
        
        for file in files:
            opt.media_path = file
            if util.is_img(file):
H
hypox64 已提交
48
                clean.cleanmosaic_img(opt,netG,netM)
H
hypox64 已提交
49
            elif util.is_video(file):
50
                if opt.netG == 'video' and not opt.traditional:            
H
hypox64 已提交
51
                    clean.cleanmosaic_video_fusion(opt,netG,netM)
H
hypox64 已提交
52
                else:
H
hypox64 已提交
53
                    clean.cleanmosaic_video_byframe(opt,netG,netM)
54
                util.clean_tempfiles(opt, tmp_init = False)
H
hypox64 已提交
55 56
            else:
                print('This type of file is not supported')
H
hypox64 已提交
57

H
hypox64 已提交
58
    elif opt.mode == 'style':
H
hypox64 已提交
59
        netG = loadmodel.style(opt)
H
hypox64 已提交
60 61 62
        for file in files:
            opt.media_path = file
            if util.is_img(file):
H
hypox64 已提交
63
                style.styletransfer_img(opt,netG)
H
hypox64 已提交
64
            elif util.is_video(file):
H
hypox64 已提交
65
                style.styletransfer_video(opt,netG)
66
                util.clean_tempfiles(opt, tmp_init = False)
H
hypox64 已提交
67 68 69
            else:
                print('This type of file is not supported')

70
    util.clean_tempfiles(opt, tmp_init = False)
71

H
HypoX64 已提交
72
if __name__ == '__main__':
73 74 75
    if opt.debug:
        main()
        sys.exit(0)
H
HypoX64 已提交
76 77
    try:
        main()
78
        print('Finished!')
H
HypoX64 已提交
79 80
    except Exception as ex:
        print('--------------------ERROR--------------------')
H
hypox64 已提交
81
        print('--------------Environment--------------')
H
hypox64 已提交
82
        print('DeepMosaics: 0.5.1')
H
hypox64 已提交
83 84 85 86 87 88 89 90 91
        print('Python:',sys.version)
        import torch
        print('Pytorch:',torch.__version__)
        import cv2
        print('OpenCV:',cv2.__version__)
        import platform
        print('Platform:',platform.platform())

        print('--------------BUG--------------')
H
HypoX64 已提交
92 93 94 95 96 97
        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 已提交
98
        #util.clean_tempfiles(tmp_init = False)
99
        sys.exit(0)