Ajay98 commited on
Commit
0268cf4
·
verified ·
1 Parent(s): cb50e70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -94
app.py CHANGED
@@ -1,95 +1,53 @@
1
  import os
2
- import face_recognition
3
- import cv2
4
- import smtplib
5
- from simple_salesforce import Salesforce
6
- from email.mime.text import MIMEText
7
- from email.mime.multipart import MIMEMultipart
8
-
9
- # Salesforce credentials
10
- USERNAME = 'ajayalways411@gmail.com'
11
- PASSWORD = 'Anusha@310819'
12
- SECURITY_TOKEN = 'DcroeUKt0Lc4Lpqgzh9dMxEyQ'
13
-
14
- # Connect to Salesforce
15
- sf = Salesforce(username=USERNAME, password=PASSWORD, security_token=SECURITY_TOKEN)
16
-
17
- # Query records from two custom objects (with image fields)
18
- custom_object_1 = 'CustomerData__c'
19
- custom_object_2 = 'staff__c'
20
-
21
- # Replace with actual field names
22
- query_1 = f"SELECT PhoneNumber__c, Name, AdhardCardPhoto__c FROM {custom_object_1}"
23
- query_2 = f"SELECT contactNumber__c, Name, AdhardCard__c FROM {custom_object_2}"
24
-
25
- # Retrieve records
26
- records_custom_object_1 = sf.query(query_1)['records']
27
- records_custom_object_2 = sf.query(query_2)['records']
28
-
29
- # Combine both objects' records
30
- salesforce_records = records_custom_object_1 + records_custom_object_2
31
-
32
- # Function to send an email if unknown face is found
33
- def send_email(unknown_image_path):
34
- sender_email = "aajayk999@gmail.com@gmail.com"
35
- receiver_email = "ajayaklaram07@gmail.com"
36
- password = "8008995817"
37
-
38
- message = MIMEMultipart("alternative")
39
- message["Subject"] = "Unknown Face Detected"
40
- message["From"] = sender_email
41
- message["To"] = receiver_email
42
-
43
- # Email body
44
- text = f"An unknown face was detected in the image: {unknown_image_path}"
45
- part1 = MIMEText(text, "plain")
46
- message.attach(part1)
47
-
48
- # Send email
49
- with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
50
- server.login(sender_email, password)
51
- server.sendmail(sender_email, receiver_email, message.as_string())
52
-
53
- # Folder with input images to match
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)")