Ejemplo n.º 1
0
def train(tagged):
    """
    Trains an SVM classifier based on the training data passed.

    Mostly based on http://dlib.net/svm_binary_classifier.py.html.

    :param tagged: list of TaggedFace to train on
    :return: dlib.svm
    """
    x = dlib.vectors()  # will carry the facial encodings
    y = dlib.array()  # will carry the gender label
    print("Preparing dataset...")
    total = len(tagged)
    for i, t in enumerate(tagged):
        print(f"\rEncoding {t.path} ({i + 1}/{total})...", end="")
        faces = encode(t.img)
        x.append(dlib.vector(faces[0]))
        y.append(t.tag)
        img = t.img
        for _ in range(5):
            faces = encode(img)
            if not faces:
                break
            x.append(dlib.vector(faces[0]))
            y.append(t.tag)
            img = cv2.resize(img, None, fx=0.7, fy=0.7)

    print("Training SVM...")
    trainer = dlib.svm_c_trainer_radial_basis()
    #trainer.be_verbose()
    trainer.set_c(10)
    model = trainer.train(x, y)
    with open(PATH_SVMFILE, "wb") as filehandle:
        pickle.dump(model, filehandle)
    return None
Ejemplo n.º 2
0
def read_and_find(path):
    """
    Reads an image from a path, and locates and encodes any face on it.

    :param path: path to an image
    :return: tuple of (cv2/np.ndarray, list of encodings, list of locations)
        or empty tuple if no face was found
    """
    print(f"Reading {path}...")
    img = cv2.imread(path)
    if img is None:
        return tuple()
    locations = detect(img)
    if not (locations):
        return tuple()
    if C_LOAD_ATLAS:  # we don't have to do the encodings in this case
        return img, [], locations, path
    return img, encode(img, locations=locations), locations, path
Ejemplo n.º 3
0
from phantom.faces import compare, encode, detect

known_faces = {
    "Bruno": "c:/test/phantom/tests/img4.jpg",
    "Luciano": "c:/test/phantom/tests/luciano2.jpg",
    "Fer": "c:/test/phantom/tests/fer.jpg",
    "Ana": "c:/test/phantom/tests/ana.jpg",
    "Santi": "c:/test/phantom/tests/santi.jpg",
}

for name, impath in known_faces.items():
    img = cv2.imread(impath)
    if img is None:
        known_faces[name] = None
        continue
    known_faces[name] = encode(img)
    

video = cv2.VideoCapture(0)

while True:
    check, frame = video.read()
    faces = detect(frame, upsample=0)
    if faces:
        encodings = encode(frame, locations=faces)
        for i, e in enumerate(encodings):
            for k, v in known_faces.items():
                if compare(e, v) <= 0.6:
                    left, top, right, bottom = faces[i]
                    x = int (left * 0.5 + right * 0.5)
                    y = top