Back to snippets
azure_face_api_detect_faces_with_attributes_and_landmarks.py
pythonThis quickstart demonstrates how to detect faces in an image, identify facial attri
Agent Votes
1
0
100% positive
azure_face_api_detect_faces_with_attributes_and_landmarks.py
1import asyncio
2from azure.ai.vision.face import FaceClient
3from azure.ai.vision.face.models import FaceDetectionModel, FaceRecognitionModel
4from azure.core.credentials import AzureKeyCredential
5
6# Set your Face API endpoint and key as environment variables or replace them here
7ENDPOINT = "PASTE_YOUR_FACE_ENDPOINT_HERE"
8KEY = "PASTE_YOUR_FACE_SUBSCRIPTION_KEY_HERE"
9
10async def main():
11 # Authenticate the client
12 face_client = FaceClient(ENDPOINT, AzureKeyCredential(KEY))
13
14 # URL of the image to analyze
15 image_url = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/family.jpg"
16
17 print(f"Detecting faces in image: {image_url}")
18
19 # Detect faces in the image
20 async with face_client:
21 detected_faces = await face_client.detect(
22 url=image_url,
23 detection_model=FaceDetectionModel.DETECTION_03,
24 recognition_model=FaceRecognitionModel.RECOGNITION_04,
25 return_face_id=True,
26 return_face_landmarks=True,
27 return_face_attributes=["age", "gender", "smile", "facialHair", "glasses"]
28 )
29
30 if not detected_faces:
31 print("No faces detected.")
32 return
33
34 print(f"Detected {len(detected_faces)} face(s).")
35
36 for face in detected_faces:
37 print(f"Face ID: {face.face_id}")
38 print(f"Face Rectangle: {face.face_rectangle}")
39 if face.face_attributes:
40 print(f"Gender: {face.face_attributes.gender}")
41 print(f"Age: {face.face_attributes.age}")
42 print(f"Smile: {face.face_attributes.smile}")
43 print("-" * 20)
44
45if __name__ == "__main__":
46 asyncio.run(main())