Ejemplo n.º 1
0
def facestrain():
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    image_dir = os.path.join(BASE_DIR, "images")

    print("[INFO] quantifying faces...")
    imagePaths = list(paths.list_images(image_dir))

    knownEncodings = []
    knownNames = []

    for (i, imagePath) in enumerate(imagePaths):
        print("[INFO] processing image {}/{}".format(i + 1, len(imagePaths)))
        name = imagePath.split(os.path.sep)[-2]
        print(name)
        stream = open(imagePath, "rb")
        bytes = bytearray(stream.read())
        numpyarray = numpy.asarray(bytes, dtype=numpy.uint8)
        image = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
        rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        face, confidence = facedetect.detect_face(image)
        boxes = []
        for idx, f in enumerate(face):
            cv2.rectangle(image, (f[0], f[1]), (f[2], f[3]), (255, 0, 0), 2)
            boxes = [(f[1], f[2], f[3], f[0])]
        encodings = facedetect.face_encodings(rgb, boxes)
        for encoding in encodings:
            knownEncodings.append(encoding)
            knownNames.append(name)
        print("[INFO] serializing encodings...")
        data = {"encodings": knownEncodings, "names": knownNames}
        f = open("encodings.pickle", "wb")
        f.write(pickle.dumps(data))
        f.close()
Ejemplo n.º 2
0
def detect_face(frameCount):
    global vs, outputFrame, lock, dat
    data = pickle.loads(open('encodings.pickle', "rb").read())
    total = 0
    while True:
        if dat == 'stop':
            continue
        frame = vs.read()
        if total > frameCount:
            face, confidence = facedetect.detect_face(frame)
            rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            boxes = []
            names = []
            for idx, f in enumerate(face):
                box = (f[1], f[2], f[3], f[0])
                boxes.append(box)
            encodings = facedetect.face_encodings(rgb, boxes)

            for encoding in encodings:
                matches = facedetect.compare_faces(data["encodings"],
                                                   encoding,
                                                   tolerance=0.35)
                name = "Unknown"
                face_distances = facedetect.face_distance(
                    data["encodings"], encoding)
                best_match_index = np.argmin(face_distances)
                if matches[best_match_index]:
                    name = data["names"][best_match_index]
                names.append(name)
            for ((top, right, bottom, left), name) in zip(boxes, names):
                cv2.rectangle(frame, (left - 20, top - 20),
                              (right + 20, bottom + 20), (255, 0, 0), 2)

                # Draw a label with a name below the face
                cv2.rectangle(frame, (left - 20, bottom - 15),
                              (right + 20, bottom + 20), (255, 0, 0),
                              cv2.FILLED)
                font = cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(frame, name, (left - 20, bottom + 15), font, 1.0,
                            (255, 255, 255), 2)
        print(frame)
        total += 1
        with lock:
            outputFrame = frame.copy()
Ejemplo n.º 3
0
print("[INFO] quantifying faces...")
imagePaths = list(paths.list_images(image_dir))

knownEncodings = []
knownNames = []

for (i, imagePath) in enumerate(imagePaths):
    print("[INFO] processing image {}/{}".format(i+1, len(imagePaths)))
    name = imagePath.split(os.path.sep)[-2]
    print(name)
    stream = open(imagePath, "rb")
    bytes = bytearray(stream.read())
    numpyarray = numpy.asarray(bytes, dtype=numpy.uint8)
    image = cv2.imdecode(numpyarray, cv2.IMREAD_UNCHANGED)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    face, confidence = facedetect.detect_face(image)
    boxes = []
    for idx, f in enumerate(face):
        cv2.rectangle(image, (f[0], f[1]), (f[2], f[3]), (255, 0, 0), 2)
        boxes = [(f[1], f[2], f[3], f[0])]
    encodings = facedetect.face_encodings(rgb, boxes)
    for encoding in encodings:
        knownEncodings.append(encoding)
        knownNames.append(name)
    print("[INFO] serializing encodings...")
    data = {"encodings": knownEncodings, "names": knownNames}
    f = open("encodings.pickle", "wb")
    f.write(pickle.dumps(data))
    f.close()