def test_raw_face_landmarks(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        face_landmarks = api._raw_face_landmarks(img)
        example_landmark = face_landmarks[0].parts()[10]

        self.assertEqual(len(face_landmarks), 1)
        self.assertEqual(face_landmarks[0].num_parts, 68)
        self.assertEqual((example_landmark.x, example_landmark.y), (552, 399))
    def test_raw_face_landmarks(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        face_landmarks = api._raw_face_landmarks(img)
        example_landmark = face_landmarks[0].parts()[10]

        self.assertEqual(len(face_landmarks), 1)
        self.assertEqual(face_landmarks[0].num_parts, 68)
        self.assertEqual((example_landmark.x, example_landmark.y), (552, 399))
Exemple #3
0
    def test_raw_face_landmarks(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), "test_images", "obama.jpg"))
        face_landmarks = api._raw_face_landmarks(img)
        example_landmark = face_landmarks[0].parts()[10]

        assert len(face_landmarks) == 1
        assert face_landmarks[0].num_parts == 68
        assert (example_landmark.x, example_landmark.y) == (552, 399)
Exemple #4
0
    def test_raw_face_landmarks(self):
        img = api.load_image_file(
            os.path.join(os.path.dirname(__file__), "test_images",
                         "obama.jpg"))
        face_landmarks = api._raw_face_landmarks(img)
        example_landmark = face_landmarks[0].parts()[10]

        assert len(face_landmarks) == 1
        assert face_landmarks[0].num_parts == 68
        assert (example_landmark.x, example_landmark.y) == (552, 399)
def get_faces_landmarks(frame, faces_locations):
    '''
    get landmarks for each detected face
        parameters :
            frame : (numpy 2d array)            rgb opencv / numpy fram/image.
            faces_locations: (numpy array/list) contains each detected faces bounding boxes in format (x1, y1, x2, y2) or (left, top, right, bottom), where (x1, y1) is the upper left corner of the box, (x2, y2) is the lower right corner of the box. 
        
        returns :
            faces_landmarks : (list of dlib full_object_detection object) that contains the facial landmarks for each face.

    '''
    faces_landmarks = _raw_face_landmarks(
        frame, faces_locations, model="small")  # face_recogintion.api lib
    return faces_landmarks
def CropFacesFunction(searchterm):
    imagefolder = os.path.join(os.getcwd(), "Downloaded_images", searchterm)
    targetfolder = os.path.join(imagefolder, "Cropped_faces")

    if not os.path.exists(targetfolder):
        os.mkdir(targetfolder)

    for image_path in os.listdir(imagefolder):
        try:
            image = face_recognition.load_image_file(
                os.path.join(imagefolder, image_path))
            face_locations = face_recognition.face_locations(image)
            img = cv2.imread(os.path.join(imagefolder, image_path))
            height, width, channels = img.shape

            for index in range(0, len(face_locations)):
                x = face_locations[index][0] - 50
                y = face_locations[index][1] + 30
                w = face_locations[index][2] + 30
                h = face_locations[index][3] - 30
                if x < 0:
                    x = 0
                if w > height:
                    w = height
                if h < 0:
                    h = 0
                if y > width:
                    y = width

                sub_face = img[x:w, h:y]

                targetfile = targetfolder + "\\" + image_path[:-4] + "_" + str(
                    index) + ".jpg"
                cv2.imwrite(targetfile, sub_face)
                try:
                    face_landmarks = _raw_face_landmarks(sub_face)
                    landmarks_as_tuples = [[
                        (p.x, p.y) for p in face_landmarks.parts()
                    ] for face_landmarks in face_landmarks]
                    fh = open(targetfile[:-4] + ".txt", "w")
                    for p in landmarks_as_tuples[0]:
                        fh.write(str(p[0]) + " " + str(p[1]) + "\n")
                    fh.close()
                except:
                    pass
        except:
            pass
