To trigger the cloud function when a document is created in the "Access" collection, you can use Firebase Cloud Firestore Triggers. Here's an updated version of the code that includes the triggering logic:
from google.cloud import firestore, vision
from google.cloud import functions_v1
def compare_faces(data, context):
# Get the ID of the newly created document in the "Access" collection
document_id = context.event_id
# Initialize Firestore client
db = firestore.Client()
# Get the "picUrl" field of the newly created document in the "Access" collection
doc_ref = db.collection('Access').document(document_id)
pic_url = doc_ref.get().to_dict().get('picUrl')
# Initialize the Vision API client
client = vision.ImageAnnotatorClient()
# Check all documents in the "DB" collection
db_collection = db.collection('DB').stream()
for doc in db_collection:
# Get the "imageLink" field of the DB document
image_link = doc.to_dict().get('imageLink')
# Perform facial recognition on the images
face_match = compare_faces_vision(client, pic_url, image_link)
if face_match:
# If a match is found, return the "Uid" field of the matching document in the "DB" collection
doc_id = doc.id
doc_ref.update({'status': 'found'})
return {'Uid': doc_id}
# If no match is found, set the "status" field of the newly created document in the "Access" collection to 'notFound'
doc_ref.update({'status': 'notFound'})
def compare_faces_vision(client, pic_url, image_link):
# Use the Vision API to detect faces in the provided images
image_1 = vision.Image(source=vision.ImageSource(image_uri=pic_url))
image_2 = vision.Image(source=vision.ImageSource(image_uri=image_link))
response_1 = client.face_detection(image=image_1)
response_2 = client.face_detection(image=image_2)
faces_1 = response_1.face_annotations
faces_2 = response_2.face_annotations
# Compare the first face in each image
if faces_1 and faces_2:
face_1 = faces_1[0].bounding_poly
face_2 = faces_2[0].bounding_poly
# Compare the positions of the first face in both images
confidence = calculate_overlap(face_1, face_2)
# Set a threshold value to consider a match
threshold = 0.7
return confidence >= threshold
return False
def calculate_overlap(face_1, face_2):
# Calculate the intersection area of the bounding boxes of the faces
intersection = (max(face_1.vertices[2].x, face_2.vertices[2].x) - min(face_1.vertices[0].x, face_2.vertices[0].x)) * (max(face_1.vertices[2].y, face_2.vertices[2].y) - min(face_1.vertices[0].y, face_2.vertices[0].y))
# Calculate the area of each face bounding box
area_1 = (face_1.vertices[2].x - face_1.vertices[0].x) * (face_1.vertices[2].y - face_1.vertices[0].y)
area_2 = (face_2.vertices[2].x - face_2.vertices[0].x) * (face_2.vertices[2].y - face_2.vertices[0].y)
# Calculate the overlap ratio
overlap = intersection / (area_1 + area_2 - intersection)
return overlap
# Create the cloud function deployment
def create_cloud_function():
cloud_function_client = functions_v1.CloudFunctionsServiceClient()
# Set the function attributes
location = 'projects/[YOUR_PROJECT_ID]/locations/[LOCATION]'
function_name = 'compareFaces'
entry_point = 'compare_faces'
runtime = 'python310'
event_trigger_resource = 'projects/[YOUR_PROJECT_ID]/databases/(default)/documents/Access/{id}'
event_trigger_event_type = 'providers/cloud.firestore/eventTypes/document.create'
# Construct the function source upload URL
source_archive_url = 'gs://[YOUR_BUCKET_NAME]/[YOUR_ZIP_FILE].zip'
# Configure the function source code
source_code = '''
def compare_faces(data, context):
# Your function code here
pass
'''
# Create the function
cloud_function = {
'name': cloud_function_client.cloud_function_path('[YOUR_PROJECT_ID]', location, function_name),
'description': 'Compare Faces Cloud Function',
'entry_point': entry_point,
'runtime': runtime,
'event_trigger': {
'event_type': event_trigger_event_type,
'resource': event_trigger_resource
},
'source_archive_url': source_archive_url,
'source_repository': {},
'source_upload_url': ''
}
# Create the function in the specified location
response = cloud_function_client.create_function(location=location, function=cloud_function)
print(response)
# Uncomment the line below to create the Cloud Function
#create_cloud_function()
Please replace [YOUR_PROJECT_ID], [LOCATION], [YOUR_BUCKET_NAME], and [YOUR_ZIP_FILE] with your respective values.
You can use the create_cloud_function function to create the desired Cloud Function. Uncomment the last line create_cloud_function() and run the script to create the function. After the function is created, you can trigger it by creating a new document in the "Access" collection.