예제 #1
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    align = NaiveDlib(args.dlibFaceMean, args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        imgName = "{}/{}.png".format(outDir, imgObject.name)
        openface.helper.mkdirP(outDir)
        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB(cache=False)
            out = align.alignImg(args.method, args.size, rgb,
                outputPrefix=outDir,
                outputDebug=args.outputDebugImages)
            if args.fallbackLfw and out is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw,
                                                               imgObject.cls),
                                                  imgObject.name)
                shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(args.outputDir,
                                                                          imgObject.cls),
                                                             imgObject.name))

            if out is not None:
                io.imsave(imgName, out)
    print('nFallbacks:', nFallbacks)
예제 #2
0
    def alignMain(self):
        """ ***** """
        warnings.filterwarnings(action='ignore', category=DeprecationWarning)


        openface.helper.mkdirP(self.__aligned_images_dir)
        imgs = list(iterImgs(self.__traininig_images_dir))
        random.shuffle(imgs)

        for imgObject in imgs:
            out_dir = os.path.join(self.__aligned_images_dir, imgObject.cls)
            openface.helper.mkdirP(out_dir)
            outputPrefix = os.path.join(out_dir, imgObject.name)
            imgName = outputPrefix + ".png"

            if os.path.isfile(imgName):
                print("  {} Already found, skipping.".format(imgName))
            else:
                rgb = imgObject.getRGB()
                if rgb is None:
                    print(" {} Unable to load.".format(imgName))
                    outRgb = None
                else:
                    outRgb = self.__align.align(AlignAndXformFaces.__Image_dim,
                                                rgb,
                                                landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE,
                                                skipMulti=True)
                    if outRgb is None:
                        print("  {} Unable to align.".format(imgName))
                if outRgb is not None:
                    print("  {} Writing aligned file to disk.".format(imgName))
                    outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                    cv2.imwrite(imgName, outBgr)
        return
예제 #3
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    if args.landmarks == 'outerEyesAndNose':
        landmarkIndices = openface.AlignDlib.OUTER_EYES_AND_NOSE
    elif args.landmarks == 'innerEyesAndBottomLip':
        landmarkIndices = openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    else:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    align = openface.AlignDlib(args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(args.outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            if args.verbose:
                print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                if args.verbose:
                    print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(args.size,
                                     rgb,
                                     landmarkIndices=landmarkIndices,
                                     skipMulti=args.skipMulti)
                if outRgb is None and args.verbose:
                    print("  + Unable to align.")

            if args.fallbackLfw and outRgb is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(
                    os.path.join(args.fallbackLfw, imgObject.cls),
                    imgObject.name)
                shutil.copy(
                    deepFunneled, "{}/{}.jpg".format(
                        os.path.join(args.outputDir, imgObject.cls),
                        imgObject.name))

            if outRgb is not None:
                if args.verbose:
                    print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)

    if args.fallbackLfw:
        print('nFallbacks:', nFallbacks)
예제 #4
0
def computeMeanMain(args):
    align = openface.AlignDlib(args.dlibFacePredictor)

    imgs = list(iterImgs(args.inputDir))
    if args.numImages > 0:
        imgs = random.sample(imgs, args.numImages)

    facePoints = []
    for img in imgs:
        rgb = img.getRGB()
        bb = align.getLargestFaceBoundingBox(rgb)
        alignedPoints = align.align(rgb, bb)
        if alignedPoints:
            facePoints.append(alignedPoints)

    facePointsNp = np.array(facePoints)
    mean = np.mean(facePointsNp, axis=0)
    std = np.std(facePointsNp, axis=0)

    write(mean, "{}/mean.csv".format(args.modelDir))
    write(std, "{}/std.csv".format(args.modelDir))

    # Only import in this mode.
    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()
    ax.scatter(mean[:, 0], -mean[:, 1], color='k')
    ax.axis('equal')
    for i, p in enumerate(mean):
        ax.annotate(str(i), (p[0] + 0.005, -p[1] + 0.005), fontsize=8)
    plt.savefig("{}/mean.png".format(args.modelDir))
예제 #5
0
def align_faces_dlib():
    openface.helper.mkdirP(img_train_aligned_path)

    images = list(iterImgs(img_train_raw_path))

    align_dlib = openface.AlignDlib(dlib_face_predictor)

    for image in images:
        aligned_person_face_path = os.path.join(img_train_aligned_path,
                                                image.cls)
        openface.helper.mkdirP(aligned_person_face_path)
        aligned_numbered_person_face_path = os.path.join(
            aligned_person_face_path, image.name)
        aligned_image_name = aligned_numbered_person_face_path + ".png"

        image_rgb = image.getRGB()
        if image_rgb is None:
            print("Unable to load image {}").format(image.name)
            aligned_image_rgb = None
        else:
            aligned_image_rgb = align_dlib.align(
                aligned_image_size,
                image_rgb,
                landmarkIndices=align_landmark_indices,
                skipMulti=align_images_with_multiple_faces)
            if aligned_image_rgb is None:
                print("Unable to align image {}").format(image.name)

        if aligned_image_rgb is not None:
            aligned_image_bgr = cv2.cvtColor(aligned_image_rgb,
                                             cv2.COLOR_RGB2BGR)
            cv2.imwrite(aligned_image_name, aligned_image_bgr)
예제 #6
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if args.landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    landmarkIndices = landmarkMap[args.landmarks]

    align = openface.AlignDlib(args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(args.outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            if args.verbose:
                print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                if args.verbose:
                    print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(args.size, rgb,
                                     landmarkIndices=landmarkIndices,
                                     skipMulti=args.skipMulti)
                if outRgb is None and args.verbose:
                    print("  + Unable to align.")

            if args.fallbackLfw and outRgb is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw,
                                                               imgObject.cls),
                                                  imgObject.name)
                shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(args.outputDir,
                                                                          imgObject.cls),
                                                             imgObject.name))

            if outRgb is not None:
                if args.verbose:
                    print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)

    if args.fallbackLfw:
        print('nFallbacks:', nFallbacks)