Exemple #7
0
def face_encodings_fr(face_image, known_face_locations=None, num_jitters=1):
    """
    Given an image, return the 128-dimension face encoding for each face in the image.

    :param face_image: The image that contains one or more faces
    :param known_face_locations: Optional - the bounding boxes of each face if you already know them.
    :param num_jitters: How many times to re-sample the face when calculating encoding. Higher is more accurate, but slower (i.e. 100 is 100x slower)
    :return: A list of 128-dimensional face encodings (one for each face in the image)
    """
    raw_landmarks = _raw_face_landmarks(face_image,
                                        known_face_locations,
                                        model="large")
    return [
        np.array(
            face_encoder.compute_face_descriptor(face_image, raw_landmark_set,
                                                 num_jitters))
        for raw_landmark_set in raw_landmarks
    ]
Exemple #8
0
def aligned_face_fr(image,
                    f_locations=None,
                    number_of_times_to_upsample=ntup,
                    model=mod):

    wd = image.shape[1]

    image = imutils.resize(image, width=800)

    if f_locations is None:
        flocations = face_recognition.face_locations(
            image, number_of_times_to_upsample=ntup, model=mod)
    else:
        flocations = []
        for f_ori in f_locations:  # 在原圖的臉位置
            top = int(f_ori[0] * 800 / wd)  # 寬調整為800後的臉位置
            right = int(f_ori[1] * 800 / wd)
            bott = int(f_ori[2] * 800 / wd)
            left = int(f_ori[3] * 800 / wd)
            flocations.append((top, right, bott, left))

    raw_landmarks = _raw_face_landmarks(image, flocations, model="large")

    aligned_images_list = []
    for f_l in zip(flocations, raw_landmarks):
        top, right, bott, left = f_l[0]
        rect = [(left, top), (right, bott)]
        (x, y, w, h) = (left, top, right - left, bott - top)
        x1 = max(x, 0)
        y1 = max(y, 0)
        faceOrig = imutils.resize(image[y1:y + h, x1:x + w], width=256)
        landmk_np = shape_to_np(f_l[1])
        faceAligned_fr = fa.align_fr(image, landmk_np)
        aligned_images_list.append(faceAligned_fr)

    return aligned_images_list
Exemple #9
0
                if x < 0:
                    x = 0
                if w > height:
                    w = height
                if h < 0:
                    h = 0
                if y > width:
                    y = width

                sub_face = img[x:w, h:y]

                targetfile = targetfolder + "/" + image_path[:-4] + "_" + str(
                    index) + ".jpg"
                cv2.imwrite(targetfile, sub_face)
                try:
                    face_landmarks = _raw_face_landmarks(sub_face)
                    landmarks_as_tuples = [[
                        (p.x, p.y) for p in face_landmarks.parts()
                    ] for face_landmarks in face_landmarks]
                    fh = open(targetfile[:-4] + ".txt", "w")
                    for p in landmarks_as_tuples[0]:
                        fh.write(str(p[0]) + " " + str(p[1]) + "\n")
                    fh.close()
                except:
                    #print("")
                    pass
        except:
            #print("Error; image not cropped:" + image_path)
            pass
    #print("Succesfully croped" + image_path)
