In the post-pandemic world, many organizations require their staff or visitors to wear masks in addition to verifying identity for attendance. This blog post explains how to build a simple Face Recognition Attendance System with Face Mask Detection using Azure AI Face API.
With Azure AI Face, we can:
- Detect faces in an image.
- Check whether a person is wearing a mask (and if nose and mouth are covered).
- Identify if a detected face is in our known list of authorized persons.
- Mark attendance only if the person is recognized and wearing a mask.
This blog includes two main code snippets:
- Recognizing a face and checking if it belongs to a trained group of authorized persons.
- Detecting whether a person is wearing a mask and marking attendance.
Prerequisites
-
Azure Subscription
- You need an active Azure subscription to create an Azure Cognitive Services resource with the Face API.
-
Python 3.7+
- Ensure Python 3.7 (or higher) is installed on your system.
-
Azure AI Vision (Face) SDK
- Install via:
pip install azure-ai-vision
-
Face API Key & Endpoint
- After creating a Face resource in Azure Portal, retrieve:
-
Endpoint (e.g.,
https://<resource-name>.cognitiveservices.azure.com/
) - API Key (a 32-character key)
-
Endpoint (e.g.,
- After creating a Face resource in Azure Portal, retrieve:
Architecture at a Glance
-
Large Person Group (LPG):
- You create a Large Person Group to store known persons (e.g., employees, students).
- Train the LPG so it can recognize these faces later.
-
Detect & Identify:
- When someone attempts to mark attendance, the system uploads a photo.
- Azure Face detects faces, returns attributes including Face ID, Mask properties, and (optionally) the quality for recognition.
- If recognized above a certain confidence threshold and wearing a mask, the system marks attendance.
-
Real-world Enhancements:
- Use a camera feed to capture images in real time.
- Integrate with your existing attendance software or a database.
Code Snippet #1: Door Access Control With Face Recognition
This snippet shows how to detect a face and check if it’s recognized in a previously trained Large Person Group. If recognized, we simulate opening a door by printing a welcome message. Otherwise, we deny access.
File Name:
door_access_control_local_upload.py
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.vision.face import FaceAdministrationClient, FaceClient
from azure.ai.vision.face.models import (
FaceAttributeTypeRecognition04,
FaceDetectionModel,
FaceRecognitionModel,
QualityForRecognition
)
FACE_API_KEY = os.environ.get("FACE_APIKEY") # Or replace with your API key as a string
FACE_ENDPOINT = os.environ.get("FACE_ENDPOINT") # Or replace with your endpoint
# Must match the ID you used when creating/training the Large Person Group
LARGE_PERSON_GROUP_ID = "my-lpg-local-upload"
def check_access(face_client, face_id, confidence_threshold=0.7):
"""
Identify the face_id against the Large Person Group.
Return (True, person_id) if recognized >= threshold, else (False, None).
"""
identify_results = face_client.identify_from_large_person_group(
face_ids=[face_id],
large_person_group_id=LARGE_PERSON_GROUP_ID
)
if not identify_results:
return (False, None)
result = identify_results[0]
if not result.candidates:
return (False, None)
top_candidate = result.candidates[0]
if top_candidate.confidence >= confidence_threshold:
return (True, top_candidate.person_id)
return (False, None)
def open_door_simulation(person_name):
"""
In a real system, trigger door unlock.
Here, just print a message for demonstration.
"""
print(f"[ACCESS GRANTED] Welcome, {person_name}! The door is now unlocked.")
def deny_access_simulation():
"""Simulate denying access."""
print("[ACCESS DENIED] Unrecognized or unauthorized individual.")
def main():
print("Door Access Control (Local Photo Upload)")
# Create clients
face_client = FaceClient(
endpoint=FACE_ENDPOINT,
credential=AzureKeyCredential(FACE_API_KEY)
)
face_admin_client = FaceAdministrationClient(
endpoint=FACE_ENDPOINT,
credential=AzureKeyCredential(FACE_API_KEY)
)
# Provide path to the image you want to check
image_path = "path/to/test_image.jpg"
if not os.path.exists(image_path):
print(f"Error: File not found => {image_path}")
return
with open(image_path, "rb") as f:
img_bytes = f.read()
try:
# DETECT face
detected_faces = face_client.detect(
image=img_bytes,
detection_model=FaceDetectionModel.DETECTION03,
recognition_model=FaceRecognitionModel.RECOGNITION04,
return_face_id=True,
return_face_attributes=[FaceAttributeTypeRecognition04.QUALITY_FOR_RECOGNITION]
)
if not detected_faces:
print("No faces detected in the image. Access denied.")
return
# For simplicity, just use the first face
face = detected_faces[0]
if face.face_attributes.quality_for_recognition == QualityForRecognition.LOW:
print("Face quality too low for recognition. Access denied.")
return
recognized, person_id = check_access(face_client, face.face_id)
if recognized and person_id:
# Retrieve the person's details to say hello by name
person_info = face_admin_client.large_person_group.get_person(
large_person_group_id=LARGE_PERSON_GROUP_ID,
person_id=person_id
)
# Greet by name
open_door_simulation(person_info.name)
else:
deny_access_simulation()
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()
How It Works
- Detection includes both MASK and QUALITY_FOR_RECOGNITION attributes.
- We ensure nose_and_mouth_covered == True before proceeding to recognition.
- If recognized (confidence >= 0.7), we print a success message. Otherwise, we deny attendance.
Real-Time Webcam Detection
For continuous detection (like an entry gate or reception area), you can capture frames from your webcam using OpenCV, then call Azure Face to check mask and recognition in real time.
File Name: realtime_webcam_detection.py
import os
import cv2
import time
from azure.core.credentials import AzureKeyCredential
from azure.ai.vision.face import FaceClient
from azure.ai.vision.face.models import (
FaceDetectionModel,
FaceRecognitionModel,
FaceAttributeTypeDetection03,
FaceAttributeTypeRecognition04,
QualityForRecognition
)
# Replace with your actual values or environment variables
FACE_API_KEY = os.environ.get("FACE_APIKEY") or "<YourFaceAPIKey>"
FACE_ENDPOINT = os.environ.get("FACE_ENDPOINT") or "<YourFaceAPIEndpoint>"
# Must match your trained Large Person Group
LARGE_PERSON_GROUP_ID = "my-lpg-local-upload"
def identify_person(face_client, face_id, threshold=0.7):
"""Identify the face in the LPG. Returns (True, person_id) if recognized."""
results = face_client.identify_from_large_person_group(
face_ids=[face_id],
large_person_group_id=LARGE_PERSON_GROUP_ID
)
if not results or not results[0].candidates:
return (False, None)
candidate = results[0].candidates[0]
return (candidate.confidence >= threshold, candidate.person_id)
def main():
print("[INFO] Starting Real-Time Face Mask & Recognition Detection")
# Create FaceClient
face_client = FaceClient(
endpoint=FACE_ENDPOINT,
credential=AzureKeyCredential(FACE_API_KEY)
)
# Open the default webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("[ERROR] Could not open webcam.")
return
try:
while True:
ret, frame = cap.read()
if not ret:
print("[ERROR] Failed to read frame from webcam.")
break
# Encode current frame to JPEG
_, img_encoded = cv2.imencode('.jpg', frame)
img_bytes = img_encoded.tobytes()
# Detect faces
try:
faces = face_client.detect(
image=img_bytes,
detection_model=FaceDetectionModel.DETECTION03,
recognition_model=FaceRecognitionModel.RECOGNITION04,
return_face_id=True,
return_face_attributes=[
FaceAttributeTypeDetection03.MASK,
FaceAttributeTypeRecognition04.QUALITY_FOR_RECOGNITION
]
)
except Exception as e:
print(f"[ERROR] Azure Face detection error: {e}")
continue
# For each face, check mask & recognition
for face in faces:
# Draw bounding box
rect = face.face_rectangle
top, left, width, height = rect.top, rect.left, rect.width, rect.height
cv2.rectangle(frame, (left, top), (left + width, top + height), (0, 255, 0), 2)
# Default label
msg = "No Mask Detected"
# Check mask coverage
mask_data = face.face_attributes.mask
if mask_data and mask_data.nose_and_mouth_covered:
# Check face quality
if face.face_attributes.quality_for_recognition != QualityForRecognition.LOW:
recognized, person_id = identify_person(face_client, face.face_id)
if recognized:
msg = "Recognized + Masked"
# If you want the person's actual name:
# (You’d need the FaceAdministrationClient here)
# person_info = face_admin_client.large_person_group.get_person(
# LARGE_PERSON_GROUP_ID, person_id
# )
# msg = f"{person_info.name} + Masked"
else:
msg = "Unknown, Masked"
else:
msg = "Low Quality Face"
else:
msg = "No/Improper Mask"
# Put text on frame
cv2.putText(frame, msg, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 255), 2)
# Show the result
cv2.imshow('Real-Time Mask & Recognition', frame)
# Press 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
time.sleep(0.3) # Slight delay to reduce API calls
except KeyboardInterrupt:
print("[INFO] Interrupted by user.")
finally:
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
How It Works
- OpenCV obtains frames from the webcam.
- Azure Face detection includes mask and quality attributes.
- Bounding Boxes: We draw rectangles and text in the video.
- If the face is recognized and masked, we label them "Recognized + Masked". Otherwise, we label them "Unknown, Masked" or "No/Improper Mask".
Integrating with an Attendance Database
In production, you would:
- Log recognized individuals in a database with timestamps (e.g., SQL, Cosmos DB).
- Trigger access control systems if recognized and masked.
- Handle notifications or custom logic if an unrecognized or unmasked person is detected.
Troubleshooting & Best Practices
Confidence Threshold
Adjust 0.7 to balance false accepts vs. false rejects. In higher-security scenarios, go higher (e.g., 0.8).
Low-Light Conditions
Poor lighting may lower face detection accuracy and quality. Provide good ambient light or an IR camera in low-light environments.
API Rate Limits
The free tier has certain call-per-minute limits. If you’re capturing 30 frames per second, you’ll exceed it quickly!
Use throttling (time.sleep(...)) or a higher service tier.
Privacy & Security
Face recognition is subject to data protection laws (GDPR, HIPAA, etc.).
Store face data securely and keep your API Key safe (e.g., Azure Key Vault).
Region Support for Mask
Check the Azure docs to ensure your region supports the mask feature.
Conclusion
By combining Azure AI Face and OpenCV, you can build a robust attendance system that verifies both identity and mask compliance. This meets modern health guidelines, is touchless, and can be extended to real-time logging or controlling physical access points.
Next Steps:
- Improve multi-person handling, logging, and auditing.
- Integrate with hardware for an actual door lock system or turnstile.
- Explore offline or on-edge solutions if you require higher throughput or internet-independent operation.
Happy Building!
Top comments (0)