-
Notifications
You must be signed in to change notification settings - Fork 2
/
identify.py
157 lines (117 loc) · 5.06 KB
/
identify.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Import necessary libraries
from ultralytics import YOLO
from ultralytics.engine.results import Results
from deepface import DeepFace
from PIL import Image
import shutil
import os
totalKnownFaces=0
totalUnknownFaces=0
knownNames =[]
def faceRecognition(input_image):
global totalKnownFaces
global totalUnknownFaces
# Path to the directory containing cropped objects
cropped_objects_dir = "./faces/"
# Path to the directory to save unknown faces
unknown_faces_dir = "./unknown/"
# Path to the directory to save known faces
known_faces_dir = "./known/"
# Initialize a list to store the extracted names
extracted_names = []
# Check if the 'unknown' folder exists, otherwise create it
if not os.path.exists(unknown_faces_dir):
os.makedirs(unknown_faces_dir)
else:
# If the 'unknown' folder exists, clear all files and subfolders
for file_or_folder in os.listdir(unknown_faces_dir):
file_or_folder_path = os.path.join(unknown_faces_dir, file_or_folder)
if os.path.isfile(file_or_folder_path):
os.remove(file_or_folder_path)
elif os.path.isdir(file_or_folder_path):
shutil.rmtree(file_or_folder_path)
# Check if the 'known' folder exists, otherwise create it
if not os.path.exists(known_faces_dir):
os.makedirs(known_faces_dir)
else:
# If the 'known' folder exists, clear all files and subfolders
for file_or_folder in os.listdir(known_faces_dir):
file_or_folder_path = os.path.join(known_faces_dir, file_or_folder)
if os.path.isfile(file_or_folder_path):
os.remove(file_or_folder_path)
elif os.path.isdir(file_or_folder_path):
shutil.rmtree(file_or_folder_path)
# Iterate through the image files in the directory
for filename in os.listdir(cropped_objects_dir):
if filename.lower().endswith((".jpg", ".jpeg", ".png")):
img_path = os.path.join(cropped_objects_dir, filename)
model = DeepFace.find(img_path=img_path, db_path="database", enforce_detection=False, model_name="Facenet512")
# print("Model length:", len(model))
# print("Model:", model)
# Check if a face was recognized in the image
if model and len(model[0]['identity']) > 0:
# Extract the name and append it to the list
name = model[0]['identity'][0].split('/')[1]
# name = model[0]['identity'][0].split('/')[0]
print("Name:", name)
# Save the known face into the 'known' folder
known_faces_path = os.path.join(known_faces_dir, f"{name}.jpg")
totalKnownFaces+=1
knownNames.append(name)
shutil.copy(img_path, known_faces_path)
else:
# If no face is recognized, set name to 'unknown'
name = 'unknown'
print("No face detected in:", img_path)
# Save the unknown face into the 'unknown' folder
unknown_faces_path = os.path.join(unknown_faces_dir, f"{totalUnknownFaces}.jpg")
totalUnknownFaces+=1
shutil.copy(img_path, unknown_faces_path)
extracted_names.append(name)
return extracted_names
def getKnownName():
return knownNames
def setKnownName():
global knownNames
knownNames=[]
def setFacesToZero():
global totalKnownFaces
global totalUnknownFaces
totalKnownFaces=0
totalUnknownFaces=0
def getKnownFaces():
return totalKnownFaces
def getUnknownFaces():
return totalUnknownFaces
def faceExtraction(input_image, model, results):
# Load the image
image = Image.open(input_image)
detected_objects = []
if hasattr(results, 'boxes') and hasattr(results, 'names'):
for box in results.boxes.xyxy:
object_id = int(box[-1])
object_name = results.names.get(object_id)
x1, y1, x2, y2 = int(box[0]), int(box[1]), int(box[2]), int(box[3])
detected_objects.append((object_name, (x1, y1, x2, y2)))
# Create or clear the 'faces' directory
if os.path.exists("./faces"):
shutil.rmtree("./faces")
os.makedirs("./faces")
totalFaces=0
# Crop and save each detected object
for i, (object_name, (x1, y1, x2, y2)) in enumerate(detected_objects):
object_image = image.crop((x1, y1, x2, y2))
object_image.save(f"./faces/face{i}.jpg")
totalFaces+=1
return totalFaces
def faceDetection(uploaded_file):
img = Image.open(uploaded_file)
temp_image_path = "./temp_image.jpg" # Temporary path to store the converted image
img.save(temp_image_path, format="JPEG")
# Use the Ultralytics model
model = YOLO('best.pt')
results: Results = model.predict(temp_image_path)[0]
total_faces = faceExtraction(temp_image_path, model, results)
# Remove the temporary image file
os.remove(temp_image_path)
return total_faces