Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,95 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 = 'your_salesforce_username'
|
| 11 |
+
PASSWORD = 'your_salesforce_password'
|
| 12 |
+
SECURITY_TOKEN = 'your_salesforce_security_token'
|
| 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 = 'CustomObject1__c'
|
| 19 |
+
custom_object_2 = 'CustomObject2__c'
|
| 20 |
+
|
| 21 |
+
# Replace with actual field names
|
| 22 |
+
query_1 = f"SELECT Id, Name, PhotoField__c FROM {custom_object_1}"
|
| 23 |
+
query_2 = f"SELECT Id, Name, PhotoField__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 = "your_email@gmail.com"
|
| 35 |
+
receiver_email = "receiver_email@gmail.com"
|
| 36 |
+
password = "your_email_password"
|
| 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 = "path_to_input_images_folder"
|
| 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)
|