예제 #7
0
def align_faces_dlib():
    openface.helper.mkdirP(img_train_aligned_path)

    images = list(iterImgs(img_train_raw_path))

    align_dlib = openface.AlignDlib(dlib_face_predictor)

    for image in images:
        aligned_person_face_path = os.path.join(img_train_aligned_path, image.cls)
        openface.helper.mkdirP(aligned_person_face_path)
        aligned_numbered_person_face_path = os.path.join(aligned_person_face_path, image.name)
        aligned_image_name = aligned_numbered_person_face_path + ".png"

        image_rgb = image.getRGB()
        if image_rgb is None:
            print("Unable to load image {}").format(image.name)
            aligned_image_rgb = None
        else:
            aligned_image_rgb = align_dlib.align(aligned_image_size, image_rgb, landmarkIndices=align_landmark_indices, skipMulti = align_images_with_multiple_faces)
            if aligned_image_rgb is None:
                print("Unable to align image {}").format(image.name)

        if aligned_image_rgb is not None:
            aligned_image_bgr = cv2.cvtColor(aligned_image_rgb, cv2.COLOR_RGB2BGR)
            cv2.imwrite(aligned_image_name, aligned_image_bgr)
예제 #8
0
def computeMeanMain(args):
    align = NaiveDlib(args.dlibFaceMean, args.dlibFacePredictor)

    imgs = list(iterImgs(args.inputDir))
    if args.numImages > 0:
        imgs = random.sample(imgs, args.numImages)

    facePoints = []
    for img in imgs:
        rgb = img.getRGB()
        bb = align.getLargestFaceBoundingBox(rgb)
        alignedPoints = align.align(rgb, bb)
        if alignedPoints:
            facePoints.append(alignedPoints)

    facePointsNp = np.array(facePoints)
    mean = np.mean(facePointsNp, axis=0)
    std = np.std(facePointsNp, axis=0)

    write(mean, "{}/mean.csv".format(args.modelDir))
    write(std, "{}/std.csv".format(args.modelDir))

    # Only import in this mode.
    import matplotlib as mpl
    mpl.use('Agg')
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()
    ax.scatter(mean[:, 0], -mean[:, 1], color='k')
    ax.axis('equal')
    for i, p in enumerate(mean):
        ax.annotate(str(i), (p[0] + 0.005, -p[1] + 0.005), fontsize=8)
    plt.savefig("{}/mean.png".format(args.modelDir))
예제 #9
0
    def align_images(self, input_dir):
        imgs = list(iterImgs(input_dir))
        # Shuffle so multiple versions can be run at once.
        random.shuffle(imgs)

        for imgObject in imgs:
            outDir = os.path.join(self.aligned_dir, imgObject.cls)
            if not os.path.isdir(outDir):
                os.makedirs(outDir)
            outputPrefix = os.path.join(outDir, imgObject.name)
            imgName = outputPrefix + ".png"

            if not os.path.isfile(imgName):
                rgb = imgObject.getRGB()
                if rgb is None:
                    outRgb = None
                else:
                    outRgb = self.align.align(
                        self.imgDim, rgb, landmarkIndices=self.landmarkIndices)
                if outRgb is not None:
                    outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                    cv2.imwrite(imgName, outBgr)
                    logger.info("Write image {}".format(imgName))
                else:
                    os.remove(imgObject.path)
                    logger.warn("No face was detected in {}. Removed.".format(
                        imgObject.path))
예제 #10
0
    def align_images(self, input_dir):
        imgs = list(iterImgs(input_dir))
        # Shuffle so multiple versions can be run at once.
        random.shuffle(imgs)

        for imgObject in imgs:
            outDir = os.path.join(self.aligned_dir, imgObject.cls)
            if not os.path.isdir(outDir):
                os.makedirs(outDir)
            outputPrefix = os.path.join(outDir, imgObject.name)
            imgName = outputPrefix + ".png"

            if not os.path.isfile(imgName):
                rgb = imgObject.getRGB()
                if rgb is None:
                    outRgb = None
                else:
                    outRgb = self.align.align(
                            self.imgDim, rgb,
                            landmarkIndices=self.landmarkIndices)
                if outRgb is not None:
                    outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                    cv2.imwrite(imgName, outBgr)
                    logger.info("Write image {}".format(imgName))
                else:
                    os.remove(imgObject.path)
                    logger.warn("No face was detected in {}. Removed.".format(imgObject.path))
            else:
                logger.debug("Skip image {}".format(imgName))
