def __init__(self, paramConfig, requiredAngles, relatedFiles, baseFace):
        self._config = paramConfig
        self._encodings = []
        self._vamFaces = []
        self._angles = requiredAngles
        self._angles.sort()
        self._facebuckets = {}
        self._baseFace = baseFace

        self._generators = {
            "encoding": self._encodingParams,
            "json": self._jsonParams,
            "eye_mouth_ratio": self._eye_mouth_ratio_params,
            "mouth_chin_ratio": self._mouth_chin_ratio_params,
            "eye_height_width_ratio": self._eye_height_width_ratio_params,
            "nose_height_width_ratio": self._nose_height_width_ratio_params,
            "brow_height_width_ratio": self._brow_height_width_ratio_params,
            "brow_chin_ratio": self._brow_chin_ratio_params,
            "custom_action": self._custom_action
        }

        for angle in self._angles:
            self._facebuckets[angle] = []

        # Read all encodings in from the file list
        for file in relatedFiles:
            try:
                if isinstance(file, EncodedFace):
                    newFace = file
                else:
                    newFace = EncodedFace.createFromFile(file)
                self._encodings.append(newFace)
            except:
                try:
                    vamFace = VamFace(file)
                    vamFace.matchMorphs(baseFace)
                    self._vamFaces.append(vamFace)
                except:
                    continue

        # Now put the encodings into the bucket with the closest angle
        for encoding in self._encodings:
            nearestBucket = abs(self._angles[0])
            for angle in self._angles:
                if abs(abs(encoding.getAngle()) - abs(angle)) < abs(
                        abs(encoding.getAngle()) - abs(nearestBucket)):
                    nearestBucket = abs(angle)
            self._facebuckets[nearestBucket].append(encoding)
Beispiel #2
0
def main(args):
    if args.pydev:
        print("Enabling debugging with pydev")
        import pydevd
        pydevd.settrace(suspend=False)
    inputPath = args.inputJsonPath
    basePath = args.baseJsonPath
    outputPath = args.outputPath
    dirRotateInterval = args.rotateDirectoryInterval
    baseFace = VamFace(basePath)
    baseFace.trimToAnimatable()

    # Read in all of the files from inputpath
    inputFaces = []
    print("Loading input faces from  {}".format(inputPath))
    for entry in glob.glob(os.path.join(inputPath, '*.json')):
        try:
            newFace = VamFace(entry)
            # Only keep the relevant morphs
            morphCnt = len(newFace.morphFloats)
            newFace.matchMorphs(baseFace)
            inputFaces.append(newFace)
        except:
            print("Error loading {}".format(entry))

    print("Loaded {} faces".format(len(inputFaces)))
    if len(inputFaces) == 0:
        print("No starting point faces were loaded!")
        exit(-1)
    faceCnt = 0
    print("Generating variations")

    mutateChance = .6
    mateChance = .7
    faceVariants = [] + inputFaces
    nextRotation = faceCnt + dirRotateInterval
    rotatedOutputPath = getNextDir(outputPath)
    while faceCnt < args.numFaces:
        #for face1 in faceVariants:
        face1 = faceVariants[random.randint(0, len(faceVariants) - 1)]

        # Randomly take parameters from the other face
        shouldMate = random.random() < mateChance
        shouldMutate = random.random() < mutateChance

        if shouldMate or shouldMutate:
            newFace = copy.deepcopy(face1)

            if shouldMate:
                mateIdx = random.randint(0, len(faceVariants) - 1)
                mate(newFace, faceVariants[mateIdx],
                     random.randint(1, len(newFace.morphFloats)))

            # Randomly apply mutations to the current face
            if shouldMutate:
                mutate(newFace, random.randint(0, random.randint(1, 50)))

            newFace.save(
                os.path.join(
                    rotatedOutputPath,
                    "face_variant_{}_{}.json".format(faceCnt,
                                                     random.randint(0,
                                                                    99999))))
            faceVariants.append(newFace)
            faceCnt += 1
            if faceCnt % 500 == 0:
                print("{}/{}".format(faceCnt, args.numFaces))
            if faceCnt >= nextRotation:
                nextRotation = faceCnt + dirRotateInterval
                rotatedOutputPath = getNextDir(outputPath)