def test_image_recognize_face():
    img = Factory.Image("lenna")
    recognizer = FaceRecognizer()
    faces = img.find_haar_features("face.xml")
    face = faces[0].crop()
    recognizer.load(os.path.join(DATA_DIR, "FaceRecognizer/GenderData.xml"))
    assert_equals(face.recognize_face(recognizer)[0], 0)

    # invalid recognizer
    assert_is_none(img.recognize_face(2))
def test_facerecognizer_load():
    f = FaceRecognizer()
    trained = f.train(
        csvfile=os.path.join(DATA_DIR, "test/standard/test_facerecognizer_train_data.csv"),
        delimiter=",")

    images3 = [os.path.join(DATA_DIR, "sampleimages/ff1.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff5.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm3.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm4.jpg")]

    imgset3 = []

    for img in images3:
        imgset3.append(Factory.Image(img))

    label = []
    for img in imgset3:
        name, confidence = f.predict(img)
        label.append(name)

    assert_list_equal(['female', 'female', 'male', 'male'], label)

    fr1 = FaceRecognizer()
    trained = fr1.load("no_such_file.xml")
    assert_equal(trained, False)

    prediction = fr1.predict(imgset3[0])
    assert_equal(prediction, None)
def test_facerecognizer_save():
    images1 = [os.path.join(DATA_DIR, "sampleimages/ff1.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff2.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff3.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff4.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff5.jpg")]

    images2 = [os.path.join(DATA_DIR, "sampleimages/fm1.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm2.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm3.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm4.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm5.jpg")]

    imgset1 = []
    imgset2 = []

    for img in images1:
        imgset1.append(Factory.Image(img))
    label1 = ["female"] * len(imgset1)

    for img in images2:
        imgset2.append(Factory.Image(img))
    label2 = ["male"] * len(imgset2)

    imgset = imgset1 + imgset2
    labels = label1 + label2

    f = FaceRecognizer()
    trained = f.train(imgset, labels)

    filename = os.path.join(tempfile.gettempdir(), "gendertrain.xml")
    if (trained):
        saved = f.save(filename)
        assert_equal(saved, True)

        if not os.path.exists(os.path.abspath(filename)):
            assert False

        os.remove(filename)
def test_image_find_and_recognize_faces():
    img = Factory.Image("lenna")
    recognizer = FaceRecognizer()
    recognizer.load(os.path.join(DATA_DIR, "FaceRecognizer/GenderData.xml"))
    assert_equals(img.find_and_recognize_faces(recognizer, "face.xml")[0][1],
                  0)
def test_facerecognizer_train():
    images1 = [os.path.join(DATA_DIR, "sampleimages/ff1.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff2.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff3.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff4.jpg"),
               os.path.join(DATA_DIR, "sampleimages/ff5.jpg")]

    images2 = [os.path.join(DATA_DIR, "sampleimages/fm1.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm2.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm3.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm4.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fm5.jpg")]

    images3 = [os.path.join(DATA_DIR, "sampleimages/fi1.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fi2.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fi3.jpg"),
               os.path.join(DATA_DIR, "sampleimages/fi4.jpg")]

    imgset1 = []
    imgset2 = []
    imgset3 = []
    label = []

    for img in images1:
        imgset1.append(Factory.Image(img))
    label1 = ["female"] * len(imgset1)

    for img in images2:
        imgset2.append(Factory.Image(img))
    label2 = ["male"] * len(imgset2)

    imgset = imgset1 + imgset2
    labels = label1 + label2
    imgset[4] = imgset[4].resize(400, 400)

    for img in images3:
        imgset3.append(Factory.Image(img))

    f = FaceRecognizer()
    trained = f.train(
        csvfile=os.path.join(DATA_DIR, "test/standard/test_facerecognizer_train_data.csv"),
        delimiter=",")

    for img in imgset3:
        name, confidence = f.predict(img)
        label.append(name)

    assert_equal(trained, True)
    assert_list_equal(["male", "male", "female", "female"], label)

    fr1 = FaceRecognizer()
    trained = fr1.train(csvfile="no_such_file.csv")
    assert_equal(trained, False)

    fr2 = FaceRecognizer()
    trained = fr2.train(imgset, labels)
    assert_equal(trained, True)

    label = []
    for img in imgset3:
        name, confidence = fr2.predict(img)
        label.append(name)
    assert_list_equal(["male", "male", "female", "female"], label)

    fr3 = FaceRecognizer()
    trained = fr3.train(imgset1, label1)
    assert_equal(trained, False)

    fr4 = FaceRecognizer()
    trained = fr4.train(imgset, label2)
    assert_equal(trained, False)

    prediction = fr4.predict(imgset3[0])
    assert_equal(prediction, None)