def test_image(src,
               image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        if True in result:
            [
                print_result(src, image_to_check, name, distance,
                             show_distance) for is_match, name, distance in
                zip(result, known_names, distances) if is_match
            ]
        else:
            print_result(src, image_to_check, "unknown_person", None,
                         show_distance)

    if not unknown_encodings:
        print_result(image_to_check, "no_persons_found", None, show_distance)
    def test_face_distance(self):
        img_a1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        img_a2 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama2.jpg'))
        img_a3 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama3.jpg'))

        img_b1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'biden.jpg'))

        face_encoding_a1 = api.face_encodings(img_a1)[0]
        face_encoding_a2 = api.face_encodings(img_a2)[0]
        face_encoding_a3 = api.face_encodings(img_a3)[0]
        face_encoding_b1 = api.face_encodings(img_b1)[0]

        faces_to_compare = [
            face_encoding_a2,
            face_encoding_a3,
            face_encoding_b1]

        distance_results = api.face_distance(faces_to_compare, face_encoding_a1)

        # 0.6 is the default face distance match threshold. So we'll spot-check that the numbers returned
        # are above or below that based on if they should match (since the exact numbers could vary).
        self.assertEqual(type(distance_results), np.ndarray)
        self.assertLessEqual(distance_results[0], 0.6)
        self.assertLessEqual(distance_results[1], 0.6)
        self.assertGreater(distance_results[2], 0.6)
Exemple #3
0
def compare_faces(profile, person, target_encoding, threshold=0.6):
    profilelink, profilepic, distance = profile
    match = None
    try:
        image = urllib.request.urlopen(profilepic)
        unknown_image = face_recognition.load_image_file(image)
        unknown_encoding = face_recognition.face_encodings(unknown_image)

        if len(unknown_encoding) > 0:
            results = face_recognition.face_distance(target_encoding,
                                                     unknown_encoding[0])
            for result in results:
                if result < float(threshold):
                    person.instagram = encoding.smart_str(profilelink,
                                                          encoding='ascii',
                                                          errors='ignore')
                    person.instagramimage = encoding.smart_str(
                        profilepic, encoding='ascii', errors='ignore')
                    logging.info("Match found: " + person.full_name +
                                 " ,Instagram: " + person.instagram)
                    if args.vv == True:
                        print("\tMatch found: " + person.full_name)
                        print("\tInstagram: " + person.instagram)
                    match = person

    except Exception as e:
        logging.error("compare_faces: " + str(e))
        print("ERROR")
        print(e)

    return match
Exemple #4
0
def process_image(image_to_check,
                  known_names,
                  known_face_encodings,
                  tolerance=0.6,
                  show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)
    result_list = list()

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        recognized_name = ""
        if True in result:
            for is_match, name, distance in zip(result, known_names,
                                                distances):
                if is_match:
                    recognized_name = name
        else:
            recognized_name = "unknown_person"

        result_list.append(recognized_name)

    return image_to_check, result_list
def recognition_faces_in_image(knownfile_stream, detectfile_stream):
    # 载入用户上传的图片
    knownimg = face_recognition.load_image_file(knownfile_stream)
    detectimg = face_recognition.load_image_file(detectfile_stream)

    # 为用户上传的图片中的人脸编码
    knownface_encodings = face_recognition.face_encodings(knownimg)
    detectface_encodings = face_recognition.face_encodings(detectimg)

    if len(knownface_encodings) > 1:
        result = {"ret": 1, "msg": "knownface has more than one face"}
        return jsonify(result)

    if not knownface_encodings or not detectface_encodings:
        result = {"ret": 2, "msg": "knownface or detectface has no face"}
        return jsonify(result)

    checked_results = []
    for detectface_encoding in detectface_encodings:
        distances = face_recognition.face_distance(knownface_encodings,
                                                   detectface_encoding)
        checked_result = list(distances <= 0.6)
        checked_results.append(distances.tolist())

    # 讲识别结果以json键值对的数据结构输出
    result = {"ret": 0, "results": checked_results}
    return jsonify(result)
    def test_face_distance(self):
        img_a1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama.jpg'))
        img_a2 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama2.jpg'))
        img_a3 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'obama3.jpg'))

        img_b1 = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'biden.jpg'))

        face_encoding_a1 = api.face_encodings(img_a1)[0]
        face_encoding_a2 = api.face_encodings(img_a2)[0]
        face_encoding_a3 = api.face_encodings(img_a3)[0]
        face_encoding_b1 = api.face_encodings(img_b1)[0]

        faces_to_compare = [
            face_encoding_a2,
            face_encoding_a3,
            face_encoding_b1]

        distance_results = api.face_distance(faces_to_compare, face_encoding_a1)

        # 0.6 is the default face distance match threshold. So we'll spot-check that the numbers returned
        # are above or below that based on if they should match (since the exact numbers could vary).
        self.assertEqual(type(distance_results), np.ndarray)
        self.assertLessEqual(distance_results[0], 0.6)
        self.assertLessEqual(distance_results[1], 0.6)
        self.assertGreater(distance_results[2], 0.6)
