Exemple #1
0
 def _run_script(self, args_str):
     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")
     GUI = cli.GuiArgs(SUBPARSER, "gui",
                       "Launch the Faceswap Graphical User Interface")
     PARSER.set_defaults(func=self.bad_args)
     ARGUMENTS = PARSER.parse_args(args_str.split(' '))
     ARGUMENTS.func(ARGUMENTS)
Exemple #2
0
def SPConvert(context):
    args = context.args

    video = get_all_files(args.refVideo)[0] if args.refVideo else args.refVideo

    PARSER = cli.FullHelpArgumentParser()
    CONVERT = cli.ConvertArgs(
        PARSER, "convert",
        "Convert a source image to a new one with the face swapped")

    argsTransfer = [
        "--input-dir",
        args.inputData1,
        "--model-dir",
        args.inputModel,
        "--output-dir",
        args.outputData,
        "--color-adjustment",
        args.colorAdjustment,
        "--mask-type",
        args.maskType,
        "--scaling",
        args.scaling,
        "--gpus",
        str(args.__gpu),
    ]

    if args.inputData2:
        argsTransfer += [
            "--alignments",
            os.path.join(args.inputData2, "alignments.json"),
        ]

    ARGUMENTS = PARSER.parse_args(argsTransfer)
    ARGUMENTS.func(ARGUMENTS)

    return args.outputData
class MyFaceSwap():

    GPUS = 2

    # ---------------------------------------
    # DATA DIRS:
    # ---------------------------------------

    MODEL_DIR = '_data/video_prep_ffmpeg/model/'

    INPUT_DIR_EXTRACT = '_data/video_prep_ffmpeg/frames/'
    OUTPUT_DIR_EXTRACT = '_data/video_prep_ffmpeg/faces/'

    DIR_A_TRAIN = '_data/video_prep_ffmpeg/faces/emma_360_cut.mp4_faces/'
    DIR_B_TRAIN = '_data/video_prep_ffmpeg/faces/jade_360_cut.mp4_faces/'

    INPUT_DIR_CONVERT = INPUT_DIR_EXTRACT
    OUTPUT_DIR_CONVERT = '_data/video_prep_ffmpeg/frames_swapped/'

    # ---------------------------------------
    # INIT PARSER:
    # ---------------------------------------

    PARSER = cli.FullHelpArgumentParser()
    SUBPARSER = PARSER.add_subparsers()

    cli.ExtractArgs(SUBPARSER, "extract", "Extract the faces from pictures")
    cli.TrainArgs(SUBPARSER, "train",
                  "This command trains the model for the two faces A and B")
    cli.ConvertArgs(
        SUBPARSER, "convert",
        "Convert a source image to a new one with the face swapped")

    PARSER.set_defaults(func=bad_args)

    def __init__(self):
        pass

    def extract(self, input_dir=None, output_dir=None):

        if input_dir is None:
            input_dir = MyFaceSwap.INPUT_DIR_EXTRACT
        if output_dir is None:
            output_dir = MyFaceSwap.OUTPUT_DIR_EXTRACT

        ARGUMENTS = MyFaceSwap.PARSER.parse_args(["extract"])

        ARGUMENTS.input_dir = input_dir
        ARGUMENTS.output_dir = output_dir

        ARGUMENTS.func(ARGUMENTS)

    def train(self,
              input_A=None,
              input_B=None,
              model_dir=None,
              gpus=None,
              preview=True,
              stop_threshold=0,
              stop_iternum=float('inf')):

        if input_A is None:
            input_A = MyFaceSwap.DIR_A_TRAIN
        if input_B is None:
            input_B = MyFaceSwap.DIR_B_TRAIN
        if model_dir is None:
            model_dir = MyFaceSwap.MODEL_DIR
        if gpus is None:
            gpus = MyFaceSwap.GPUS

        ARGUMENTS = MyFaceSwap.PARSER.parse_args(["train"])

        ARGUMENTS.input_A = input_A
        ARGUMENTS.input_B = input_B
        ARGUMENTS.model_dir = model_dir
        ARGUMENTS.gpus = gpus
        ARGUMENTS.preview = preview

        ARGUMENTS.thresh = stop_threshold
        ARGUMENTS.iter_num = stop_iternum

        ARGUMENTS.func(ARGUMENTS)

        # removing backup-files to free space
        import os
        for item in os.listdir(model_dir):
            if item.endswith(".bk"):
                os.remove(os.path.join(model_dir, item))
        print('backup-files were removed')

    def convert(self,
                input_dir=INPUT_DIR_CONVERT,
                output_dir=OUTPUT_DIR_CONVERT,
                model_dir=MODEL_DIR,
                gpus=GPUS):

        ARGUMENTS = MyFaceSwap.PARSER.parse_args(["convert"])

        ARGUMENTS.input_dir = input_dir
        ARGUMENTS.output_dir = output_dir
        ARGUMENTS.model_dir = model_dir
        ARGUMENTS.gpus = gpus

        ARGUMENTS.func(ARGUMENTS)
Exemple #4
0
""" The master faceswap.py script """
import sys
import lib.cli as cli

if sys.version_info[0] < 3:
    raise Exception("This program requires at least python3.2")
if sys.version_info[0] == 3 and sys.version_info[1] < 2:
    raise Exception("This program requires at least python3.2")


def bad_args(args):
    """ Print help on bad 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")
    PARSER.set_defaults(func=bad_args)
    ARGUMENTS = PARSER.parse_args()
    ARGUMENTS.func(ARGUMENTS)