      parser.print_help()
      exit(0)
  
  if __name__ == "__main__":
      parser = FullHelpArgumentParser()
      subparser = parser.add_subparsers()
      extract = ExtractTrainingData(
          subparser, "extract", "Extract the faces from a pictures.")
      train = TrainingProcessor(
          subparser, "train", "This command trains the model for the two faces A and B.")
      convert = ConvertImage(
          subparser, "convert", "Convert a source image to a new one with the face swapped.")
      guiparsers = {'extract': extract, 'train': train, 'convert': convert}
      gui = TKGui(
          subparser, guiparsers, "gui", "Launch the Faceswap Graphical User Interface.")
      parser.set_defaults(func=bad_args)
      arguments = parser.parse_args()
      arguments.func(arguments)
 
</s> remove     parser.print_help()
 </s> add     """ Print help on bad arguments """
    PARSER.print_help() </s> remove     @staticmethod
    def get_optional_arguments():
        ''' Put the arguments in a list so that they are accessible from both argparse and gui '''
        # Override this for custom arguments
        argument_list = []
        return argument_list

    def parse_arguments(self, description, subparser, command):
        parser = subparser.add_parser(
            command,
            help="This command trains the model for the two faces A and B.",
            description=description,
            epilog="Questions and feedback: \
            https://github.com/deepfakes/faceswap-playground")

        for option in self.argument_list:
            args = option['opts']
            kwargs = {key: option[key] for key in option.keys() if key != 'opts'}
            parser.add_argument(*args, **kwargs)

        parser = self.add_optional_arguments(parser)
        parser.set_defaults(func=self.process_arguments)

    def add_optional_arguments(self, parser):
        for option in self.optional_arguments:
            args = option['opts']
            kwargs = {key: option[key] for key in option.keys() if key != 'opts'}
            parser.add_argument(*args, **kwargs)
        return parser
 </s> add class Train(object):
    """ The training process.  """
    def __init__(self, arguments):
        self.args = arguments
        self.images = self.get_images() </s> remove         extractor_name = "Align" # TODO Pass as argument
        self.extractor = PluginLoader.get_extractor(extractor_name)()
        processes = self.arguments.processes
        try:
            if processes != 1:
                files = list(self.read_directory())
                for filename, faces in tqdm(pool_process(self.processFiles, files, processes=processes), total = len(files)):
                    self.num_faces_detected += 1
                    self.faces_detected[os.path.basename(filename)] = faces
            else:
                for filename in tqdm(self.read_directory()):
                    try:
                        image = cv2.imread(filename)
                        self.faces_detected[os.path.basename(filename)] = self.handleImage(image, filename)
                    except Exception as e:
                        if self.arguments.verbose:
                            print('Failed to extract from image: {}. Reason: {}'.format(filename, e))
                        pass
        finally:
            self.write_alignments()

    def processFiles(self, filename):
 </s> add         """ Perform the extraction process """
        print('Starting, this may take a while...')
        Utils.set_verbosity(self.args.verbose)

        if hasattr(self.args, 'processes') and self.args.processes > 1:
            self.extract_multi_process()
        else:
            self.extract_single_process()

        self.alignments.write_alignments(self.faces.faces_detected)

        images, faces = Utils.finalize(self.images.images_found,
                                       self.faces.num_faces_detected,
                                       self.faces.verify_output)
        self.images.images_found = images
        self.faces.num_faces_detected = faces

    def extract_single_process(self):
        """ Run extraction in a single process """
        for filename in tqdm(self.images.input_images):
            filename, faces = self.process_single_image(filename)
            self.faces.faces_detected[os.path.basename(filename)] = faces

    def extract_multi_process(self):
        """ Run the extraction on the correct number of processes """
        for filename, faces in tqdm(pool_process(self.process_single_image,
                                                 self.images.input_images,
                                                 processes=self.args.processes),
                                    total=self.images.images_found):
            self.faces.num_faces_detected += 1
            self.faces.faces_detected[os.path.basename(filename)] = faces

    def process_single_image(self, filename):
        """ Detect faces in an image. Rotate the image the specified amount
            until at least one face is found, or until image rotations are
            depleted.
            Once at least one face has been detected, pass to process_single_face
            to process the individual faces """
        retval = filename, list() </s> remove     def parse_arguments(self, description, subparser, command):
        """ Parse the command line arguments for the GUI """
        parser = subparser.add_parser(
            command,
            help="This Launches a GUI for Faceswap.",
            description=description,
            epilog="Questions and feedback: \
                    https://github.com/deepfakes/faceswap-playground")

        parser.add_argument('-d', '--debug',
                            action='store_true',
                            dest='debug',
                            default=False,
                            help='Output to Shell console instead of GUI console')
        parser.set_defaults(func=self.process)

    def process(self, arguments):
 </s> add     def process(self): </s> remove         args = {'prog': self.prog, 'message': message}
        self.exit(2, '%(prog)s: error: %(message)s\n' % args)

class DirectoryProcessor(object):
    '''
    Abstract class that processes a directory of images
    and writes output to the specified folder
    '''
    arguments = None
    parser = None

    input_dir = None
    output_dir = None

    images_found = 0
    num_faces_detected = 0
    faces_detected = dict()
    verify_output = False
    rotation_angles = None

    def __init__(self, subparser, command, description='default'):
 </s> add         args = {"prog": self.prog, "message": message}
        self.exit(2, "%(prog)s: error: %(message)s\n" % args)

class FaceSwapArgs(object):
    """ Faceswap argument parser functions that are universal
        to all commands. Should be the parent function of all
        subsequent argparsers """
    def __init__(self, subparser, command, description="default", subparsers=None):
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
      parser.print_help()
      exit(0)
  
  if __name__ == "__main__":
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()
    EXTRACT = cli.ExtractArgs(
        SUBPARSER, "extract", "Extract the faces from pictures")
    TRAIN = cli.TrainArgs(
        SUBPARSER, "train", "This command trains the model for the two faces A and B")
    CONVERT = cli.ConvertArgs(
        SUBPARSER, "convert", "Convert a source image to a new one with the face swapped")
    GUIPARSERS = {'extract': EXTRACT, 'train': TRAIN, 'convert': CONVERT}
    GUI = cli.GuiArgs(
        SUBPARSER, "gui", "Launch the Faceswap Graphical User Interface", GUIPARSERS)
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace
 cli/scripts Refactor (#367)

* Refactor for PEP 8 and split process function

* Remove backwards compatibility for skip frames

* Split optional functions into own class. Make functions more modular

* Conform scripts folder to PEP 8

* train.py - Fix write image bug. Make more modular

* extract.py - Make more modular, Put optional actions into own class

* cli.py - start PEP 8

* cli,py - Pep 8. Refactor and make modular. Bugfixes

* 1st round refactor. Completely untested and probably broken.

* convert.py: Extract alignments from frames if they don't exist

* BugFix: SkipExisting broken since face name refactor

* Extract.py tested

* Minor formatting

* convert.py + train.py amended not tested

* train.py - Semi-fix for hang on reaching target iteration. Now quits on preview mode
Make tensorflow / system warning less verbose

* 2nd pass refactor. Semi tested

bugfixes

* Remove obsolete code. imread/write to Utils

* rename inout.py to fsmedia.py

* Final bugfixes 
faceswap.py