Ejemplo n.º 1
0
def scan_known_people(train_dir):
    known_names = []
    known_face_encodings = []
    print("CNN image pre-process starting.........")
    # Loop through each person in the training set
    for class_dir in os.listdir(train_dir):
        if not os.path.isdir(os.path.join(train_dir, class_dir)):
            continue

        # Loop through each training image for the current person
        for file in image_files_in_folder(os.path.join(train_dir, class_dir)):
            basename = os.path.splitext(os.path.basename(file))[0]
            print('{} filename'.format(basename))
            if (len(basename.split("_")) > 1):
                member_firstname = basename.split('_')[0]
                member_lastname = basename.split('_')[1]
            else:
                member_firstname = basename
                member_lastname = ""

            face_image = face_recognition.load_image_file(file)
            face_locations = face_recognition.face_locations(
                face_image, number_of_times_to_upsample=2, model='cnn')
            encodings = face_recognition.face_encodings(
                face_image,
                known_face_locations=face_locations,
                num_jitters=30)
            if len(encodings) > 1:
                click.echo(
                    "WARNING: More than one face found in {}. Only considering the first face."
                    .format(file))

            if len(encodings) == 0:
                click.echo(
                    "WARNING: No faces found in {}. Ignoring file.".format(
                        file))
            else:
                known_names.append(basename)
                # known_face_encodings.append(encodings[0])
                insertmember(member_firstname, member_lastname, encodings[0])
                click.echo(f'Member {basename} has been added.', color='green')
Ejemplo n.º 2
0
def train(train_dir,
          model_save_path=None,
          n_neighbors=None,
          knn_algo='auto',
          verbose=True):
    """
    Trains a k-nearest neighbors classifier for face recognition.

    :param train_dir: directory that contains a sub-directory for each known person, with its name.

     (View in source code to see train_dir example tree structure)

     Structure:
        <train_dir>/
        ├── <person1>/
        │   ├── <somename1>.jpeg
        │   ├── <somename2>.jpeg
        │   ├── ...
        ├── <person2>/
        │   ├── <somename1>.jpeg
        │   └── <somename2>.jpeg
        └── ...

    :param model_save_path: (optional) path to save model on disk
    :param n_neighbors: (optional) number of neighbors to weigh in classification. Chosen automatically if not specified
    :param knn_algo: (optional) underlying data structure to support knn.default is ball_tree
    :param verbose: verbosity of training
    :return: returns knn classifier that was trained on the given data.
    """
    X = []
    y = []

    # Loop through each person in the training set
    for class_dir in os.listdir(train_dir):
        if not os.path.isdir(os.path.join(train_dir, class_dir)):
            continue

        # Loop through each training image for the current person
        for img_path in image_files_in_folder(
                os.path.join(train_dir, class_dir)):
            image = face_recognition.load_image_file(img_path)
            face_bounding_boxes = face_recognition.face_locations(image)

            if len(face_bounding_boxes) != 1:
                # If there are no people (or too many people) in a training image, skip the image.
                if verbose:
                    print("Image {} not suitable for training: {}".format(
                        img_path,
                        "Didn't find a face" if len(face_bounding_boxes) < 1
                        else "Found more than one face"))
            else:
                # Add face encoding for current image to the training set
                X.append(
                    face_recognition.face_encodings(
                        image, known_face_locations=face_bounding_boxes)[0])
                y.append(class_dir)

    # Determine how many neighbors to use for weighting in the KNN classifier
    if n_neighbors is None:
        n_neighbors = int(round(math.sqrt(len(X))))
        if verbose:
            print("Chose n_neighbors automatically:", n_neighbors)

    # Create and train the KNN classifier
    knn_clf = neighbors.KNeighborsClassifier(n_neighbors=n_neighbors,
                                             algorithm=knn_algo,
                                             weights='uniform')
    knn_clf.fit(X, y)

    # Save the trained KNN classifier
    if model_save_path is not None:
        with open(model_save_path, 'wb') as f:
            pickle.dump(knn_clf, f)

    return knn_clf
# loop over the image paths
for (i, imagePath) in enumerate(imagePaths):
    # load the input image and convert it from RGB (OpenCV ordering)
    # to dlib ordering (RGB)
    print("[INFO] processing image {}/{}".format(i + 1, len(imagePaths)))
    print(imagePath)
    image = cv2.imread(imagePath)
    rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # detect the (x, y)-coordinates of the bounding boxes
    # corresponding to each face in the input image
    boxes = face_recognition.face_locations(rgb,
                                            model=args["detection_method"])

    # compute the facial embedding for the face
    encodings = face_recognition.face_encodings(rgb, boxes)

    # build a dictionary of the image path, bounding box location,
    # and facial encodings for the current image
    d = [{
        "imagePath": imagePath,
        "loc": box,
        "encoding": enc
    } for (box, enc) in zip(boxes, encodings)]
    data.extend(d)

# dump the facial encodings data to disk
print("[INFO] serializing encodings...")
f = open(args["encodings"], "wb")
f.write(pickle.dumps(data))
f.close()
Ejemplo n.º 4
0
def imageparser():
    #Initialize some variables
    face_locations = []
    face_encodings = []
    #face_names = []
    req = request
    # convert string of image data to uint8
    nparr = np.fromstring(req.data, np.uint8)
    # decode image
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

    # Find all the faces and face encodings in the current frame of video
    face_locations = face_recognition.face_locations(
        img, number_of_times_to_upsample=2)
    face_encodings = face_recognition.face_encodings(img,
                                                     face_locations,
                                                     num_jitters=10)

    face_names = []
    # if the face is unknown look for the CNN model comparision
    app.logger.info("Checking the CNN Model")
    face_names, unidentified_face_encodings = mlearning.cnn_compare(
        face_encodings)
    # for x in face_new_names:
    #     face_names.append(x)
    app.logger.info(
        "Length of face_names : {}, unidentified names : {}".format(
            len(face_names), len(unidentified_face_encodings)))
    if (len(unidentified_face_encodings) >= 1):
        #app.logger.info("face encodings {}".format(unidentified_face_encodings))
        # # ------------------------------------------ KNN Model Prediction ------------------------------
        # # Note: You can pass in either a classifier file name or a classifier model instance
        predictions = mlearning.predict_without_location(
            len(unidentified_face_encodings),
            unidentified_face_encodings,
            knn_clf=knn_clf,
            model_path=None)
        app.logger.info(predictions)
        for name in predictions:
            app.logger.debug("- Found {} ".format(name))
            if (len(str(name).split("_")) > 1):
                app.logger.debug("Name: " + str(name).split("_")[1])
                face_names.append(str(name).split("_")[1])
            else:
                newfaceid = str(uuid.uuid4())
                # using now() to get current time
                current_time = datetime.datetime.now()
                cv2.imwrite(
                    './unidentified_images/' + newfaceid + '_' +
                    str(current_time) + '.jpg', img)

        app.logger.debug('Found name - {}'.format(",".join(
            str(x) for x in face_names)))

    # # build a response dict to send back to client
    response = {
        'message': 'image received. size={}x{}'.format(img.shape[1],
                                                       img.shape[0]),
        'details':
        'Found name - {}'.format(",".join(str(x) for x in face_names))
    }

    # encode response using jsonpickle
    response_pickled = jsonpickle.encode(response)

    return Response(response=response_pickled,
                    status=200,
                    mimetype="application/json")