예제 #11
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    align = NaiveDlib(args.dlibFaceMean, args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        imgName = "{}/{}.png".format(outDir, imgObject.name)
        openface.helper.mkdirP(outDir)
        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB(cache=False)
            out = align.alignImg(args.method, args.size, rgb)
            if args.fallbackLfw and out is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(
                    os.path.join(args.fallbackLfw, imgObject.cls),
                    imgObject.name)
                shutil.copy(
                    deepFunneled, "{}/{}.jpg".format(
                        os.path.join(args.outputDir, imgObject.cls),
                        imgObject.name))

            if out is not None:
                io.imsave(imgName, out)
    print('nFallbacks:', nFallbacks)
예제 #12
0
def detectAlignImages(args):
    pool = ThreadPool(5)
    imgs = list(iterImgs(args.input))
    imgs.sort(key=lambda x: x.path)
    print("All images is listed")

    aligner = NaiveDlib(args.dlibFaceMean, args.dlibFacePredictor)
    bundles = []
    current_class = None
    class_num = -1
    for imgObject in imgs:
        if imgObject.cls != current_class:
            class_num += 1
            current_class = imgObject.cls
        #descr.write(os.path.join(imgObject.cls, imgObject.name) + " " + str(class_num) + "\n")
        bundles.append((args, imgObject, aligner, class_num))
    #print("Description is generated")
    exists = pool.map(alignImage, bundles)

    pool.close()
    pool.join()
    with open(os.path.join(args.output, 'description.txt'), 'w') as descr:
        for (exist, obj) in zip(exists, bundles):
            if exist:
                descr.write(
                    os.path.join(obj[1].cls, obj[1].name) + " " + str(obj[3]) +
                    "\n")
예제 #13
0
def alignMain(outputDir,inputDir,landmarks,dlibFacePredictor,verbose,size,skipMulti):
    openface.helper.mkdirP(outputDir)

    imgs = list(iterImgs(inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    landmarkIndices = landmarkMap[landmarks]

    align = openface.AlignDlib(dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix

        if os.path.isfile(imgName):
            if verbose:
                print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                if verbose:
                    print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(size, rgb,
                                     landmarkIndices=landmarkIndices,
                                     skipMulti=skipMulti)
                if outRgb is None and verbose:
                    print("  + Unable to align.")

            # if args.fallbackLfw and outRgb is None:
            #     nFallbacks += 1
            #     deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw,
            #                                                    imgObject.cls),
            #                                       imgObject.name)
            #     shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(outputDir,
            #                                                               imgObject.cls),
            #                                                  imgObject.name))

            if outRgb is not None:
                if verbose:
                    print("  + Writing aligned file to disk.")
                for i,rgb in enumerate(outRgb):
                    outBgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)
                    cv2.imwrite(imgName+str(i)+ ".png", outBgr)
예제 #14
0
def alignMain(outputDir, inputDir, landmark="outerEyesAndNose"):
    """function that performs the alignment of faces in the input directory using the provided landmarks and writes the aligned faces to disk in the provided outputDir

    paramters: outputDir, inputDir, landmark
    type parameter: string, string, string

    return: aligned files are written to disk"""
    openface.helper.mkdirP(outputDir)

    imgs = list(iterImgs(inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if landmark not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmark))

    landmarkIndices = landmarkMap[landmark]

    align = openface.AlignDlib("shape_predictor_68_face_landmarks.dat")

    nFallbacks = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(96, rgb,
                                     landmarkIndices=landmarkIndices,
                                     skipMulti=True)
                if outRgb is None:
                    print("  + Unable to align.")

            if outRgb is not None:
                print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)
예제 #15
0
def alignMain(inputDir, output, landmarks, dlibFacePredictor, size):
    # input_save("input/")
    imgs = list(iterImgs(inputDir))
    name = inputDir.split("/")[1]
    output = os.path.join(output, name)
    # print name
    # print output
    # exit(-1)

    openface.helper.mkdirP(output)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    # innerEyesAndBottomLip daha duzgun aliyor.
    if landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    landmarkIndices = landmarkMap[landmarks]

    align = openface.AlignDlib(dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        path = "face/hepsi/" + str(imgObject) + ".png"

        # print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(output, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                # print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(size,
                                     rgb,
                                     landmarkIndices=landmarkIndices)
                if outRgb is None:
                    print("  + Unable to align.")

            if outRgb is not None:
                # print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)
예제 #16
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    if args.landmarks == 'outerEyesAndNose':
        landmarkIndices = NaiveDlib.OUTER_EYES_AND_NOSE
    elif args.landmarks == 'innerEyesAndBottomLip':
        landmarkIndices = NaiveDlib.INNER_EYES_AND_BOTTOM_LIP
    else:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    align = NaiveDlib(args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB()
            if rgb is not None:
                print(imgName, type(rgb), rgb.shape)
                outRgb = align.alignImg('affine',
                                        args.size,
                                        rgb,
                                        landmarkIndices=landmarkIndices)
            else:
                outRgb = None
            if args.fallbackLfw and outRgb is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(
                    os.path.join(args.fallbackLfw, imgObject.cls),
                    imgObject.name)
                shutil.copy(
                    deepFunneled, "{}/{}.jpg".format(
                        os.path.join(args.outputDir, imgObject.cls),
                        imgObject.name))

            if outRgb is not None:
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)

    if args.fallbackLfw:
        print('nFallbacks:', nFallbacks)
예제 #17
0
def align_images(input_path, output_path, processes=3, size=96):
    # not available in all opencv 2.4.x versions
    try:
        # since 2.4.13 opencv supports multithreading out of the box
        # this does not play well with multiprocessing of python
        # we need multiprocessing to spread the load of image alignment to all the cores
        # as python threading is limited to one core only
        cv2.setNumThreads(0)
    except AttributeError:
        pass

    openface.helper.mkdirP(output_path)
    imgs = list(iterImgs(input_path))
    m = multiprocessing.Manager()
    workers = m.Queue()
    results = m.Queue()

    # fill the worker queue before starting the processes as the workers
    # will exit in case the queue is empty
    logger.info('preparing images')
    for imgObject in imgs:
        outDir = os.path.join(output_path, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"
        rgb = imgObject.getRGB()
        workers.put((imgName, rgb))

    threads = []
    logger.info('running workers')
    for i in range(processes):
        t = AlignWorker(workers, results, size)
        t.start()
        threads.append(t)

    workers_done = 0
    while not workers_done == processes:
        logger.info("{0} images left...".format(workers.qsize()))
        while not results.empty():
            imgName, outRgb = results.get(block=True)
            # the last message a worker will send is a 'None' image to signal it is done
            # as the worker runs in another process you cannot access ressources/properties
            # of the worker directly
            if imgName is None:
                workers_done += 1
            elif outRgb is not None:
                # dlib works with RGB but opencv requires BGR order for writing images
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)
        time.sleep(0.5)
예제 #18
0
 def gen_data(self):
     face_reps = []
     labels = []
     reps_fname = "{}/reps.csv".format(self.aligned_dir)
     label_fname = "{}/labels.csv".format(self.aligned_dir)
     for imgObject in iterImgs(self.aligned_dir):
         reps = self.net.forward(imgObject.getRGB())
         face_reps.append(reps)
         labels.append((imgObject.cls, imgObject.name))
     if face_reps and labels and not self.stop_training.is_set():
         pd.DataFrame(face_reps).to_csv(reps_fname, header=False, index=False)
         pd.DataFrame(labels).to_csv(label_fname, header=False, index=False)
         logger.info("Generated label file {}".format(label_fname))
         logger.info("Generated representation file {}".format(reps_fname))
예제 #19
0
def align(args, multi):
    # Aligning now

    start = time.time()

    mkdirP(alignedDir)
    imgs = list(iterImgs(trainingDir))
    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)
    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if landmarks not in landmarkMap:
        # TODO: Avoid exceptions, find way to silently fail and skip image
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    landmarkIndices = landmarkMap[landmarks]
    align = openface.AlignDlib(dlibFacePredictor)
    nFallbacks = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(alignedDir, imgObject.cls)
        mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        # TODO: output is still PNG?
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            if args.verbose:
                print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                if args.verbose:
                    print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(imgSize, rgb,
                                     landmarkIndices=landmarkIndices,
                                     skipMulti=not multi)
                if outRgb is None and args.verbose:
                    print("  + Unable to align.")
            if outRgb is not None:
                if args.verbose:
                    print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)
