options.py 7.4 KB
Newer Older
H
hypox64 已提交
1 2
import argparse
import os
3
import sys
H
hypox64 已提交
4

H
hypox64 已提交
5 6 7 8 9 10 11 12 13

class Options():
    def __init__(self):
        self.parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
        self.initialized = False

    def initialize(self):

        #base
14
        self.parser.add_argument('--debug', action='store_true', help='if specified, start debug mode')
15
        self.parser.add_argument('--gpu_id', type=str,default='0', help='if -1, use cpu')
16
        self.parser.add_argument('--media_path', type=str, default='./imgs/ruoruo.jpg',help='your videos or images path')
17
        self.parser.add_argument('-ss', '--start_time', type=str, default='00:00:00',help='start position of video, default is the beginning of video')
H
hypox64 已提交
18
        self.parser.add_argument('-t', '--last_time', type=str, default='00:00:00',help='duration of the video, default is the entire video')
H
HypoX64 已提交
19
        self.parser.add_argument('--mode', type=str, default='auto',help='Program running mode. auto | add | clean | style')
20
        self.parser.add_argument('--model_path', type=str, default='./pretrained_models/mosaic/add_face.pth',help='pretrained model path')
H
hypox64 已提交
21
        self.parser.add_argument('--result_dir', type=str, default='./result',help='output media will be saved here')
22
        self.parser.add_argument('--temp_dir', type=str, default='./tmp', help='Temporary files will go here')
H
hypox64 已提交
23
        self.parser.add_argument('--tempimage_type', type=str, default='jpg',help='type of temp image, png | jpg, png is better but occupy more storage space')
H
hypox64 已提交
24
        self.parser.add_argument('--netG', type=str, default='auto',
25
            help='select model to use for netG(Clean mosaic and Transfer style) -> auto | unet_128 | unet_256 | resnet_9blocks | HD | video')
26
        self.parser.add_argument('--fps', type=int, default=0,help='read and output fps, if 0-> origin')
27
        self.parser.add_argument('--no_preview', action='store_true', help='if specified,do not preview images when processing video. eg.(when run it on server)')
H
HypoX64 已提交
28
        self.parser.add_argument('--output_size', type=int, default=0,help='size of output media, if 0 -> origin')
H
hypox64 已提交
29
        self.parser.add_argument('--mask_threshold', type=int, default=64,help='Mosaic detection threshold (0~255). The smaller is it, the more likely judged as a mosaic area.')
H
hypox64 已提交
30

H
hypox64 已提交
31
        #AddMosaic
H
hypox64 已提交
32 33
        self.parser.add_argument('--mosaic_mod', type=str, default='squa_avg',help='type of mosaic -> squa_avg | squa_random | squa_avg_circle_edge | rect_avg | random')
        self.parser.add_argument('--mosaic_size', type=int, default=0,help='mosaic size,if 0 auto size')
H
HypoX64 已提交
34
        self.parser.add_argument('--mask_extend', type=int, default=10,help='extend mosaic area')
H
hypox64 已提交
35
        
H
hypox64 已提交
36
        #CleanMosaic     
37
        self.parser.add_argument('--mosaic_position_model_path', type=str, default='auto',help='name of model use to find mosaic position')
H
HypoX64 已提交
38
        self.parser.add_argument('--traditional', action='store_true', help='if specified, use traditional image processing methods to clean mosaic')
39 40
        self.parser.add_argument('--tr_blur', type=int, default=10, help='ksize of blur when using traditional method, it will affect final quality')
        self.parser.add_argument('--tr_down', type=int, default=10, help='downsample when using traditional method,it will affect final quality')
H
HypoX64 已提交
41 42
        self.parser.add_argument('--no_feather', action='store_true', help='if specified, no edge feather and color correction, but run faster')
        self.parser.add_argument('--all_mosaic_area', action='store_true', help='if specified, find all mosaic area, else only find the largest area')
H
hypox64 已提交
43
        self.parser.add_argument('--medfilt_num', type=int, default=11,help='medfilt window of mosaic movement in the video')
H
hypox64 已提交
44 45 46
        self.parser.add_argument('--ex_mult', type=str, default='auto',help='mosaic area expansion')
        
        #StyleTransfer
