  from scripts.convert import ConvertImage
  from scripts.gui import TKGui
  
  def bad_args(args):
      parser.print_help()
      exit(0)
  
  if __name__ == "__main__":
      parser = FullHelpArgumentParser()
      subparser = parser.add_subparsers()
 
</s> remove from lib.cli import FullHelpArgumentParser

from scripts.extract import ExtractTrainingData
from scripts.train import TrainingProcessor
from scripts.convert import ConvertImage
from scripts.gui import TKGui
 </s> add  </s> remove     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> add     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) </s> remove # DLIB is a GPU Memory hog, so the following modules should only be imported when required
detect_faces = None
DetectedFace = None
FaceFilter = None

def import_faces_detect():
    ''' Import the faces_detect module only when it is required '''
    global detect_faces
    global DetectedFace
    if detect_faces is None or DetectedFace is None:
        import lib.faces_detect 
        detect_faces = lib.faces_detect.detect_faces
        DetectedFace = lib.faces_detect.DetectedFace
 </s> add from plugins.PluginLoader import PluginLoader

class ScriptExecutor(object):
    """ Loads the relevant script modules and executes the script.
        This class is initialised in each of the argparsers for the relevant command,
        then execute script is called within their set_default function """

    def __init__(self, command, subparsers=None):
        self.command = command.lower()
        self.subparsers = subparsers

    def import_script(self):
        """ Only import a script's modules when running that script."""
        if self.command == 'extract':
            from scripts.extract import Extract as script
        elif self.command == 'train':
            from scripts.train import Train as script
        elif self.command == 'convert':
            from scripts.convert import Convert as script
        elif self.command == 'gui':
            from scripts.gui import Gui as script
        else:
            script = None
        return script </s> remove tf = None
set_session = None
def import_tensorflow_keras():
    ''' Import the TensorFlow and keras set_session modules only when they are required '''
    global tf
    global set_session
    if tf is None or set_session is None:
        import tensorflow
        import keras.backend.tensorflow_backend
        tf = tensorflow
        set_session = keras.backend.tensorflow_backend.set_session

class TrainingProcessor(object):
    arguments = None

    def __init__(self, subparser, command, description='default'):
        self.argument_list = self.get_argument_list()
        self.optional_arguments = self.get_optional_arguments()
        self.parse_arguments(description, subparser, command)
        self.lock = threading.Lock()
 </s> add import cv2
import tensorflow as tf
from keras.backend.tensorflow_backend import set_session </s> remove                 trainer.train_one_step(epoch, self.show if (save_iteration or self.save_now) else None)
 </s> add             self.run_training_cycle(model, trainer)
        except KeyboardInterrupt:
            try:
                model.save_weights()
            except KeyboardInterrupt:
                print("Saving model weights has been cancelled!")
            exit(0)
        except Exception as err:
            raise err

    def load_model(self):
        """ Load the model requested for training """
        model_dir = get_folder(self.args.model_dir)
        model = PluginLoader.get_model(self.trainer_name)(model_dir, self.args.gpus)

        model.load(swapped=False)
        return model

    def load_trainer(self, model):
        """ Load the trainer requested for traning """
        images_a, images_b = self.images

        trainer = PluginLoader.get_trainer(self.trainer_name)
        trainer = trainer(model,
                          images_a,
                          images_b,
                          self.args.batch_size,
                          self.args.perceptual_loss)
        return trainer

    def run_training_cycle(self, model, trainer):
        """ Perform the training cycle """
        for epoch in range(0, self.args.epochs):
            save_iteration = epoch % self.args.save_interval == 0
            viewer = self.show if save_iteration or self.save_now else None
            trainer.train_one_step(epoch, viewer)
            if self.stop:
                break
            elif save_iteration:
                model.save_weights()
            elif self.save_now:
                model.save_weights()
                self.save_now = False
        model.save_weights()
        self.stop = True
    """ Print help on bad arguments """
    PARSER.print_help()
  from scripts.convert import ConvertImage
  from scripts.gui import TKGui
  
  def bad_args(args):
    """ Print help on bad arguments """
    PARSER.print_help()
      exit(0)
  
  if __name__ == "__main__":
      parser = FullHelpArgumentParser()
      subparser = parser.add_subparsers()
keep keep keep keep replace keep keep keep keep keep
 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