def trainClassifier(self):
        """Trainng the classifier begins by aligning any images in the
        img directory and putting them into the aligned images
        directory. Each of the aligned face images are passed through the
        neural net and the resultant embeddings along with their
        labels (names of the people) are used to train the classifier
        which is saved to a pickle file as a character stream"""

        print("trainClassifier called")

        path = "/home/hatice/PycharmProjects/newOpenface/mini/cache.t7"
        try:
            os.remove(path) # Remove cache from aligned images folder
        except:
            print("Failed to remove cache.t7. Could be that it did not existed in the first place.")
            pass

        start = time.time()
        OUTER_EYES_AND_NOSE = [36, 45, 33]
        aligndlib.alignMain("img/","mini/","outerEyesAndNose","/home/hatice/PycharmProjects/newOpenface/models/dlib/shape_predictor_68_face_landmarks.dat",args.imgDim)
        print("Aligning images for training took {} seconds.".format(time.time() - start))
        done = False
        start = time.time()

        done = self.generate_representation()

        if done is True:
            print("Representation Generation (Classification Model) took {} seconds.".format(time.time() - start))
            start = time.time()
            # Train Model
            self.train("/home/hatice/PycharmProjects/newOpenface/generated-embeddings/","LinearSvm",-1)
            print("Training took {} seconds.".format(time.time() - start))
        else:
            print("Generate representation did not return True")
    def trainClassifier(self):
        """Trainng the classifier begins by aligning any images in the
        training-images directory and putting them into the aligned images
        directory. Each of the aligned face images are passed through the
        neural net and the resultant embeddings along with their
        labels (names of the people) are used to train the classifier
        which is saved to a pickle file as a character stream"""

        logger.info("trainClassifier called")

        path = fileDir + "/aligned-images/cache.t7"
        try:
            os.remove(path) # Remove cache from aligned images folder
        except:
            logger.info("Failed to remove cache.t7. Could be that it did not existed in the first place.")
            pass

        start = time.time()
        aligndlib.alignMain("training-images/","aligned-images/","outerEyesAndNose",args.dlibFacePredictor,args.imgDim)
        logger.info("Aligning images for training took {} seconds.".format(time.time() - start))
        done = False
        start = time.time()

        done = self.generate_representation()

        if done is True:
            logger.info("Representation Generation (Classification Model) took {} seconds.".format(time.time() - start))
            start = time.time()
            # Train Model
            self.train("generated-embeddings/","LinearSvm",-1)
            logger.info("Training took {} seconds.".format(time.time() - start))
        else:
            logger.info("Generate representation did not return True")
Пример #3
0
    def trainClassifier(self):
        """Trainng the classifier begins by aligning any images in the 
		training-images directory and putting them into the aligned images
		directory. Each of the aligned face images are passed through the 
		neural net and the resultant embeddings along with their
		labels (names of the people) are used to train the classifier
		which is saved to a pickle file as a character stream"""

        path = fileDir + "/aligned-images/cache.t7"
        try:
            os.remove(path)  # Remove cache from aligned images folder
        except:
            print "Tried to remove cache.t7"
            pass

        start = time.time()
        aligndlib.alignMain("training-images/", "aligned-images/",
                            "outerEyesAndNose", args.dlibFacePredictor,
                            args.imgDim)
        print("\nAligning images took {} seconds.".format(time.time() - start))
        done = False
        start = time.time()

        done = self.generate_representation()

        if done is True:
            print(
                "Representation Generation (Classification Model) took {} seconds."
                .format(time.time() - start))
            start = time.time()
            # Train Model
            self.train("generated-embeddings/", "LinearSvm", -1)
            print("Training took {} seconds.".format(time.time() - start))
        else:
            print("Generate representation did not return True")
    def trainClassifier(self, database):
        """Trainng the classifier begins by aligning any images in the
        training-images directory and putting them into the aligned images
        directory. Each of the aligned face images are passed through the
        neural net and the resultant embeddings along with their
        labels (names of the people) are used to train the classifier
        which is saved to a pickle file as a character stream"""

        logger.info("trainClassifier called")

        path = fileDir + "/aligned-images/cache.t7"
        try:
            os.remove(path)  # Remove cache from aligned images folder
        except:
            logger.info(
                "Failed to remove cache.t7. Could be that it did not existed in the first place."
            )
            pass

        start = time.time()
        aligndlib.alignMain("training-images/", "aligned-images/",
                            "outerEyesAndNose", args.dlibFacePredictor,
                            args.imgDim)
        logger.info("Aligning images for training took {} seconds.".format(
            time.time() - start))
        done = False
        start = time.time()

        done = self.generate_representation()

        if done is True:
            logger.info(
                "Representation Generation (Classification Model) took {} seconds."
                .format(time.time() - start))
            start = time.time()
            # Train Model
            o_fname, n_fname, o_fname2, n_fname2 = self.train(
                "generated-embeddings/", -1, database)
            logger.info("Training took {} seconds.".format(time.time() -
                                                           start))
        else:
            logger.info("Generate representation did not return True")

        return o_fname, n_fname, o_fname2, n_fname2, done
    def trainClassifier(self):

        self.trainingEvent.clear(
        )  #event used to hault face_processing threads to ensure no threads try access .pkl file while it is being updated

        path = self.fileDir + "/aligned-images/cache.t7"
        try:
            os.remove(path)  # remove cache from aligned images folder
        except:
            print "Tried to remove cache.t7"
            pass

        start = time.time()
        aligndlib.alignMain("training-images/", "aligned-images/",
                            "outerEyesAndNose", self.args.dlibFacePredictor,
                            self.args.imgDim)
        print("\nAligning images took {} seconds.".format(time.time() - start))

        done = False
        start = time.time()

        done = self.generate_representation()

        if done is True:
            print(
                "Representation Generation (Classification Model) took {} seconds."
                .format(time.time() - start))
            start = time.time()
            #Train Model
            self.train("generated-embeddings/", "LinearSvm", -1)
            print("Training took {} seconds.".format(time.time() - start))
        else:
            print("Generate representation did not return True")

        self.trainingEvent.set()  #threads can continue processing

        return True
                                                                   clf_final)])

        self.clf.fit(embeddings, labelsNum)  #link embeddings to labels

        fName = "{}classifier.pkl".format(workDir)
        print("Saving classifier to '{}'".format(fName))
        with open(fName, 'w') as f:
            pickle.dump(
                (self.le, self.clf), f
            )  # Creates character stream and writes to file to use for recognition

    #
    # def getSquaredl2Distance(self,rep1,rep2):
    #     """Returns number between 0-4, Openface calculated the mean between
    #     similar faces is 0.99 i.e. returns less than 0.99 if reps both belong
    #     to the same person"""
    #
    #     d = rep1 - rep2


if __name__ == "__main__":

    a = FaceRecogniser()
    # a.trainClassifier()
    aligndlib.alignMain(
        "input/", "output/", "innerEyesAndBottomLip",
        "/home/hatice/PycharmProjects/newOpenface/models/dlib/shape_predictor_68_face_landmarks.dat",
        args.imgDim)

    a.recognize_face("output/bugraabi.png")