47
        self.parser.add_argument('--preprocess', type=str, default='resize', help='resize and cropping of images at load time [ resize | resize_scale_width | edges | gray] or resize,edges(use comma to split)')
H
HypoX64 已提交
48
        self.parser.add_argument('--edges', action='store_true', help='if specified, use edges to generate pictures,(input_nc = 1)')  
H
hypox64 已提交
49
        self.parser.add_argument('--canny', type=int, default=150,help='threshold of canny')
H
HypoX64 已提交
50
        self.parser.add_argument('--only_edges', action='store_true', help='if specified, output media will be edges')
H
hypox64 已提交
51

H
hypox64 已提交
52 53 54
        self.initialized = True


H
HypoX64 已提交
55
    def getparse(self, test_flag = False):
H
hypox64 已提交
56 57 58
        if not self.initialized:
            self.initialize()
        self.opt = self.parser.parse_args()
59
        
H
hypox64 已提交
60
        model_name = os.path.basename(self.opt.model_path)
61
        self.opt.temp_dir = os.path.join(self.opt.temp_dir, 'DeepMosaics_temp')
62

63 64
        if self.opt.gpu_id != '-1':
            os.environ["CUDA_VISIBLE_DEVICES"] = str(self.opt.gpu_id)
H
BVDNet  
hypox64 已提交
65 66
            import torch
            if not torch.cuda.is_available():
67
                self.opt.gpu_id = '-1'
H
BVDNet  
hypox64 已提交
68
        # else:
69
        #     self.opt.gpu_id = '-1'
H
hypox64 已提交
70

H
HypoX64 已提交
71 72
        if test_flag:
            if not os.path.exists(self.opt.media_path):
73
                print('Error: Media does not exist!')
H
HypoX64 已提交
74
                input('Please press any key to exit.\n')
75
                sys.exit(0)
76 77
            if not os.path.exists(self.opt.model_path):
                print('Error: Model does not exist!')
H
HypoX64 已提交
78
                input('Please press any key to exit.\n')
79
                sys.exit(0)
H
hypox64 已提交
80

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
            if self.opt.mode == 'auto':
                if 'clean' in model_name or self.opt.traditional:
                    self.opt.mode = 'clean'
                elif 'add' in model_name:
                    self.opt.mode = 'add'
                elif 'style' in model_name or 'edges' in model_name:
                    self.opt.mode = 'style'
                else:
                    print('Please check model_path!')
                    input('Please press any key to exit.\n')
                    sys.exit(0)

            if self.opt.output_size == 0 and self.opt.mode == 'style':
                self.opt.output_size = 512

            if 'edges' in model_name or 'edges' in self.opt.preprocess:
                self.opt.edges = True

            if self.opt.netG == 'auto' and self.opt.mode =='clean':
                if 'unet_128' in model_name:
                    self.opt.netG = 'unet_128'
                elif 'resnet_9blocks' in model_name:
                    self.opt.netG = 'resnet_9blocks'
H
hypox64 已提交
104 105
                elif 'HD' in model_name and 'video' not in model_name:
                    self.opt.netG = 'HD'
106 107 108 109 110 111 112 113 114 115 116 117
                elif 'video' in model_name:
                    self.opt.netG = 'video'
                else:
                    print('Type of Generator error!')
                    input('Please press any key to exit.\n')
                    sys.exit(0)

            if self.opt.ex_mult == 'auto':
                if 'face' in model_name:
                    self.opt.ex_mult = 1.1
                else:
                    self.opt.ex_mult = 1.5
H
hypox64 已提交
118
            else:
119 120
                self.opt.ex_mult = float(self.opt.ex_mult)

H
hypox64 已提交
121
            if self.opt.mosaic_position_model_path == 'auto' and self.opt.mode == 'clean':
122 123 124 125 126 127 128
                _path = os.path.join(os.path.split(self.opt.model_path)[0],'mosaic_position.pth')
                if os.path.isfile(_path):
                    self.opt.mosaic_position_model_path = _path
                else:
                    input('Please check mosaic_position_model_path!')
                    input('Please press any key to exit.\n')
                    sys.exit(0)
129

H
hypox64 已提交
130
        return self.opt