-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
136 lines (114 loc) · 6.12 KB
/
Copy pathapp.py
File metadata and controls
136 lines (114 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import argparse
import json
import os
import re
import person
import storage
import embeddings
import faces
import identity
from erase import clear_folder
import table
def main():
parser = argparse.ArgumentParser(description="Run the facial recognition app.")
parser.add_argument('--config', required=True, help='Path to the configuration file')
args = parser.parse_args()
with open(args.config, 'r') as config_file:
config = json.load(config_file)
base_path = config['base_path']
images_folder = os.path.join(base_path, 'images')
faces_folder = os.path.join(base_path, 'faces')
haarcascade_path = os.path.join(base_path, 'haarcascade_frontalface_default/haarcascade_frontalface_default.xml')
people_file = os.path.join(base_path, 'people')
reference_face_folder = os.path.join(base_path, 'reference_face')
reference_image_folder = os.path.join(base_path, 'reference_image')
print("What do you want to do:")
print("1 - Create the pictures table in the database (you need to do this first, once, if you haven't already)")
print("2 - Process images and upload embeddings")
print("3 - Assign a new person to, or update a profile in the database")
print("4 - Search for images by name")
print("5 - Delete a person from the database")
choice = input("Enter the number of your choice: ")
if choice == '1':
table.create_table()
print("Table created successfully.")
elif choice == '2':
faces.find_faces(images_folder, faces_folder, haarcascade_path)
embeddings.upload_face_embeddings(faces_folder)
clear_folder(faces_folder)
clear_folder(images_folder)
print("Images processed and embeddings uploaded successfully.")
elif choice == '3':
file_existance = os.path.exists(people_file)
while True:
input_name = input("Enter the name of the person you wish to create / update: ")
if not re.match(r'^[a-zA-Z\s\-\.,!?]+$', input_name):
print("Invalid input. Please enter a valid string.")
else:
creation_name = input_name.lower()
break
match file_existance:
case True:
profiles = storage.load_persons(people_file)
if person.person_exists(creation_name, profiles):
update = input("A profile with the same name already exists. Do you want to update it? (y/n): ")
if update.lower() == 'y':
faces.reference_face(reference_image_folder, reference_face_folder, haarcascade_path)
string_representation = embeddings.create_embedding_string(os.path.join(reference_face_folder, config['image_name']))
identity.assign_identity(creation_name, profiles, string_representation, config['threshold'])
clear_folder(reference_image_folder)
clear_folder(reference_face_folder)
print("Identity updated successfully.")
else:
print("Operation cancelled.")
else:
profile = person.create_person(creation_name)
profiles[profile.name] = profile.id
storage.save_persons(profiles, people_file)
faces.reference_face(reference_image_folder, reference_face_folder, haarcascade_path)
string_representation = embeddings.create_embedding_string(os.path.join(reference_face_folder, config['image_name']))
identity.assign_identity(creation_name, profiles, string_representation, config['threshold'])
clear_folder(reference_image_folder)
clear_folder(reference_face_folder)
print("Person instance created and identity assigned successfully.")
case False:
profiles = {}
profile = person.create_person(creation_name)
profiles[profile.name] = profile.id
storage.save_persons(profiles, people_file)
profiles = storage.load_persons(people_file)
faces.reference_face(reference_image_folder, reference_face_folder, haarcascade_path)
string_representation = embeddings.create_embedding_string(os.path.join(reference_face_folder, config['image_name']))
identity.assign_identity(creation_name, profiles, string_representation, config['threshold'])
clear_folder(reference_image_folder)
clear_folder(reference_face_folder)
print("Person instance created and identity assigned successfully.")
elif choice == '4':
search_name = input("Enter the name of the person you wish to search for: ")
profiles = storage.load_persons(people_file)
if person.person_exists(search_name, profiles):
results = identity.search_by_name(search_name, profiles)
seen = set()
for pic in results:
parts = pic.rsplit('_', 1)
original = parts[0]
if original not in seen:
seen.add(original)
print(original)
else:
print("Name not recognized. Please try again.")
elif choice == '5':
profiles = storage.load_persons(people_file)
deletion_name = input("Enter the name of the person you wish to delete: ").lower()
if person.person_exists(deletion_name, profiles):
target_id = person.get_person_id(deletion_name, profiles)
identity.clear_id_and_sim(target_id)
del profiles[deletion_name]
storage.save_persons(profiles, people_file)
print("Person deleted successfully.")
else:
print("Name not recognized. Please try again.")
else:
print("Invalid choice. Please run the script again and choose a valid option.")
if __name__ == "__main__":
main()