def encode_all_people_in_db(): crud = Crud() people = crud.get_person({}) for person in people: image_location = person["photo_location"] image_encoding = encode_image(image_location) if len(image_encoding) > 1: image_encoding = image_encoding[0] if len(image_encoding)>0: crud.update_person(person, {"encoded_face":list(image_encoding), "has_encodings":True})
class TestPersonSeen(TestCase): def setUp(self): self.sc = SocketClient() self.crud = Crud() def test_identify_and_insert_alex(self): picture = self.sc.get_picture() known_faces = get_feature_encoding_list() people_similar = compare_face_to_known(picture, known_faces, True) if people_similar is not None: most_similar = people_similar[0] self.assertEqual(most_similar["name"], "Alex Young") self.crud.person_seen_db_insert(most_similar) person_seen = self.crud.find_someone_seen_in_last_x_seconds(2) self.assertEqual(person_seen[0], "Alex Young") else: self.fail("No faces found")
class TestEncodeTestImages(TestCase): def setUp(self): insert_test_faces_into_db() self.crud = Crud() def test_encode_images(self): encode_all_people_in_db() person = self.crud.get_person({"name":"AJ Cook"}) self.assertTrue("encoded_face" in person[0]) self.assertTrue(len(person[0]["encoded_face"]) > 2)
def insert_test_faces_into_db(): crud = Crud() crud.delete_all_people() for person in os.listdir(DATA_DIR): person_dir = DATA_DIR + person + os.sep for pic in os.listdir(person_dir): if "0001.jpg" in pic: name = " ".join(person.split("_")) role = ROLES[random.randint(0, 6)] person_data_pic = DATA_DIR + person + os.sep + pic person_target_dir = PICTURE_DIR + person person_target_pic = person_target_dir + os.sep + person + ".jpg" if not os.path.exists(person_target_dir): os.makedirs(person_target_dir) copyfile(person_data_pic, person_target_pic) person_data = { "name": name, "role": role, "photo_location": PICTURE_DIR + person + os.sep + person + ".jpg", "has_encodings": False } crud.insert_person(person_data)
def compare_face_to_known(face_to_match, known_face_list, to_match_top=1): # Load a test image and get encondings for it encode_face_to_match = encode_image(face_to_match) encoded_known_faces = known_face_list[1] names_known_faces = known_face_list[0] if len(encode_face_to_match) == 0: return None # See how far apart the test image is from the known faces face_distances = face_recognition.face_distance(encoded_known_faces, encode_face_to_match) person_distance_from_match = list(zip(names_known_faces, face_distances)) person_distance_from_match.sort(key=lambda x: x[1]) similar_people = person_distance_from_match[0:to_match_top] similar_people_records = [] crud = Crud() for person in similar_people: if person[1] < 0.5: person = crud.get_person({"name": person[0]})[0] similar_people_records.append(person) return similar_people_records
def setUp(self): self.sc = SocketClient() self.crud = Crud()
def setUp(self): insert_test_faces_into_db() self.crud = Crud()