def face_analysis(frame,
                  detection_threshold=0.6,
                  recognition_threshold_distance=0.4,
                  save_new_ids_rate=5,
                  minimum_face_width=40,
                  top_picked_faces=10,
                  recognize=True,
                  recogniion_with_race_gender=True,
                  anaylize_emotions=False):
    '''
    face detection and recognition with gender, race and emotions detection
        parameters : 
            frame :                             (numpy 2d array)            rgb opencv / numpy fram/image.
            
            detection_threshold :               (float between 0.0 and 1.0) detection threshold that any detected face with less confidence than threshold will be ignored.
            
            recognition_threshold_distance :    (float between 0.0 and 1.0) verfy 2 faces if the distance between them less than or equal the given threshold,
                                            the allowed distance between known face encodes and detected one , the lower the more accurate.
            
            save_new_ids_rate :                 (int) the number of new faces encodes with its new ids that will be saved to the disk after added them to the known faces list.
            
            minimum_face_width :                (int) the minmum width of acceptable detected face, ignore any detected face with width less than the minimum.
            
            top_picked_faces :                  (int) sort faces by confidence and select only the top K faces to work with.
            
            recognize :                         (boolen) true if face recogonition process is required, false other wise
            
            recogniion_with_race_gender :       (boolen) true if want to use face encodings that predicted by recoginition net to feed to trained MLP to classify the gender and race. (NOTE: recognize parameter should be true to use the predicted encodes to feed to the MLP(facetag model) other wise this parameter will be ignored.

            anaylize_emotions :                 (boolen) true if u want to analyize faces emotions.


        returns:
            
            selected_faces_locations : (list of list contains 4 elements (x1, y1, x2, y2)), ex:[[x1,y1,x2,y2], [...], ...] the faces location in given frame after been filterd by minimum width and selected the top k faces of all faces.

            faces_confidences :        (list of floats between 0.0 and 1.0) contains the confidence of prediction of each face.

            detected_faces_ids :       (list of int) contains the ids of recognized faces if the recognize paramter flag is True, otherwise will return an empty list because there is no recognition process.
            
            faces_landmarks :          (list of dlib full_object_detection object) that contains the facial land marks for each face

            genders :                  (list of strings) contains the gender(Male/Female) of each face.
            races :                    (list of strings) contains the race (white, black, asian, etc..) of each face.
            emotions :                 (list of strings) contains the emotion (happy, angry, sad, fear, etc..) of each face.
            dicarded_faces :           (list of list contains 4 elements (x1, y1, x2, y2)), ex:[[x1,y1,x2,y2], [...], ...] the discarded faces on the two filtering proces(minimum width and top k selected).
    '''

    # initialize all returned data to be empty.
    selected_faces_locations,faces_confidences, detected_faces_ids, emotions = [], [], [], []
    faces_landmarks, genders, races, dicarded_faces = [], [], [], []

    if settings.Debug:
        print("\n[INFO][face_analysis_agent] recived frame with shape = ",
              frame.shape)

    # step one detect faces
    faces_locations, faces_confidences, faces_widths = face_detection(
        frame, detection_threshold=detection_threshold)

    # check if no faces found in the given frame return empty lists, mean nothing found.
    if len(faces_locations) == 0:
        return -1

    # filter the founded faces with minimum width (filter phase 1)
    filtered_faces, dicarded_faces = filter_faces_with_minimum_width(
        faces_locations,
        faces_confidences,
        faces_widths,
        minimum_face_width=minimum_face_width)

    # if there are remaining faces after width filtering process (continue to filter phase 2)
    if len(filtered_faces) > 0:
        # choose K top faces from all detected faces (filter phase 2)
        selected_faces_locations, faces_confidences, _, dicarded_faces = select_topK_faces(
            filtered_faces, dicarded_faces, k=top_picked_faces)

        # check if recogonize process required.
        if recognize:
            # will return the genders and races if with_gender_race flage is equal to true.
            detected_faces_ids, faces_landmarks, _, genders, races = get_face_ids(
                frame,
                selected_faces_locations,
                recognition_threshold_distance=recognition_threshold_distance,
                save_rate=save_new_ids_rate,
                with_gender_race=recogniion_with_race_gender)

        # if emotion anaylise required
        if anaylize_emotions:

            if len(
                    faces_landmarks
            ) == 0:  #if faces land marks not detectd before, detect them now!
                # reformating the faces bounding box to fit dlib format
                face_locations_for_landMarks = [
                    (top, right, bottom, left)
                    for [left, top, right, bottom] in selected_faces_locations
                ]
                # use face_recognition api to get facial landmarks (5 points model)
                faces_landmarks = _raw_face_landmarks(
                    frame, face_locations_for_landMarks, model='small')

            # get aligned face batches to feed to emotion extractor model.
            faces_batch = align(
                frame,
                selected_faces_locations,
                faces_landmarks,
                target_sizes=[emotion_extractor.get_input_shape()])

            emotions_batch = faces_batch[0]
            emotions = emotion_extractor.predict(emotions_batch)

    return selected_faces_locations, faces_confidences, detected_faces_ids, faces_landmarks, genders, races, emotions, dicarded_faces