def test_image(image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        if True in result:
            [
                print_result(image_to_check, name, distance, show_distance) for
                is_match, name, distance in zip(result, known_names, distances)
                if is_match
            ]
Exemple #8
0
def test_image(image_to_check, known_names, known_face_encodings):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if unknown_image.shape[1] > 1600:
        scale_factor = 1600.0 / unknown_image.shape[1]
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            unknown_image = scipy.misc.imresize(unknown_image, scale_factor)

    unknown_encodings = face_recognition.face_encodings(unknown_image)
    print("unknown_encodings " + str(unknown_encodings))

    if len(unknown_encodings) == 1:
        for unknown_encoding in unknown_encodings:
            result = face_recognition.compare_faces(known_face_encodings,
                                                    unknown_encoding)
            distance = face_recognition.face_distance(known_face_encodings,
                                                      unknown_encoding)
            print(distance[0])
            print("True") if True in result else print("False ")

        return distance[0], result[0]
    else:
        return "0", "Many Faces or No Faces"
def test_image(image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if unknown_image.shape[1] > 1600:
        scale_factor = 1600.0 / unknown_image.shape[1]
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            unknown_image = scipy.misc.imresize(unknown_image, scale_factor)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        if True in result:
            [
                print_result(image_to_check, name, distance, show_distance) for
                is_match, name, distance in zip(result, known_names, distances)
                if is_match
            ]
        else:
            print_result(image_to_check, "unknown_person", None, show_distance)
Exemple #10
0
def test_image(image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        if True in result:
            [
                print_result(image_to_check, name, distance, show_distance) for
                is_match, name, distance in zip(result, known_names, distances)
                if is_match
            ]
            #put IMAGE into firebase unknown_image?
        else:
            #no matches
            print_result(image_to_check, "unknown_person", None, show_distance)

    if not unknown_encodings:
        # print out fact that no faces were found in image
        print_result(image_to_check, "no_persons_found", None, show_distance)
Exemple #11
0
def test_image(image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)
    face_names = []
    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        match = face_recognition.compare_faces(known_face_encodings,
                                               unknown_encoding, 0.5)
        name = "Unknown"
        for k in range(len(match)):
            if match[k]:
                name = known_names[k]
        face_names.append(name)
    if not unknown_encodings:
        # print out fact that no faces were found in image
        print(image_to_check, "no_persons_found", None, show_distance)

    return face_names
def test(image1,encoding1,image2,encoding2):
    
    for encodings2 in encoding2:
        distance = face_recognition.face_distance(encoding1,encodings2)
        
    similarity = distance_to_similarity(distance[0])
    similarity = float("{0:.2f}".format(similarity))               
    return similarity 
    def test_face_distance_empty_lists(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'biden.jpg'))
        face_encoding = api.face_encodings(img)[0]

        # empty python list
        faces_to_compare = []

        distance_results = api.face_distance(faces_to_compare, face_encoding)
        self.assertEqual(type(distance_results), np.ndarray)
        self.assertEqual(len(distance_results), 0)

        # empty numpy list
        faces_to_compare = np.array([])

        distance_results = api.face_distance(faces_to_compare, face_encoding)
        self.assertEqual(type(distance_results), np.ndarray)
        self.assertEqual(len(distance_results), 0)
    def test_face_distance_empty_lists(self):
        img = api.load_image_file(os.path.join(os.path.dirname(__file__), 'test_images', 'biden.jpg'))
        face_encoding = api.face_encodings(img)[0]

        # empty python list
        faces_to_compare = []

        distance_results = api.face_distance(faces_to_compare, face_encoding)
        self.assertEqual(type(distance_results), np.ndarray)
        self.assertEqual(len(distance_results), 0)

        # empty numpy list
        faces_to_compare = np.array([])

        distance_results = api.face_distance(faces_to_compare, face_encoding)
        self.assertEqual(type(distance_results), np.ndarray)
        self.assertEqual(len(distance_results), 0)
def get_face_name(faces_to_compare,face_encodings):
    orgin_name,targe_name,simily='','',0
    for o in faces_to_compare:
        name=o["name"]
        _face_encodings=o["face_encodings"]
        max_distance=max(api.face_distance([face_encodings],_face_encodings))
        if max_distance > 0.8 :
            return name,max_distance
        else  :
            simily= simily if simily >max_distance else max_distance
            targe_name= targe_name if simily >max_distance else name
    return targe_name,simily
Exemple #16
0
def test(encoding1, encoding2):
    """
    Compare document with Person Image Encodings.
    """
    if len(encoding1) < 1 or len(encoding2) < 1:
        print("No Face Identified")
        sys.exit(0)
    for encodings2 in encoding2:
        distance = face_recognition.face_distance(encoding1, encodings2)
    similarity = distance_to_similarity(distance[0])
    similarity = float("{0:.2f}".format(similarity))
    return similarity
Exemple #17
0
def save_keypoints_video(video_path, input_folder, output_folder, tolerance,
                         batch_size):
    video = cv2.VideoCapture(str(video_path))

    out_path = move_path(video_path, input_folder, output_folder)
    out_path = out_path.with_suffix('')
    known_faces = []
    frame_count = 0
    frames = []
    while video.isOpened():
        ret, frame = video.read()
        if ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = scale_frame(frame)
            frames.append(frame)

        if (len(frames) == batch_size) or not ret:
            batch_of_face_locations = face_recognition.batch_face_locations(
                frames,
                number_of_times_to_upsample=upsample,
                batch_size=batch_size)

            # Now let's list all the faces we found in all batch_size frames
            for i, face_locations in enumerate(batch_of_face_locations):
                frame_count += 1
                encodings, landmarks = face_recognition.face_encodings_and_landmarks(
                    frames[i], known_face_locations=locations)

                num_faces = landmarks.shape[0]
                if num_faces == 0:
                    continue

                for face_idx, (encoding, landmark_array) in enumerate(
                        zip(encodings, landmarks)):
                    distances = face_recognition.face_distance(
                        known_faces, encoding)
                    if len(distances) == 0 or distances.min() > tolerance:
                        known_idx = len(known_faces)
                        known_faces.append(encoding)
                    else:
                        known_idx = int(np.argmin(distances))
                        known_faces[known_idx] = np.mean(
                            [known_faces[known_idx], encoding], axis=0)

                    file_path = Path(out_path, F"{known_idx}",
                                     F"{frame_count}.npy")
                    file_path.parent.mkdir(parents=True, exist_ok=True)
                    np.save(file_path, landmark_array)

            # Clear the frames array to start the next batch
            frames = []

    print(F"finished video {out_path}")
Exemple #18
0
 def recognize(self, faces):
     faces = [
         _trim_css_to_bounds(_rect_to_css(face), self.sample.shape)
         for face in faces
     ]
     unknown_encodings = face_recognition.face_encodings(self.sample, faces)
     distances = face_recognition.face_distance(unknown_encodings,
                                                self.person_picture)
     for i, distance in enumerate(distances):
         self.reco_distance = distance
         if self.reco_distance <= self.reco_tolerance:
             return i
     return -1
Exemple #19
0
def recognize_faces(face_image_path):
    # Read image from disk
    image = api.load_image_file(face_image_path)

    # Get face locations in image
    face_locations = api.face_locations(image, model="hog")

    # Create encodings from image
    face_encodings = api.face_encodings(image, known_face_locations=face_locations)

    # Get encodings from DB
    existing_face_encoding_objects = [list(g) for _, g in groupby(FaceEncoding.query.order_by(FaceEncoding.player_id).all(), attrgetter('player_id'))]

    outputs = []

    for i, face_encoding in enumerate(face_encodings):
        distances = {}

        for face_encoding_group in existing_face_encoding_objects:
            # Calculate distance to every encoding for this group (person)
            existing_face_encodings = np.array([fe.encoding for fe in face_encoding_group])
            face_distances = api.face_distance(existing_face_encodings, face_encoding)

            distances[face_encoding_group[0].player] = sum(face_distances) / len(face_distances)

        recognized_player = None
        if len(distances) > 0:
            recognized_player = min(distances, key=distances.get)
            if distances[recognized_player] > THRESHOLD:
                recognized_player = None

        # Create a crop of the face
        top, right, bottom, left = face_locations[i]
        image_crop = image[top:bottom, left:right]

        im = Image.fromarray(image_crop)
        with io.BytesIO() as output:
            im.save(output, format="JPEG")
            contents = output.getvalue()

        outputs.append(
            {
                "image": base64.b64encode(contents).decode("utf-8"),
                "embedding": face_encoding.tolist(),
                "player": recognized_player.serialize() if recognized_player else None
            })

    return outputs
Exemple #20
0
def compute_distances(image_to_check, known_face_encodings):
    unknown_encodings = face_recognition.face_encodings(image_to_check,
                                                        num_jitters=1)
    #skimage.io.imsave(open('try0.png','wb'), image_to_check, plugin='pil', format_str='png')
    if len(unknown_encodings) == 0:
        rotated = skimage.transform.rotate(image_to_check, 90, resize=True)
        unknown_encodings = face_recognition.face_encodings(rotated,
                                                            num_jitters=3)
    if len(unknown_encodings) == 0:
        rotated = skimage.transform.rotate(image_to_check, -90, resize=True)
        unknown_encodings = face_recognition.face_encodings(rotated,
                                                            num_jitters=3)
    return [
        list(
            face_recognition.face_distance(known_face_encodings,
                                           unknown_encoding))
        for unknown_encoding in unknown_encodings
    ]
    def test_bad_face_matching_confidence(self):
        self.tolerance = 0.50
        self.threshold = 0.80

        ic_path = os.path.join(test_data_path, 'my ic.jpg')
        passport_path = os.path.join(test_data_path, 'passport.jpg')

        image_ic = api.load_image_file(ic_path)
        image_passport = api.load_image_file(passport_path)

        face_encoding_ic = api.face_encodings(image_ic)[0]
        face_encoding_passport = api.face_encodings(image_passport)[0]

        face_distances = api.face_distance([face_encoding_ic],
                                           face_encoding_passport)
        confidence = self.face_distance_to_conf(face_distances, self.tolerance)

        self.assertLessEqual(confidence, self.threshold)
Exemple #22
0
def test_image(image_to_check,
               known_names,
               known_face_encodings,
               tolerance=0.6,
               show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings,
                                                   unknown_encoding)
        result = list(distances <= tolerance)

        if True in result:
            print('hello biotch')
            [
                print_result(image_to_check, name, distance, show_distance) for
                is_match, name, distance in zip(result, known_names, distances)
                if is_match
            ]
            for is_match, name, distance in zip(result, known_names,
                                                distances):
                if is_match:
                    print('hello again bitch')
                    call([
                        "python", "GUI.py", "found", name,
                        str(distance),
                        str(tolerance)
                    ])
        else:
            print_result(image_to_check, "unknown_person", None, show_distance)
            call(["python", "GUI.py", "not_found"])

    if not unknown_encodings:
        # print out fact that no faces were found in image
        print_result(image_to_check, "no_persons_found", None, show_distance)
        call(["python", "GUI.py", "unknown"])
    def test_face_matching_confidence(self):
        self.tolerance = 0.50
        self.threshold = 0.80

        ic_path = os.path.join(test_data_path, 'my ic.jpg')
        driving_license_path = os.path.join(test_data_path,
                                            'my driving license.jpg')

        image_ic = api.load_image_file(ic_path)
        image_driving_license = api.load_image_file(driving_license_path)

        face_encoding_ic = api.face_encodings(image_ic)[0]
        face_encoding_driving = api.face_encodings(image_driving_license)[0]

        face_distances = api.face_distance([face_encoding_ic],
                                           face_encoding_driving)
        confidence = self.face_distance_to_conf(face_distances, self.tolerance)

        self.assertGreaterEqual(confidence, self.threshold)
Exemple #24
0
def test_image(image_to_check, known_names, known_face_encodings, tolerance=0.6, show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if unknown_image.shape[1] > 1600:
        scale_factor = 1600.0 / unknown_image.shape[1]
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            unknown_image = scipy.misc.imresize(unknown_image, scale_factor)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings, unknown_encoding)
        result = list(distances <= tolerance)

        if True in result:
            [print_result(image_to_check, name, distance, show_distance) for is_match, name, distance in zip(result, known_names, distances) if is_match]
        else:
            print_result(image_to_check, "unknown_person", None, show_distance)
    def match_face_encodings(self, known_face, face_to_compare):
        """
        Match face encodings
        :param known_face: Face object as the basis for verification
        :param face_to_compare: Face object to verify
        :return: Value of face distance as computed
        """
        known_face_encoding = self.get_encoding(known_face)
        face_encoding_to_compare = self.get_encoding(face_to_compare)

        if known_face_encoding.size < 1:
            known_face_encoding = self.encode_image(known_face)

        if face_encoding_to_compare.size < 1:
            face_encoding_to_compare = self.encode_image(face_to_compare)

        face_distance = api.face_distance(
            face_encodings=[known_face_encoding], face_to_compare=face_encoding_to_compare
        )

        return face_distance[0]
Exemple #26
0
def save_keypoints_video(video_path, input_folder, output_folder, model,
                         tolerance):
    video = cv2.VideoCapture(str(video_path))

    out_path = move_path(video_path, input_folder, output_folder)
    out_path = out_path.with_suffix('')
    known_faces = []
    frame_count = 0
    while video.isOpened():
        ret, frame = video.read()
        if not ret:
            break

        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        locations = face_recognition.face_locations(frame, model=model)
        encodings, landmarks = face_recognition.face_encodings_and_landmarks(
            frame, known_face_locations=locations)

        num_faces = landmarks.shape[0]
        if num_faces == 0:
            continue

        for face_idx, (encoding,
                       landmark_array) in enumerate(zip(encodings, landmarks)):
            distances = face_recognition.face_distance(known_faces, encoding)
            if len(distances) == 0 or distances.min() > tolerance:
                known_idx = len(known_faces)
                known_faces.append(encoding)
            else:
                known_idx = int(np.argmin(distances))
                known_faces[known_idx] = np.mean(
                    [known_faces[known_idx], encoding], axis=0)

            file_path = Path(out_path, F"{known_idx}", F"{frame_count}.npy")
            file_path.parent.mkdir(parents=True, exist_ok=True)
            np.save(file_path, landmark_array)

        frame_count += 1

    print(F"finished video {out_path}")
def test_image(image_to_check, known_names, known_face_encodings, tolerance=0.6, show_distance=False):
    unknown_image = face_recognition.load_image_file(image_to_check)

    # Scale down image if it's giant so things run a little faster
    if max(unknown_image.shape) > 1600:
        pil_img = PIL.Image.fromarray(unknown_image)
        pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS)
        unknown_image = np.array(pil_img)

    unknown_encodings = face_recognition.face_encodings(unknown_image)

    for unknown_encoding in unknown_encodings:
        distances = face_recognition.face_distance(known_face_encodings, unknown_encoding)
        result = list(distances <= tolerance)

        if True in result:
            [print_result(image_to_check, name, distance, show_distance) for is_match, name, distance in zip(result, known_names, distances) if is_match]
        else:
            print_result(image_to_check, "unknown_person", None, show_distance)

    if not unknown_encodings:
        # print out fact that no faces were found in image
        print_result(image_to_check, "no_persons_found", None, show_distance)
def compare_faces_in_image(knownface, detectface):
    knownface_encodings = []
    detectface_encodings = []

    knownface_encodings.append(np.array(knownface))
    detectface_encodings.append(np.array(detectface))
    if len(knownface_encodings) > 1:
        result = {"ret": 1, "msg": "knownface has more than one face"}
        return jsonify(result)

    if not knownface_encodings or not detectface_encodings:
        result = {"ret": 2, "msg": "knownface or detectface has no face"}
        return jsonify(result)

    checked_results = []
    for detectface_encoding in detectface_encodings:
        distances = face_recognition.face_distance(knownface_encodings,
                                                   detectface_encoding)
        checked_result = list(distances <= 0.6)
        checked_results.append(distances.tolist())

    # 讲识别结果以json键值对的数据结构输出
    result = {"ret": 0, "results": checked_results}
    return jsonify(result)
        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings,
                                                     face_encoding,
                                                     tolerance=0.35)
            name = "Unknown"

            # # If a match was found in known_face_encodings, just use the first one.
            # if True in matches:
            #     first_match_index = matches.index(True)
            #     name = known_face_ids[first_match_index]

            # Or instead, use the known face with the smallest distance to the new face
            face_distances = face_recognition.face_distance(
                known_face_encodings, face_encoding)
            # print(face_distances)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_ids[best_match_index]

            face_names.append(name)
    if (sanity_count == 0):
        prev_name = name
        sanity_count += 1
    elif (sanity_count < 60):
        if (prev_name == name and name != "Unknown"):
            sanity_count += 1
            prev_name = name
        else:
            sanity_count = 0
