Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,95 +1,53 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 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 |
-
input_folder = "C:\Users\nikit\OneDrive\Desktop\New folder (2)"
|
| 55 |
-
|
| 56 |
-
# Function to compare images and find matches
|
| 57 |
-
def compare_images(salesforce_records, input_folder):
|
| 58 |
-
for image_file in os.listdir(input_folder):
|
| 59 |
-
input_image_path = os.path.join(input_folder, image_file)
|
| 60 |
-
|
| 61 |
-
# Load the input image
|
| 62 |
-
input_image = face_recognition.load_image_file(input_image_path)
|
| 63 |
-
input_encoding = face_recognition.face_encodings(input_image)
|
| 64 |
-
|
| 65 |
-
if len(input_encoding) == 0:
|
| 66 |
-
print(f"No face found in {image_file}")
|
| 67 |
-
continue
|
| 68 |
-
|
| 69 |
-
input_encoding = input_encoding[0]
|
| 70 |
-
|
| 71 |
-
found_match = False
|
| 72 |
-
|
| 73 |
-
# Compare input image to Salesforce records
|
| 74 |
-
for record in salesforce_records:
|
| 75 |
-
record_photo_url = record['PhotoField__c']
|
| 76 |
-
|
| 77 |
-
# Load Salesforce image (you can download and use images from Salesforce as needed)
|
| 78 |
-
# Here, assuming the photos are locally available
|
| 79 |
-
salesforce_image = face_recognition.load_image_file(record_photo_url)
|
| 80 |
-
salesforce_encoding = face_recognition.face_encodings(salesforce_image)[0]
|
| 81 |
-
|
| 82 |
-
# Compare the encodings
|
| 83 |
-
matches = face_recognition.compare_faces([salesforce_encoding], input_encoding)
|
| 84 |
-
|
| 85 |
-
if True in matches:
|
| 86 |
-
print(f"Match found! Input image matches {record['Name']}")
|
| 87 |
-
found_match = True
|
| 88 |
-
break
|
| 89 |
-
|
| 90 |
-
if not found_match:
|
| 91 |
-
print(f"Unknown face in {image_file}")
|
| 92 |
-
send_email(input_image_path)
|
| 93 |
-
|
| 94 |
-
# Run the comparison function
|
| 95 |
-
compare_images(salesforce_records, input_folder)
|
|
|
|
| 1 |
import os
|
| 2 |
+
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load pre-trained model and tokenizer
|
| 7 |
+
model_name = "deepface/face-recognition"
|
| 8 |
+
model = AutoModel.from_pretrained(model_name)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Define folders
|
| 12 |
+
folder1 = "C:/Users/nikit/OneDrive/esktop/New folder (2)"
|
| 13 |
+
folder2 = "C:/Users/nikit/OneDrive/Desktop/New folder"
|
| 14 |
+
|
| 15 |
+
# Define threshold for matching (e.g., 0.5)
|
| 16 |
+
threshold = 0.5
|
| 17 |
+
|
| 18 |
+
# Function to extract face embeddings
|
| 19 |
+
def extract_embeddings(folder):
|
| 20 |
+
embeddings = []
|
| 21 |
+
for file in os.listdir(folder):
|
| 22 |
+
image = Image.open(os.path.join(folder, file))
|
| 23 |
+
inputs = tokenizer(images=image, return_tensors='pt')
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
embeddings.append(outputs.last_hidden_state[:, 0, :].detach().numpy())
|
| 26 |
+
return embeddings
|
| 27 |
+
|
| 28 |
+
# Extract embeddings from both folders
|
| 29 |
+
embeddings1 = extract_embeddings(folder1)
|
| 30 |
+
embeddings2 = extract_embeddings(folder2)
|
| 31 |
+
|
| 32 |
+
# Compare embeddings
|
| 33 |
+
matches = []
|
| 34 |
+
for embedding1 in embeddings1:
|
| 35 |
+
for embedding2 in embeddings2:
|
| 36 |
+
similarity = torch.cosine_similarity(torch.tensor(embedding1), torch.tensor(embedding2))
|
| 37 |
+
if similarity > threshold:
|
| 38 |
+
matches.append((embedding1, embedding2))
|
| 39 |
+
|
| 40 |
+
# Print matches
|
| 41 |
+
for match in matches:
|
| 42 |
+
print(f"Match found: {match[0]} (Folder 1) and {match[1]} (Folder 2)")
|
| 43 |
+
|
| 44 |
+
# Print non-matches
|
| 45 |
+
non_matches = []
|
| 46 |
+
for embedding1 in embeddings1:
|
| 47 |
+
for embedding2 in embeddings2:
|
| 48 |
+
similarity = torch.cosine_similarity(torch.tensor(embedding1), torch.tensor(embedding2))
|
| 49 |
+
if similarity < threshold:
|
| 50 |
+
non_matches.append((embedding1, embedding2))
|
| 51 |
+
|
| 52 |
+
for non_match in non_matches:
|
| 53 |
+
print(f"No match found: {non_match[0]} (Folder 1) and {non_match[1]} (Folder 2)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|