コード例 #1
0
def TrainManyTestOne():
    """
    Training:
    Train with many images of a subject.
    Many subjects will be in  model.
    Testing:
    Test with one image of each subject.
    :return:
    """
    os.remove(learnedData)
    faceRecognizer = FaceRecognizer.Recognizer(learnedData)

    train_images, test_images = [], []
    for subject in os.listdir(path):
        images = os.listdir(posixpath.join(path, subject))
        mappedId = int(subject[-2:])
        train_images = images[1:]
        test_images.append(
            posixpath.join(posixpath.join(path, subject), images[0]))

        for image in train_images:
            print "train", image, mappedId
            faces = getFaces(
                posixpath.join(posixpath.join(path, subject), image))
            faceRecognizer.update(faces, [mappedId])

    print "Prediction starts: "
    for image in test_images:
        print image
        faces = getFaces(image)
        faceRecognizer.predict(faces)
コード例 #2
0
def TrainAllSubjectsExceptOneAndPredictThatSubject():
    os.remove(learnedData)
    faceRecognizer = FaceRecognizer.Recognizer(learnedData)

    subjects = os.listdir(path)
    train_subjects = subjects[:-1]
    test_subjec = subjects[-1]

    print train_subjects
    print test_subjec

    for subject in train_subjects:
        # print subject
        mappedId = int(subject[-2:])
        for image in os.listdir(posixpath.join(path, subject)):
            print image
            faces = getFaces(
                posixpath.join(posixpath.join(path, subject), image))
            faceRecognizer.update(faces, [mappedId])

    print "Prediction of images of ", test_subjec, "starts"
    for image in os.listdir(posixpath.join(path, test_subjec)):
        print image
        faces = getFaces(
            posixpath.join(posixpath.join(path, test_subjec), image))
        faceRecognizer.predict(faces)
コード例 #3
0
def TrainAllTestAll():
    os.remove(learnedData)
    faceRecognizer = FaceRecognizer.Recognizer(learnedData)
    # Training starts
    subjectId = 1
    for subject in os.listdir(path):
        # print subject
        for images in os.listdir(posixpath.join(path, subject)):
            # print images
            mappedIds = [subjectId]
            faces = getFaces(
                posixpath.join(posixpath.join(path, subject), images))
            faceRecognizer.update(faces, mappedIds)

        subjectId += 1

    # Testing starts
    for subject in os.listdir(path):
        # print subject
        for images in os.listdir(posixpath.join(path, subject)):
            faces = getFaces(
                posixpath.join(posixpath.join(path, subject), images))
            faceRecognizer.predict(faces)