Exemple #30
0

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument(
        "-i",
        "--new-image",
        required=True,
        help=
        "sciezka do obrazu, z ktorym porownywane beda zdjecia z bazy dancyh")
    ap.add_argument("-e",
                    "--encodings-file",
                    required=True,
                    help="sciezka do pliku z kodowaniem twarzy")
    args = vars(ap.parse_args())

    new_encoded_image = get_encoding_for_image(args['new_image'])

    # odczytywanie kodowania twarzy z bazy danych
    results = pickle.loads(open(args['encodings_file'], "rb").read())

    encodings = results['encodings']
    names = results['names']

    # obliczanie odleglosci - czyli podobienstwa twarzy i wypisywanie ich
    distance_results = face_distance(encodings, new_encoded_image)
    results = list(zip(distance_results, names))
    sorted_list = sorted(results, key=lambda x: x[0])
    for x in sorted_list:
        print(x)
def mark_your_attendance():

    mpl.rcParams['toolbar'] = 'None'
    STORAGE_PATH = "/home/harsh/face-recognition-attendance-system/storage"

    try:
        with open(os.path.join(STORAGE_PATH, "known_face_ids.pickle"),
                  "rb") as fp:
            known_face_ids = pickle.load(fp)
        with open(os.path.join(STORAGE_PATH, "known_face_encodings.pickle"),
                  "rb") as fp:
            known_face_encodings = pickle.load(fp)
        # known_face_ids = np.load("known_face_ids.npy")
        # known_face_encodings = np.load("known_face_encodings.npy")
    except:
        known_face_encodings = []
        known_face_ids = []

    # CSV_PATH = "/home/harsh/Backup/face-recognition/data/attendance.csv"
    CSV_PATH = "/home/harsh/face-recognition-attendance-system/static/data/attendance.csv"

    if (os.path.exists(CSV_PATH)):
        csv_file = open(CSV_PATH, "a+")
        writer = csv.writer(csv_file)

    else:
        os.mknod(CSV_PATH)
        csv_file = open(CSV_PATH, "w+")
        writer = csv.writer(csv_file)
        writer.writerow(["Student ID", "Date", "Time of Entry"])

    name = "Unknown"
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True
    sanity_count = 0
    unknown_count = 0
    marked = True

    video_capture = cv2.VideoCapture(0)
    ret, frame = video_capture.read()

    plot = plt.subplot(1, 1, 1)
    plt.title("Detecting Face")
    plt.axis('off')
    im1 = plot.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

    while True:
        # Grab a single frame of video
        ret, frame = video_capture.read()
        # print("FRAME READ WORKS")
        # Resize frame of video to 1/4 size for faster face recognition processing
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]

        # Only process every other frame of video to save time
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(
                rgb_small_frame, face_locations)

            face_names = []
            for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                matches = face_recognition.compare_faces(known_face_encodings,
                                                         face_encoding,
                                                         tolerance=0.35)
                name = "Unknown"

                # # If a match was found in known_face_encodings, just use the first one.
                # if True in matches:
                #     first_match_index = matches.index(True)
                #     name = known_face_ids[first_match_index]

                # Or instead, use the known face with the smallest distance to the new face
                face_distances = face_recognition.face_distance(
                    known_face_encodings, face_encoding)
                # print(face_distances)
                try:
                    best_match_index = np.argmin(face_distances)
                except:
                    # print("No students have been marked")
                    video_capture.release()
                    cv2.destroyAllWindows()
                    marked = False
                    return marked
                if matches[best_match_index]:
                    name = known_face_ids[best_match_index]

                face_names.append(name)

        if (name == "Unknown"):
            unknown_count += 1
        else:
            unknown_count = 0

        if (unknown_count == 600):
            # video_capture.release()
            # cv2.destroyAllWindows()
            # print("You haven't been registered")
            marked = False
            unknown_count = 0
            break

        process_this_frame = not process_this_frame

        # Display the results
        for (top, right, bottom, left), name in zip(face_locations,
                                                    face_names):
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
            top *= 4
            right *= 4
            bottom *= 4
            left *= 4

            # Draw a box around the face
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            # Draw a label with a name below the face
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom),
                          (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.7,
                        (255, 255, 255), 1)

        # print("BEFORE sHOWING")
        # Display the resulting image
        # cv2.imshow('Video', frame)
        # if cv2.waitKey(20) == 27:
        #     break

        plt.ion()
        im1.set_data(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
        plt.pause(0.001)
        # as opencv loads in BGR format by default, we want to show it in RGB.
        plt.show()

        # print("AFTER SHOWING")
        # Hit 'q' on the keyboard to quit!
        if (sanity_count == 0):
            prev_name = name
            sanity_count += 1

        elif (sanity_count < 60):
            if (prev_name == name and name != "Unknown"):
                sanity_count += 1
                prev_name = name
            else:
                sanity_count = 0

        elif (sanity_count == 60):
            # print("Face registered")
            # video_capture.release()
            # cv2.destroyAllWindows()
            sanity_count = 0
            now = datetime.now()
            dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
            date = dt_string.split(" ")[0]
            time = dt_string.split(" ")[1]
            writer.writerow([name, date, time])
            # print(name + date + time)
            break

    # Release handle to the webcam

    plt.close()
    video_capture.release()
    cv2.destroyAllWindows()

    return marked
Exemple #32
0
import csv


dir='face_photos_small'
files=os.listdir(dir)
csv=open('dataset.csv',mode='w')

encodingmap={}

for file in files:
    filepath=dir+"/"+file
    image=fr.load_image_file(filepath)
    encoding=fr.face_encodings(image)[0]
    encodingmap[file]=encoding
    # print(file,encoding)
    csv.write('"'+file+'"')
    for e in encoding:
        csv.write(','+str(e))
    csv.write('\n')
csv.close()

for f1 in encodingmap.keys():
    for f2 in encodingmap.keys():
        if f1==f2:
            continue
        e1=encodingmap[f1]
        e2=encodingmap[f2]
        # print(f1,e1,f2,e2)
        dist=fr.face_distance([e1],e2)
        print(f1,f2,dist)
Exemple #33
0
def analyseFootage(clipname):
    CLIP_PATH = FOOTAGES_PATH + "/" + clipname

    if os.path.isfile(CLIP_PATH) == False:
        return 0

    #Load the known face IDs and encodings for facial recognition
    try:
        with open(os.path.join(STORAGE_PATH, "known_face_ids.pickle"),
                  "rb") as fp:
            known_face_ids = pickle.load(fp)
        with open(os.path.join(STORAGE_PATH, "known_face_encodings.pickle"),
                  "rb") as fp:
            known_face_encodings = pickle.load(fp)
    except:
        known_face_encodings = []
        known_face_ids = []

    #Start the Video Stream
    fvs = FileVideoStream(CLIP_PATH).start()
    time.sleep(1.0)

    print("[INFO] Loading the facial detector")
    detector = dlib.get_frontal_face_detector()
    #predictor = dlib.shape_predictor(LANDMARK_PATH)
    #fa = FaceAligner(predictor, desiredFaceWidth = 96)
    name = "Unknown"
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True
    #sanity_count = 0
    unknown_count = 0
    marked = True

    print("[INFO] Initializing CCTV Footage")
    while fvs.more():
        # grab the frame from the threaded video file stream, resize
        # it, and convert it to grayscale (while still retaining 3
        # channels)
        frame = fvs.read()

        if frame is None:
            break

        frame = imutils.resize(frame, width=600)

        frame = adjust_gamma(frame, gamma=1.5)
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        #To store the faces
        #This will detect all the images in the current frame, and it will return the coordinates of the faces
        #Takes in image and some other parameter for accurate result
        faces = detector(gray_frame, 0)
        #In above 'faces' variable there can be multiple faces so we have to get each and every face and draw a rectangle around it.

        #sampleNum = sampleNum+1
        for face in faces:
            #num_frames = num_frames + 1
            #print("inside for loop")

            if face is None:
                print("face is none")
                continue

            #face_aligned = fa.align(frame,gray_frame,face)
            #face_aligned = imutils.resize(face_aligned ,width = 600)

            if process_this_frame:
                # Find all the faces and face encodings in the current frame of video
                face_locations = face_recognition.face_locations(frame)
                face_encodings = face_recognition.face_encodings(
                    frame, face_locations)

                face_names = []
                for face_encoding in face_encodings:
                    # See if the face is a match for the known face(s)
                    matches = face_recognition.compare_faces(
                        known_face_encodings, face_encoding, tolerance=0.35)
                    name = "Unknown"

                    # # If a match was found in known_face_encodings, just use the first one.
                    # if True in matches:
                    #     first_match_index = matches.index(True)
                    #     name = known_face_ids[first_match_index]

                    # Or instead, use the known face with the smallest distance to the new face
                    face_distances = face_recognition.face_distance(
                        known_face_encodings, face_encoding)
                    # print(face_distances)
                    try:
                        best_match_index = np.argmin(face_distances)
                        if matches[best_match_index]:
                            name = known_face_ids[best_match_index]
                    except:
                        # print("No students have been marked")
                        #video_capture.release()
                        #cv2.destroyAllWindows()

                        marked = False
                        #return marked
                    #if matches[best_match_index]:
                    #    name = known_face_ids[best_match_index]

                    face_names.append(name)

            if name == "Unknown":
                unknown_count += 1
            else:
                unknown_count = 0

            if unknown_count == 600:
                # video_capture.release()
                # cv2.destroyAllWindows()
                # print("You haven't been registered")
                marked = False
                unknown_count = 0
                break

            process_this_frame = not process_this_frame

            for (top, right, bottom,
                 left), name in zip(face_locations, face_names):

                # Draw a box around the face
                cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255),
                              1)

                # Draw a label with a name below the face
                cv2.rectangle(frame, (left, bottom + 15), (right, bottom),
                              (0, 0, 255), cv2.FILLED)
                font = cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(frame, name, (left + 6, bottom + 15), font, 0.4,
                            (255, 255, 255), 1)

        #Showing the image in another window
        #Creates a window with window name "Face" and with the image img
        #cv2.imshow("Video feed (PRESS Q TO QUIT",frame)
        frame = cv2.imencode('.jpg', frame)[1].tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

        #if cv2.waitKey(1) == ord("q") :
        #    break

    print("here")
    # do a bit of cleanup
    cv2.destroyAllWindows()
    #fvs.stop()
    return
def mark_your_attendance(location, known_face_encodings, known_face_ids):

    mpl.rcParams['toolbar'] = 'None'

    if (os.path.exists(DB_PATH)):
        #rdbms='sqlite'
        #conn = psycopg2.connect(DB_PATH)
        #c=conn.cursor()
        rdbms = 'postgresql'
        conn = psycopg2.connect(host="localhost",
                                database="face_rec_db",
                                user="******",
                                password="******")
        c = conn.cursor()
    else:
        #os.mknod(DB_PATH)
        conn = sqlite3.connect(DB_PATH)
        c = conn.cursor()
        c.execute('''CREATE TABLE IF NOT EXISTS ATTENDANCE
         (ID        TEXT   NOT NULL,
         TIMESTAMP  TEXT       NOT NULL,
         LOCATION  TEXT);''')
        conn.commit()

    name = "Unknown"
    face_locations = []
    face_encodings = []
    face_names = []
    process_this_frame = True
    sanity_count = 0
    unknown_count = 0
    marked = True

    video_capture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    #_,frame = video_capture.read()

    while True:
        # Grab a single frame of video
        _, frame = video_capture.read()

        #Applying face enhancement steps
        frame = imageEnhancement.adjust_gamma(frame, gamma=1.5)

        # Resize frame of video to 1/4 size for faster face recognition processing
        #small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
        small_frame = frame
        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]

        # Only process every other frame of video to save time
        if process_this_frame:
            # Find all the faces and face encodings in the current frame of video
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(
                rgb_small_frame, face_locations)

            face_names = []
            for face_encoding in face_encodings:
                # See if the face is a match for the known face(s)
                matches = face_recognition.compare_faces(known_face_encodings,
                                                         face_encoding,
                                                         tolerance=0.35)
                name = "Unknown"

                # # If a match was found in known_face_encodings, just use the first one.
                # if True in matches:
                #     first_match_index = matches.index(True)
                #     name = known_face_ids[first_match_index]

                # Or instead, use the known face with the smallest distance to the new face
                face_distances = face_recognition.face_distance(
                    known_face_encodings, face_encoding)
                # print(face_distances)
                try:
                    best_match_index = np.argmin(face_distances)
                except:
                    # print("No students have been marked")
                    video_capture.release()
                    cv2.destroyAllWindows()
                    marked = False
                    return marked
                if matches[best_match_index]:
                    name = known_face_ids[best_match_index]

                face_names.append(name)

        if name == "Unknown":
            unknown_count += 1
        else:
            unknown_count = 0

        if unknown_count == 600:
            # video_capture.release()
            # cv2.destroyAllWindows()
            # print("You haven't been registered")
            marked = False
            unknown_count = 0
            break

        process_this_frame = not process_this_frame

        # Display the results
        for (top, right, bottom, left), name in zip(face_locations,
                                                    face_names):
            # Scale back up face locations since the frame we detected in was scaled to 1/4 size
            #top *= 4
            #right *= 4
            #bottom *= 4
            #left *= 4

            # Draw a box around the face
            cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

            # Draw a label with a name below the face
            cv2.rectangle(frame, (left, bottom - 35), (right, bottom),
                          (0, 0, 255), cv2.FILLED)
            font = cv2.FONT_HERSHEY_DUPLEX
            cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.7,
                        (255, 255, 255), 1)

        # print("AFTER SHOWING")
        # Hit 'q' on the keyboard to quit!
        if (sanity_count == 0):
            prev_name = name
            sanity_count += 1

        elif sanity_count < 60:
            if (prev_name == name and name != "Unknown"):
                sanity_count += 1
                prev_name = name
            else:
                sanity_count = 0

        elif sanity_count == 60:
            # print("Face registered")
            # video_capture.release()
            # cv2.destroyAllWindows()
            sanity_count = 0
            # now = datetime.now()
            # if(entry_or_exit==0):
            #     c.execute("INSERT INTO ATTENDANCE VALUES (?,datetime('now'),'IN');",(name, ))
            # else:
            #     c.execute("INSERT INTO ATTENDANCE VALUES (?,datetime('now'),'OUT');",(name, ))

            if (rdbms == 'sqlite'):
                c.execute(
                    "INSERT INTO ATTENDANCE VALUES (?,datetime('now'),?);", (
                        name,
                        location,
                    ))
            elif (rdbms == 'postgresql'):
                c.execute("INSERT INTO attendance VALUES (%s,now(),%s);",
                          (name, location))
            conn.commit()

            break

        #OpenCV's implementation to show an image in window(doesn't work on production server)
        #cv2.imshow("Marking Attendance (PRESS Q TO QUIT)",frame)

        #Encoding the frame to be stream into browser
        frame = cv2.imencode('.jpg', frame)[1].tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

        #if cv2.waitKey(20) == ord("q"):
        #    break

    # Release handle to the webcam

    #plt.close()
    video_capture.release()
    cv2.destroyAllWindows()
    conn.close()

    return marked