Spaces:
No application file
No application file
Delete app.py
Browse files
app.py
DELETED
@@ -1,73 +0,0 @@
|
|
1 |
-
|
2 |
-
import os # Import the os module
|
3 |
-
import torch
|
4 |
-
from facenet_pytorch import InceptionResnetV1, MTCNN
|
5 |
-
from PIL import Image
|
6 |
-
from torchvision import transforms
|
7 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
8 |
-
import gradio as gr
|
9 |
-
|
10 |
-
# Load pre-trained FaceNet model
|
11 |
-
facenet_model = InceptionResnetV1(pretrained='vggface2').eval()
|
12 |
-
|
13 |
-
# Load MTCNN for face detection and alignment
|
14 |
-
mtcnn = MTCNN(keep_all=True, device='cuda' if torch.cuda.is_available() else 'cpu')
|
15 |
-
|
16 |
-
# Preprocessing function for FaceNet with face alignment
|
17 |
-
def preprocess_image_facenet(img):
|
18 |
-
img = Image.fromarray(img).convert('RGB')
|
19 |
-
img_cropped = mtcnn(img)
|
20 |
-
if img_cropped is not None:
|
21 |
-
img_cropped = img_cropped[0] # Take the first face detected
|
22 |
-
transform = transforms.Compose([
|
23 |
-
transforms.Resize((160, 160)),
|
24 |
-
transforms.ToTensor(),
|
25 |
-
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
26 |
-
])
|
27 |
-
img_tensor = transform(img_cropped).unsqueeze(0)
|
28 |
-
return img_tensor
|
29 |
-
else:
|
30 |
-
return None
|
31 |
-
|
32 |
-
# Register employee images for FaceNet
|
33 |
-
def register_images_facenet(model, image_dir):
|
34 |
-
embeddings = {}
|
35 |
-
for filename in os.listdir(image_dir):
|
36 |
-
if filename.endswith('.jpg') or filename.endswith('.png'):
|
37 |
-
img_path = os.path.join(image_dir, filename)
|
38 |
-
img_tensor = preprocess_image_facenet(img_path)
|
39 |
-
if img_tensor is not None:
|
40 |
-
with torch.no_grad():
|
41 |
-
embedding = model(img_tensor).numpy()
|
42 |
-
embeddings[filename.split('.')[0]] = embedding
|
43 |
-
return embeddings
|
44 |
-
|
45 |
-
# Load employee images and register them
|
46 |
-
employee_images_dir = 'employees_images' # Ensure this directory is in the same directory as app.py
|
47 |
-
facenet_embeddings = register_images_facenet(facenet_model, employee_images_dir)
|
48 |
-
|
49 |
-
# Identify image function
|
50 |
-
def identify_image_facenet(model, embeddings, img, threshold=0.5):
|
51 |
-
img_tensor = preprocess_image_facenet(img)
|
52 |
-
if img_tensor is not None:
|
53 |
-
with torch.no_grad():
|
54 |
-
embedding = model(img_tensor).numpy()
|
55 |
-
max_similarity = 0
|
56 |
-
identified_person = None
|
57 |
-
for name, registered_embedding in embeddings.items():
|
58 |
-
similarity = cosine_similarity(embedding, registered_embedding)
|
59 |
-
if similarity > max_similarity:
|
60 |
-
max_similarity = similarity
|
61 |
-
identified_person = name
|
62 |
-
if max_similarity >= threshold:
|
63 |
-
return f"Identified as {identified_person} with similarity {max_similarity[0][0]:.2f}"
|
64 |
-
else:
|
65 |
-
return "No match found"
|
66 |
-
else:
|
67 |
-
return "No face detected"
|
68 |
-
|
69 |
-
# Gradio interface
|
70 |
-
facenet_interface = gr.Interface(fn=identify_image_facenet, inputs="image", outputs="text", title="FaceNet Verification with Threshold 0.5")
|
71 |
-
|
72 |
-
if _name_ == "_main_":
|
73 |
-
facenet_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|