Example #1
0
def main(samples_dir, test_dir, output_dir):
    face_descriptors, class_names = load_samples_descriptors(samples_dir)
    save2pickle(face_descriptors, class_names, "wg_colleagues.pkl")
    # face_descriptors, class_names = load_from_pickle('wg_colleagues.pkl')
    print(face_descriptors[0])
    print("len face_descriptors: {}".format(len(face_descriptors)))
    print("len class_names: {}".format(len(class_names)))
    print("class_names: {}".format(class_names))
    print("total class: {}".format(len(set(class_names))))
    image_files = images_in_dir(test_dir)

    for im_f in image_files:
        im = cv2.imread(im_f)
        faces = detect_faces(im)
        start = time.time()
        for face in faces:
            descriptor = compute_face_descriptor(im, face)
            idx, distance = closest_one(face_descriptors, descriptor)
            if distance > 0.4:
                label = 'unknown'
            else:
                label = class_names[idx]
            subdir = _subdir(output_dir, label)

            images_count = len(glob.glob('{}/*.jpg'.format(subdir)))

            f_name = '{}_{}.jpg'.format(label, '{0:04d}'.format(images_count))

            print('{}: {}, of distance :{} '.format(im_f, f_name, distance))

            output_path = os.path.join(subdir, f_name)
            cv2.imwrite(output_path, im)

        print('time : ', time.time() - start)
Example #2
0
def test_load_samples_descriptors():
    face_descriptors, class_names = load_samples_descriptors(images_dir)
    assert len(face_descriptors) == 3
    assert len(class_names) == 3
    assert class_names[0] == "Aaron_Eckhart"
    assert class_names[1] == "Blythe_Hartley"
    assert class_names[2] == "Blythe_Hartley"
Example #3
0
def main(samples_dir, test_dir, output_dir):
    face_descriptors, class_names = load_samples_descriptors(samples_dir)
    # save2pickle(face_descriptors, class_names, "wg_colleagues.pkl")
    # face_descriptors, class_names = load_from_pickle('wg_colleagues.pkl')

    image_files = images_in_dir(test_dir)
    labels, names = _labeled(class_names)
    classifier = SVMClassifier()
    classifier.train(face_descriptors, labels, names)

    for im_f in image_files:
        f_name = filename(im_f)
        im = cv2.imread(im_f)
        faces = detect_faces(im)
        start = time.time()
        print('{} faces detected'.format(len(faces)))
        for face in faces:
            descriptor = compute_face_descriptor(im, face)
            results = classifier.predict([descriptor])

            for r in results:
                label = '{}'.format(r)
                print('label:', label)

                subdir = _subdir(output_dir, label)

                images_count = len(glob.glob('{}/*.jpg'.format(subdir)))

                f_name = '{}_{}.jpg'.format(label,
                                            '{0:04d}'.format(images_count))

                output_path = os.path.join(subdir, f_name)
                cv2.imwrite(output_path, im)

        print('{} done, time spent: {}'.format(f_name, time.time() - start))
Example #4
0
def main(samples_dir, test_images_dir, output_dir):
    face_descriptors, class_names = load_samples_descriptors(samples_dir)
    # save2pickle(face_descriptors, class_names, 'wg_merged.pkl')
    # face_descriptors, class_names = load_from_pickle('wg_merged.pkl')
    print(class_names)
    labels, names = _labeled(class_names)
    classifier = SVMClassifier()

    print([names[i] for i in labels])

    classifier.train(face_descriptors, labels, names)

    # classifier.load("classifier_2018-05-15 13:30:06.213832.pkl")
    image_files = images_in_dir(test_images_dir)
    for im_f in image_files:
        output_path = os.path.join(output_dir, filename(im_f))
        im = cv2.imread(im_f)
        faces = detect_faces(im)
        for face in faces:
            descriptor = compute_face_descriptor(im, face)
            results = classifier.predict([descriptor])
            for r in results:
                txt = '{}'.format(r)
                put_text(im, txt, font_face=cv2.FONT_HERSHEY_SIMPLEX)

                print('{}: {} '.format(im_f, r))
        cv2.imwrite(output_path, im)
Example #5
0
def test_save_and_load():
    face_descriptors, class_names = load_samples_descriptors(images_dir)
    print(face_descriptors)
    print(class_names)
    save2pickle(face_descriptors, class_names, os.path.join(BASE_DIR, "descriptors.pkl"))
    fd, names = load_from_pickle("descriptors.pkl")
    print(fd)
    print(names)
    assert np.array_equal(fd, face_descriptors) is True
    assert names == class_names
def main(samples_dir, validate_data_dir, output_dir):
    classifier_file = "classifier_wgers_160_2018-06-14.pkl"
    classifier = SVMClassifier(probability=False)
    if classifier_file is None:
        face_descriptors, class_names = load_samples_descriptors(samples_dir)
        print("len face_descriptors: {}".format(len(face_descriptors)))
        print("len class_names: {}".format(len(class_names)))
        print("class_names: {}".format(class_names))
        print("class_names: {}".format(class_names))
        print("total class: {}".format(len(set(class_names))))
        labels, names = _labeled(class_names)
        print([names[i] for i in labels])
        classifier.train(face_descriptors, labels, names)
    else:
        classifier.load(classifier_file)

    hierarchy = read_from_hierarchy(validate_data_dir)

    total = 0
    accurate = 0
    time_spent = 0
    for k in hierarchy.keys():
        image_files = images_in_dir(hierarchy[k])
        total += len(image_files)
        for im_f in image_files:
            im = cv2.imread(im_f)

            # faces = detect_faces(im)
            height, width = im.shape[:2]
            faces = rect_2_dlib_rectangles((0, 0), (width, height))

            a, t = _evaluate_with_classifier(im, faces, classifier, k, output_dir)
            accurate += a
            time_spent += t
        print("-------------------------------------Accuracy is: {} ".format(accurate / total))
    print('==================================================================')
    print('Accurate is {}, total: {}'.format(accurate, total))
    print('Total time spent is: {}'.format(time_spent))
    print("Average time spent is: {}".format(time_spent / total))
    print("Average Accuracy is: {} ".format(accurate / total))