예제 #20
0
 def gen_data(self):
     face_reps = []
     labels = []
     reps_fname = "{}/reps.csv".format(self.aligned_dir)
     label_fname = "{}/labels.csv".format(self.aligned_dir)
     for imgObject in iterImgs(self.aligned_dir):
         reps = self.net.forward(imgObject.getRGB())
         face_reps.append(reps)
         labels.append((imgObject.cls, imgObject.name))
     if face_reps and labels and not self.stop_training.is_set():
         pd.DataFrame(face_reps).to_csv(reps_fname,
                                        header=False,
                                        index=False)
         pd.DataFrame(labels).to_csv(label_fname, header=False, index=False)
         logger.info("Generated label file {}".format(label_fname))
         logger.info("Generated representation file {}".format(reps_fname))
예제 #21
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    if args.landmarks == 'outerEyesAndNose':
        landmarkIndices = NaiveDlib.OUTER_EYES_AND_NOSE
    elif args.landmarks == 'innerEyesAndBottomLip':
        landmarkIndices = NaiveDlib.INNER_EYES_AND_BOTTOM_LIP
    else:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    align = NaiveDlib(args.dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        outDir = os.path.join(args.outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if not os.path.isfile(imgName):
            rgb = imgObject.getRGB()
            if rgb is not None:
                print(imgName, type(rgb), rgb.shape)
                outRgb = align.alignImg('affine', args.size, rgb,
                                        landmarkIndices=landmarkIndices)
            else:
                outRgb = None
            if args.fallbackLfw and outRgb is None:
                nFallbacks += 1
                deepFunneled = "{}/{}.jpg".format(os.path.join(args.fallbackLfw,
                                                               imgObject.cls),
                                                  imgObject.name)
                shutil.copy(deepFunneled, "{}/{}.jpg".format(os.path.join(args.outputDir,
                                                                          imgObject.cls),
                                                             imgObject.name))

            if outRgb is not None:
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)

    if args.fallbackLfw:
        print('nFallbacks:', nFallbacks)
예제 #22
0
def alignMain(inputDir, outputDir, landmarks, dlibFacePredictor, size):
    openface.helper.mkdirP(outputDir)

    imgs = list(iterImgs(inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    landmarkIndices = landmarkMap[landmarks]

    align = openface.AlignDlib(dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(size,
                                     rgb,
                                     landmarkIndices=landmarkIndices)
                if outRgb is None:
                    print("  + Unable to align.")

            if outRgb is not None:
                print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)
예제 #23
0
def detectAlignImages(args):#input_dir, output_dir):
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        imgs = list(iterImgs(args.input))
        aligner = NaiveDlib(args.dlibFaceMean, args.dlibFacePredictor)
        futures = []

        for imgObject in imgs:
            future = executor.submit(alignImage, args, imgObject, aligner)
            futures.append(future)

        count_success = 0
        n = len(futures)
        #for future in concurrent.futures.as_completed(futures):
        for future in futures:
            future.result()
            count_success += 1
            if count_success % 1000 == 0:
                print("{} photo resized from {}".format(count_success, n))
예제 #24
0
def generate(args):
    openface.helper.mkdirP(args.outputDir)
    imgs = list(iterImgs(args.inputDir))

    s = ''
    i = 0
    j = 0
    label = []
    rep = []
    for imgObject in imgs:
        #print("=== {} ===".format(imgObject.path))
        y = imgObject.path.split('/')
        img = cv2.imread(imgObject.path)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img = cv2.resize(img, (96, 96), interpolation=cv2.INTER_LINEAR)
        img = np.transpose(img, (2, 0, 1))
        img = img.astype(np.float32) / 255.0
        input = torch.from_numpy(img).unsqueeze(0)
        output = model(Variable(input))
        output = output.data.numpy()
        rep.append(output[0, :])
        if s != y[2]:
            i += 1
            s = y[2]
        g = [i, imgObject.path]
        label.append(g)
        j += 1
        if j % 50 == 0:
            print("represented " + str(j) + "/" + str(len(imgs)))
    label = np.array(label)
    d = rep[1] - rep[0]
    print(np.dot(d, d))
    rep = np.array(rep)
    print(np.shape(rep))
    la = pd.DataFrame(label)
    la.to_csv(args.outputDir + "/labels1.csv",
              mode='w',
              index=False,
              header=False)
    re = pd.DataFrame(rep)
    re.to_csv(args.outputDir + "/reps1.csv",
              mode='w',
              index=False,
              header=False)
예제 #25
0
def alignMain(inputDir,outputDir,landmarks,dlibFacePredictor,size):
    openface.helper.mkdirP(outputDir)

    imgs = list(iterImgs(inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    landmarkIndices = landmarkMap[landmarks]

    align = openface.AlignDlib(dlibFacePredictor)

    nFallbacks = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                print("  + Unable to load.")
                outRgb = None
            else:
                outRgb = align.align(size, rgb, landmarkIndices=landmarkIndices)                           
                if outRgb is None:
                    print("  + Unable to align.")           

            if outRgb is not None:
                print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgName, outBgr)
예제 #26
0
    def align_images(self, input_dir):
        imgs = list(iterImgs(input_dir))
        # Shuffle so multiple versions can be run at once.
        random.shuffle(imgs)

        for imgObject in imgs:
            outDir = os.path.join(self.aligned_dir, imgObject.cls)
            if not os.path.isdir(outDir):
                os.makedirs(outDir)
            outputPrefix = os.path.join(outDir, imgObject.name)
            imgName = outputPrefix + ".png"
            if not os.path.isfile(imgName):
                logger.info("Aligning image %s", imgName)
                try:
                    self.align_image(imgObject, imgName)
                except Exception as ex:
                    logger.error(ex)
            else:
                logger.info("Skip existing aligned image %s", imgName)
예제 #27
0
    def gen_data(self):
        face_reps = []
        labels = []
        reps_fname = "{}/reps.csv".format(self.classifier_dir)
        label_fname = "{}/labels.csv".format(self.classifier_dir)

        imgs = list(iterImgs(self.aligned_dir))
        n_total = len(imgs)
        for i, imgObject in enumerate(imgs, 1):
            reps = self.net.forward(imgObject.getRGB())
            face_reps.append(reps)
            labels.append((imgObject.cls, imgObject.name))
            logger.info("{}/{}".format(i, n_total))

        if face_reps and labels:
            pd.DataFrame(face_reps).to_csv(reps_fname, header=False, index=False)
            pd.DataFrame(labels).to_csv(label_fname, header=False, index=False)
            logger.info("Generated label file {}".format(label_fname))
            logger.info("Generated representation file {}".format(reps_fname))
예제 #28
0
def align_face(im_size, inputDir, outputDir='data/aligned_dataset'):
    openface.helper.mkdirP(outputDir)

    imgs = list(iterImgs(inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)
    landmarks = 'innerEyesAndBottomLip'
    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    landmarkIndices = landmarkMap[landmarks]

    align = openface.AlignDlib(dlibFacePredictor)
    for imgObject in imgs:
        print "Aligned images: {}".format(imgObject.path)
        ext = imgObject.path.split('.')[-1]
        outDir = os.path.join(outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + "." + ext

        rgb = imgObject.getRGB()
        if rgb is None:
            outRgb = None
        else:
            outRgb = align.align(im_size,
                                 rgb,
                                 landmarkIndices=landmarkIndices,
                                 skipMulti=True)

        if outRgb is None:
            shutil.copyfile(imgObject.path, imgName)

        if outRgb is not None:
            outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
            cv2.imwrite(imgName, outBgr)
예제 #29
0
def generate(args):
    openface.helper.mkdirP(args.outputDir)
    imgs = list(iterImgs(args.inputDir))
    sock.sendall((str(len(imgs)) + '\n').encode())

    j = 0
    label = []
    rep = []
    for imgObject in imgs:
        #print("=== {} ===".format(imgObject.path))
        #y = imgObject.path.split('/')
        #print(imgObject.path)
        #print(y)
        img = cv2.imread(imgObject.path)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img = cv2.resize(img, (96, 96), interpolation=cv2.INTER_LINEAR)
        img = np.transpose(img, (2, 0, 1))
        img = img.astype(np.float32) / 255.0
        input = torch.from_numpy(img).unsqueeze(0)
        output = model(Variable(input))
        output = output.data.numpy()
        rep.append(output[0, :])
        sock.sendall('added\n'.encode())
        g = [args.name, imgObject.path]
        label.append(g)
        j += 1
        if j % 50 == 0:
            print("represented " + str(j) + "/" + str(len(imgs)))
            sock.sendall(
                ("represented " + str(j) + "/" + str(len(imgs))).encode())
    label = np.array(label)
    rep = np.array(rep)
    #print(np.shape(rep))
    la = pd.DataFrame(label)
    outfile = open(args.outputDir + "/labels.csv", mode='a')
    la.to_csv(outfile, index=False, header=False)
    outfile.close()
    re = pd.DataFrame(rep)
    outfile = open(args.outputDir + "/reps.csv", mode='a')
    re.to_csv(outfile, index=False, header=False)
    outfile.close()
예제 #30
0
    parser = argparse.ArgumentParser()
    parser.add_argument('imgDir', type=str, help="Input image directory.")
    parser.add_argument('--numImages', type=int, default=1000)
    parser.add_argument('--model',
                        type=str,
                        help="TODO",
                        default="./models/openface/nn4.v1.t7")
    parser.add_argument('--outputFile',
                        type=str,
                        help="Output file, stored in numpy serialized format.",
                        default="./unknown.npy")
    parser.add_argument('--imgDim',
                        type=int,
                        help="Default image size.",
                        default=96)
    args = parser.parse_args()

    align = NaiveDlib("models/dlib/", "shape_predictor_68_face_landmarks.dat")
    openface = openface.TorchWrap(args.model, imgDim=args.imgDim, cuda=False)

    allImgs = list(iterImgs(args.imgDir))
    imgObjs = random.sample(allImgs, args.numImages)

    reps = []
    for imgObj in imgObjs:
        rep = openface.forward(imgObj.path)
        rep = np.array(rep)
        reps.append(rep)

    np.save(args.outputFile, np.row_stack(reps))
예제 #31
0
def generate(inputDir):
    imgs = []
    for i in range(0, len(inputDir)):
        img = list(iterImgs(inputDir[i]))
        imgs += img
    sock.sendall('length\n'.encode())
    sock.sendall((str(len(imgs)) + '\n').encode())
    print(len(imgs))
    i = 0
    j = 0
    label = []
    rep = []
    for imgObject in imgs:
        #print("=== {} ===".format(imgObject.path))
        img = cv2.imread(imgObject.path)
        if img is None:
            sock.sendall('unable to load\n'.encode())
            continue
        width = img.shape[1]
        height = img.shape[0]
        check = False
        if width > 600:
            width = 600
            check = True
        if height > 600:
            height = 600
            check = True
        if check:
            img = cv2.resize(img, (width, height),
                             interpolation=cv2.INTER_LINEAR)
        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
        img = cv2.resize(img, (96, 96), interpolation=cv2.INTER_LINEAR)
        img = np.transpose(img, (2, 0, 1))
        img = img.astype(np.float32) / 255.0
        input = torch.from_numpy(img).unsqueeze(0)
        output = model(Variable(input))
        output = output.data.numpy()
        rep.append(output[0, :])
        sock.sendall('one complete\n'.encode())
        label.append(imgObject.path)
        j += 1
        if j % 50 == 0:
            print("represented " + str(j) + "/" + str(len(imgs)))
    label = np.array(label)
    images = []
    for i in range(0, len(rep)):
        temp = [label[i]]
        for j in range(i + 1, len(rep)):
            d = rep[j] - rep[i]
            d = np.dot(d, d)
            if d <= 0.02:
                temp.append(label[j])
        if len(temp) > 1:
            images.append(temp)
        sock.sendall('one complete\n'.encode())
    '''d = rep[1]-rep[0]
    print(np.shape(rep[1]))
    print(np.dot(d,d))
    rep = np.array(rep)
    print(np.shape(rep))
    la = pd.DataFrame(label)
    la.to_csv(args.outputDir+"/labels1.csv", mode='w', index=False, header=False)
    re = pd.DataFrame(rep)
    re.to_csv(args.outputDir+"/reps1.csv", mode='w', index=False, header=False)
    '''
    images = pd.DataFrame(images)
    images.to_csv("tmp.csv", mode='w', index=False, header=False)
예제 #32
0
import openface
import openface.helper
from openface.data import iterImgs

openface.helper.mkdirP(
    './aligned-images/'
)  #'/aligned-images/ --> Out Directory for storing aligned images

# Model used to predict the 68 face landmarks
dlibFacePredictor = './models/shape_predictor_68_face_landmarks.dat'

#Defining an object of AlignDlib class
align = openface.AlignDlib(dlibFacePredictor)

#Iterating among all the images
imgs = list(iterImgs('./training-images'))

#Shuffling the Images
random.shuffle(imgs)

nFallbacks = 0

landmarkMap = {
    'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
    'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
}
landmarks = 'outerEyesAndNose'  # Default, can also take 'innerEyesAndBottomLip'
landmarkIndices = landmarkMap[landmarks]

#Iterating through images one-by-one
for imgObject in imgs:
예제 #33
0
def processFaces(args, imgDirPath):
    imgs = list(iterImgs(imgDirPath))
    return imgs[0].getBGR()
예제 #34
0
def alignMain(args):
    openface.helper.mkdirP(args.outputDir)

    imgs = list(iterImgs(args.inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if args.landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    landmarkIndices = landmarkMap[args.landmarks]

    align = openface.AlignDlib(args.dlibFacePredictor)

    nFallbacks = 0
    failed_images = []
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        outDir = os.path.join(args.outputDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgName = outputPrefix + ".png"

        if os.path.isfile(imgName):
            if args.verbose:
                print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            if rgb is None:
                if args.verbose:
                    print("  + Unable to load.")
                outRgb = None
            else:
                if args.aligned:
                    outRgb = align.align(args.size,
                                         rgb,
                                         landmarkIndices=landmarkIndices,
                                         skipMulti=args.skipMulti)
                else:
                    outRgb = align.align(args.size,
                                         rgb,
                                         skipMulti=args.skipMulti)
                if outRgb is None and args.verbose:
                    print("  + Unable to align.")
            try:
                if args.fallbackLfw and outRgb is None:
                    nFallbacks += 1
                    res = cv2.resize(rgb, (args.size, args.size),
                                     interpolation=cv2.INTER_CUBIC)
                    if args.rgb:
                        outBgr = cv2.cvtColor(res, cv2.COLOR_RGB2BGR)
                        cv2.imwrite(imgName, outBgr)
                    else:
                        outBgr = cv2.cvtColor(res, cv2.COLOR_RGB2GRAY)
                        cv2.imwrite(imgName, outBgr)
            except:
                failed_images.append(imgName)
            if outRgb is not None:
                if args.verbose:
                    print("  + Writing aligned file to disk.")
                if args.rgb:
                    outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                else:
                    outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2GRAY)
                cv2.imwrite(imgName, outBgr)

    if args.fallbackLfw:
        print('nFallbacks:', nFallbacks)
        print(failed_images)
예제 #35
0
def find_landmarks(inputDir, outputDir, landmarks='outerEyesAndNose', size=96, useCNN=True, skipMulti=False, verbose=True):
    ''' Align all faces chips from folder inputDir and save output to outputDir '''

    fileDir = '/media/tunguyen/Devs/DeepLearning/FaceReg/openface'
    modelDir = os.path.join(fileDir, 'models')
    dlibModelDir = os.path.join(modelDir, 'dlib')
    # openfaceModelDir = os.path.join(modelDir, 'openface')

    dlibFacePredictor = os.path.join(
        dlibModelDir, "shape_predictor_68_face_landmarks.dat")
    # dlibFacePredictor = os.path.join(
    #     dlibModelDir, "shape_predictor_5_face_landmarks.dat")
    cnnDetectModel = os.path.join(dlibModelDir, "mmod_human_face_detector.dat")

    openface.helper.mkdirP(outputDir)

    imgs = list(iterImgs(inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP,
        '5_landmarks': openface.AlignDlib.BASIC_5_POINTS
    }
    if landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    if "predictor_5" in dlibFacePredictor:
        landmarks = '5_landmarks'

    landmarkIndices = landmarkMap[landmarks]

    align = openface.AlignDlib(dlibFacePredictor, cnnDetectModel)

    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        #outDir = os.path.join(outputDir, imgObject.cls)
        # openface.helper.mkdirP(outDir)
        outDir = outputDir
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgPath = outputPrefix + ".png"
        print('\n'+outDir)

        if os.path.isfile(imgPath):
        # if 1 == 0:
            if verbose:
                print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            #bgr_img = cv2.imread(imgObject.path)
            #rgb = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)

            if rgb is None:
                if verbose:
                    print("  + Unable to load.")
                outRgb = None
            else:
                if "predictor_68" in dlibFacePredictor:
                    outRgb, landmarks = align.align(size, rgb,
                                         landmarkIndices=landmarkIndices,
                                         skipMulti=skipMulti,
                                         useCNN=useCNN)
                else:
                    outRgb, landmarks = align.align(size, rgb,
                                         useCNN=useCNN,
                                         landmarksNum=5)

                if outRgb is None and verbose:
                    print("  + Unable to align.")

            # print(outRgb)
            if outRgb is not None and landmarks is not None:
                if verbose:
                    print("  + Writing aligned file to disk.")

                print(landmarks)

                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                # cv2.imwrite(imgPath, outBgr)

                topy = landmarks[51][1] - 10
                rightx = landmarks[54][0] + 8
                leftx = landmarks[48][0] - 8
                bottomy = landmarks[58][1]

                cv2.rectangle(outBgr,(leftx, topy),(rightx,bottomy),(0,0,0),-1)

                # for i in range(0, 68):
                #     if i in [48, 58, 54, 51]:
                #         cv2.putText(outBgr,
                #                     text=str(i),
                #                     org=(landmarks[i][0], landmarks[i][1]),
                #                     fontFace=cv2.FONT_HERSHEY_SIMPLEX,
                #                     fontScale=0.2,
                #                     color=(255, 0, 0),
                #                     thickness=1,
                #                     lineType=2)
                    
                cv2.imwrite(imgPath, outBgr)
예제 #36
0
def alignMain(args):
    openface.helper.mkdirP(args.outDir)

    check = os.listdir(os.path.normpath(args.outDir)).__contains__(args.name)
    print(check)
    sock.sendall((str(check) + "\n").encode())
    if not check:
        os.mkdir(os.path.join(args.outDir, args.name))
    else:
        shutil.rmtree(os.path.join(args.outDir, args.name))
        os.mkdir(os.path.join(args.outDir, args.name))

    print('directory created')
    sock.sendall('directory created\n'.encode())
    imgs = list(iterImgs(args.inputDir))
    sock.sendall("length\n".encode())
    sock.sendall((str(len(imgs) * 3) + "\n").encode())

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP
    }
    if args.landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(args.landmarks))

    landmarkIndices = landmarkMap[args.landmarks]

    align = openface.AlignDlib(args.dlibFacePredictor)

    nFallbacks = 0
    cnt = 0
    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        s = os.path.normpath(args.inputDir) + '/' + imgObject.name
        img = cv2.imread(s)
        if img is None:
            sock.sendall("  + Unable to load.\n".encode())
            sock.send("processed\n".encode())
        width = img.shape[1]
        height = img.shape[0]
        check = False
        if width > 600:
            width = 600
            check = True
        if height > 600:
            height = 600
            check = True
        if check:
            img = cv2.resize(img, (width, height),
                             interpolation=cv2.INTER_LINEAR)
        cv2.imwrite(
            os.path.normpath(args.inputDir) + '/1' + imgObject.name, img)
        im = Image.open(
            os.path.normpath(args.inputDir) + '/1' + imgObject.name)
        os.remove(os.path.normpath(args.inputDir) + '/1' + imgObject.name)
        contrast = ImageEnhance.Contrast(im)
        bright = ImageEnhance.Brightness(im)
        sock.sendall(("=== {} ===\n".format(imgObject.path)).encode())
        outDir = os.path.join(args.outDir, args.name)
        #outDir = os.path.join(args.outDir, imgObject.cls)
        openface.helper.mkdirP(outDir)
        outputPrefix = os.path.join(outDir,
                                    os.path.splitext(imgObject.name)[0])
        j = 0
        i = 0.7
        t = False
        for k in range(1, 3, 1):
            for l in range(1, 4):
                if k != 1 and l == 1:
                    continue
                elif t:
                    im = Image.open(s)
                    contrast = ImageEnhance.Contrast(im)
                    bright = ImageEnhance.Brightness(im)
                if k == 1:
                    imgName = outputPrefix + ".png"
                elif l == 2:
                    imgObject = contrast.enhance(i)
                    imgObject = np.array(imgObject)
                    imgName = outputPrefix + "_" + str(j) + ".png"
                elif l == 3:
                    imgObject = bright.enhance(i)
                    imgObject = np.array(imgObject)
                    imgName = outputPrefix + "_" + str(j + 1) + ".png"
                if k == 1:
                    rgb = imgObject.getRGB()
                    print(type(rgb))
                else:
                    rgb = imgObject
                cnt = cnt + 1
                sock.sendall(("cnt " + str(cnt) + "\n").encode())
                if rgb is None:
                    if args.verbose:
                        print("  + Unable to load.")
                        sock.sendall("  + Unable to load.\n".encode())
                        sock.send("processed\n".encode())
                    outRgb = None
                else:
                    outRgb = align.align(args.size,
                                         rgb,
                                         landmarkIndices=landmarkIndices,
                                         skipMulti=args.skipMulti)
                    if outRgb is None and args.verbose:
                        print("  + Unable to align." + " " + str(k) + " " +
                              str(l))
                        sock.sendall("  + Unable to align.\n".encode())
                        sock.send("processed\n".encode())
                if args.fallbackLfw and outRgb is None:
                    nFallbacks += 1
                    deepFunneled = "{}/{}.jpg".format(
                        os.path.join(args.fallbackLfw, imgObject.cls),
                        imgObject.name)
                    shutil.copy(
                        deepFunneled, "{}/{}.jpg".format(
                            os.path.join(args.outDir, imgObject.cls),
                            imgObject.name))

                if outRgb is not None:
                    if args.verbose:
                        print("  + Writing aligned file to disk.")
                        sock.sendall(
                            "  + Writing aligned file to disk.\n".encode())
                    outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                    cv2.imwrite(imgName, outBgr)
                    sock.sendall("processed\n".encode())
                    if k == 1:
                        x = outputPrefix + ".png"
                        s = x
                        t = True
                        sock.sendall("write to index\n".encode())
                        sock.sendall((x + '\n').encode())
                        break
            if k != 1:
                i += 0.3
                j += 2
        if args.fallbackLfw:
            print('nFallbacks:', nFallbacks)
예제 #37
0
def prebatchAlign(num_processes, args, multi):
    mkdirP(alignedDir)
    imgs = list(iterImgs(trainingDir))
    np.random.shuffle(imgs)
    div_arrays = divideArray(imgs, num_processes)
    return div_arrays
예제 #38
0
def align_face(inputDir, outputDir, landmarks='outerEyesAndNose', size=96, useCNN=True, skipMulti=False, verbose=True):
    ''' Align all faces chips from folder inputDir and save output to outputDir '''

    fileDir = '/media/tunguyen/Devs/DeepLearning/FaceReg/openface'
    modelDir = os.path.join(fileDir, 'models')
    dlibModelDir = os.path.join(modelDir, 'dlib')
    # openfaceModelDir = os.path.join(modelDir, 'openface')

    # dlibFacePredictor = os.path.join(
    #     dlibModelDir, "shape_predictor_68_face_landmarks.dat")
    dlibFacePredictor = os.path.join(
        dlibModelDir, "shape_predictor_5_face_landmarks.dat")
    cnnDetectModel = os.path.join(dlibModelDir, "mmod_human_face_detector.dat")

    openface.helper.mkdirP(outputDir)

    imgs = list(iterImgs(inputDir))

    # Shuffle so multiple versions can be run at once.
    random.shuffle(imgs)

    landmarkMap = {
        'outerEyesAndNose': openface.AlignDlib.OUTER_EYES_AND_NOSE,
        'innerEyesAndBottomLip': openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP,
        '5_landmarks': openface.AlignDlib.BASIC_5_POINTS
    }
    if landmarks not in landmarkMap:
        raise Exception("Landmarks unrecognized: {}".format(landmarks))

    if "predictor_5" in dlibFacePredictor:
        landmarks = '5_landmarks'

    landmarkIndices = landmarkMap[landmarks]

    align = openface.AlignDlib(dlibFacePredictor, cnnDetectModel)

    for imgObject in imgs:
        print("=== {} ===".format(imgObject.path))
        #outDir = os.path.join(outputDir, imgObject.cls)
        # openface.helper.mkdirP(outDir)
        outDir = outputDir
        outputPrefix = os.path.join(outDir, imgObject.name)
        imgPath = outputPrefix + ".png"
        print('\n'+outDir)

        if os.path.isfile(imgPath):
            if verbose:
                print("  + Already found, skipping.")
        else:
            rgb = imgObject.getRGB()
            #bgr_img = cv2.imread(imgObject.path)
            #rgb = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)

            if rgb is None:
                if verbose:
                    print("  + Unable to load.")
                outRgb = None
            else:
                if "predictor_68" in dlibFacePredictor:
                    outRgb, _ = align.align(size, rgb,
                                         landmarkIndices=landmarkIndices,
                                         skipMulti=skipMulti,
                                         useCNN=useCNN)
                else:
                    outRgb, _ = align.align(size, rgb,
                                         useCNN=useCNN,
                                         landmarksNum=5)

                if outRgb is None and verbose:
                    print("  + Unable to align.")

            # print(outRgb)
            if outRgb is not None:
                if verbose:
                    print("  + Writing aligned file to disk.")
                outBgr = cv2.cvtColor(outRgb, cv2.COLOR_RGB2BGR)
                cv2.imwrite(imgPath, outBgr)
from openface.alignment import NaiveDlib
from openface.data import iterImgs

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('imgDir', type=str, help="Input image directory.")
    parser.add_argument('--numImages', type=int, default=1000)
    parser.add_argument('--model', type=str, help="TODO",
                        default="./models/openface/nn4.v1.t7")
    parser.add_argument('--outputFile', type=str,
                        help="Output file, stored in numpy serialized format.",
                        default="./unknown.npy")
    parser.add_argument('--imgDim', type=int, help="Default image size.",
                        default=96)
    args = parser.parse_args()

    align = NaiveDlib("models/dlib/",
                      "shape_predictor_68_face_landmarks.dat")
    openface = openface.TorchWrap(args.model, imgDim=args.imgDim, cuda=False)

    allImgs = list(iterImgs(args.imgDir))
    imgObjs = random.sample(allImgs, args.numImages)

    reps = []
    for imgObj in imgObjs:
        rep = openface.forward(imgObj.path)
        rep = np.array(rep)
        reps.append(rep)

    np.save(args.outputFile, np.row_stack(reps))
예제 #40
0
    cv2.waitKey(50) & 0xFF

    if box:
        if num > 0:
            cv2.imwrite(output_path, frame)
            num = num + 1
        else:
            num = num + 1
    	continue

capture1.release()
cv2.destroyAllWindows()

os.path.split(output_path)[0]
imgs = list(iterImgs(inputDir))

random.shuffle(imgs)

for imgObject in imgs:
    print("=== {} ===".format(imgObject.path))
    outDir = os.path.join(outputDir, imgObject.cls)
    openface.helper.mkdirP(outDir)
    outputPrefix = os.path.join(outDir, imgObject.name)
    imgName = outputPrefix + ".png"

    if os.path.isfile(imgName):
        print("  + Already found, skipping.")
    else:
        rgb = imgObject.getRGB()
        if rgb is None: