Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 175 additions & 143 deletions python/Face/FaceQuickstart-single.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
# <snippet_single>
import asyncio
import io
import os
import sys
import time
import uuid
import requests
from urllib.parse import urlparse
from io import BytesIO
# To install this module, run:
# python -m pip install Pillow
from PIL import Image, ImageDraw
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person, QualityForRecognition

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,
)


# This key will serve all examples in this document.
Expand All @@ -22,139 +20,173 @@
# This endpoint will be used in all examples in this quickstart.
ENDPOINT = os.environ["VISION_ENDPOINT"]

# Base url for the Verify and Facelist/Large Facelist operations
IMAGE_BASE_URL = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/'

# Used in the Person Group Operations and Delete Person Group examples.
# You can call list_person_groups to print a list of preexisting PersonGroups.
# SOURCE_PERSON_GROUP_ID should be all lowercase and alphanumeric. For example, 'mygroupname' (dashes are OK).
PERSON_GROUP_ID = str(uuid.uuid4()) # assign a random ID (or name it anything)

# Used for the Delete Person Group example.
TARGET_PERSON_GROUP_ID = str(uuid.uuid4()) # assign a random ID (or name it anything)

# Create an authenticated FaceClient.
face_client = FaceClient(ENDPOINT, CognitiveServicesCredentials(KEY))

'''
Create the PersonGroup
'''
# Create empty Person Group. Person Group ID must be lower case, alphanumeric, and/or with '-', '_'.
print('Person group:', PERSON_GROUP_ID)
face_client.person_group.create(person_group_id=PERSON_GROUP_ID, name=PERSON_GROUP_ID, recognition_model='recognition_04')

# Define woman friend
woman = face_client.person_group_person.create(PERSON_GROUP_ID, name="Woman")
# Define man friend
man = face_client.person_group_person.create(PERSON_GROUP_ID, name="Man")
# Define child friend
child = face_client.person_group_person.create(PERSON_GROUP_ID, name="Child")

'''
Detect faces and register them to each person
'''
# Find all jpeg images of friends in working directory (TBD pull from web instead)
woman_images = ["https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Mom1.jpg", "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Mom2.jpg"]
man_images = ["https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Dad1.jpg", "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Dad2.jpg"]
child_images = ["https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Son1.jpg", "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Son2.jpg"]

# Add to woman person
for image in woman_images:
# Check if the image is of sufficent quality for recognition.
sufficientQuality = True
detected_faces = face_client.face.detect_with_url(url=image, detection_model='detection_03', recognition_model='recognition_04', return_face_attributes=['qualityForRecognition'])
for face in detected_faces:
if face.face_attributes.quality_for_recognition != QualityForRecognition.high:
sufficientQuality = False
break
face_client.person_group_person.add_face_from_url(PERSON_GROUP_ID, woman.person_id, image)
print("face {} added to person {}".format(face.face_id, woman.person_id))

if not sufficientQuality: continue

# Add to man person
for image in man_images:
# Check if the image is of sufficent quality for recognition.
sufficientQuality = True
detected_faces = face_client.face.detect_with_url(url=image, detection_model='detection_03', recognition_model='recognition_04', return_face_attributes=['qualityForRecognition'])
for face in detected_faces:
if face.face_attributes.quality_for_recognition != QualityForRecognition.high:
sufficientQuality = False
break
face_client.person_group_person.add_face_from_url(PERSON_GROUP_ID, man.person_id, image)
print("face {} added to person {}".format(face.face_id, man.person_id))

if not sufficientQuality: continue

# Add to child person
for image in child_images:
# Check if the image is of sufficent quality for recognition.
sufficientQuality = True
detected_faces = face_client.face.detect_with_url(url=image, detection_model='detection_03', recognition_model='recognition_04', return_face_attributes=['qualityForRecognition'])
for face in detected_faces:
if face.face_attributes.quality_for_recognition != QualityForRecognition.high:
sufficientQuality = False
print("{} has insufficient quality".format(face))
break
face_client.person_group_person.add_face_from_url(PERSON_GROUP_ID, child.person_id, image)
print("face {} added to person {}".format(face.face_id, child.person_id))
if not sufficientQuality: continue


'''
Train PersonGroup
'''
# Train the person group
print("pg resource is {}".format(PERSON_GROUP_ID))
rawresponse = face_client.person_group.train(PERSON_GROUP_ID, raw= True)
print(rawresponse)

while (True):
training_status = face_client.person_group.get_training_status(PERSON_GROUP_ID)
print("Training status: {}.".format(training_status.status))
PERSON_GROUP_ID = str(uuid.uuid4()) # assign a random ID (or name it anything)

# Create an authenticated FaceAdministrationClient and FaceClient.
# FaceAdministrationClient is used for create, update, get, or delete Person Group and Person. Also it can add or
# remove face from a Person. FaceClient is for calling detect, identify and verify.
with FaceAdministrationClient(endpoint=ENDPOINT, credential=AzureKeyCredential(KEY)) as face_admin_client, \
FaceClient(endpoint=ENDPOINT, credential=AzureKeyCredential(KEY)) as face_client:
'''
Create the PersonGroup
'''
# Create empty Person Group. Person Group ID must be lower case, alphanumeric, and/or with '-', '_'.
print('Person group:', PERSON_GROUP_ID)
face_admin_client.create_person_group(
person_group_id=PERSON_GROUP_ID, name=PERSON_GROUP_ID, recognition_model=FaceRecognitionModel.RECOGNITION_04)

# Define woman friend
woman = face_admin_client.create_person_group_person(PERSON_GROUP_ID, name="Woman")
# Define man friend
man = face_admin_client.create_person_group_person(PERSON_GROUP_ID, name="Man")
# Define child friend
child = face_admin_client.create_person_group_person(PERSON_GROUP_ID, name="Child")

'''
Detect faces and register them to each person
'''
# Find all jpeg images of friends in working directory (TBD pull from web instead)
woman_images = [
"https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Mom1.jpg", # noqa: E501
"https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Mom2.jpg", # noqa: E501
]
man_images = [
"https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Dad1.jpg", # noqa: E501
"https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Dad2.jpg", # noqa: E501
]
child_images = [
"https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Son1.jpg", # noqa: E501
"https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/Family1-Son2.jpg", # noqa: E501
]

# Add to woman person
for image in woman_images:
# Check if the image is of sufficent quality for recognition.
sufficientQuality = True
detected_faces = face_client.detect_from_url(
url=image,
detection_model=FaceDetectionModel.DETECTION_03,
recognition_model=FaceRecognitionModel.RECOGNITION_04,
return_face_id=True,
return_face_attributes=[FaceAttributeTypeRecognition04.QUALITY_FOR_RECOGNITION])
for face in detected_faces:
if face.face_attributes.quality_for_recognition != QualityForRecognition.HIGH:
sufficientQuality = False
break
face_admin_client.add_person_group_person_face_from_url(PERSON_GROUP_ID, woman.person_id, url=image)
print(f"face {face.face_id} added to person {woman.person_id}")

if not sufficientQuality:
continue

# Add to man person
for image in man_images:
# Check if the image is of sufficent quality for recognition.
sufficientQuality = True
detected_faces = face_client.detect_from_url(
url=image,
detection_model=FaceDetectionModel.DETECTION_03,
recognition_model=FaceRecognitionModel.RECOGNITION_04,
return_face_id=True,
return_face_attributes=[FaceAttributeTypeRecognition04.QUALITY_FOR_RECOGNITION])
for face in detected_faces:
if face.face_attributes.quality_for_recognition != QualityForRecognition.HIGH:
sufficientQuality = False
break
face_admin_client.add_person_group_person_face_from_url(PERSON_GROUP_ID, man.person_id, url=image)
print(f"face {face.face_id} added to person {man.person_id}")

if not sufficientQuality:
continue

# Add to child person
for image in child_images:
# Check if the image is of sufficent quality for recognition.
sufficientQuality = True
detected_faces = face_client.detect_from_url(
url=image,
detection_model=FaceDetectionModel.DETECTION_03,
recognition_model=FaceRecognitionModel.RECOGNITION_04,
return_face_id=True,
return_face_attributes=[FaceAttributeTypeRecognition04.QUALITY_FOR_RECOGNITION])
for face in detected_faces:
if face.face_attributes.quality_for_recognition != QualityForRecognition.HIGH:
sufficientQuality = False
print("{} has insufficient quality".format(face))
break
face_admin_client.add_person_group_person_face_from_url(PERSON_GROUP_ID, child.person_id, url=image)
print(f"face {face.face_id} added to person {child.person_id}")

if not sufficientQuality:
continue

'''
Train PersonGroup
'''
# Train the person group
print(f"Train the person group {PERSON_GROUP_ID}")
try:
poller = face_admin_client.begin_train_person_group(PERSON_GROUP_ID, polling_interval=30)
poller.wait() # Keep polling until the "Train" operation completes.
except Exception as error:
face_admin_client.delete_person_group(person_group_id=PERSON_GROUP_ID)
sys.exit(f"Training the person group has failed. Error: {error}")
print(f"The person group {PERSON_GROUP_ID} is trained successfully.")

'''
Identify a face against a defined PersonGroup
'''
# Group image for testing against
test_image = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/identification1.jpg" # noqa: E501

print("Pausing for 10 seconds to avoid triggering rate limit on free account...")
time.sleep(10)

# Detect faces
face_ids = []
# We use detection model 03 to get better performance, recognition model 04 to support quality for
# recognition attribute.
faces = face_client.detect_from_url(
url=test_image,
detection_model=FaceDetectionModel.DETECTION_03,
recognition_model=FaceRecognitionModel.RECOGNITION_04,
return_face_id=True,
return_face_attributes=[FaceAttributeTypeRecognition04.QUALITY_FOR_RECOGNITION])
for face in faces:
# Only take the face if it is of sufficient quality.
if face.face_attributes.quality_for_recognition == QualityForRecognition.HIGH or \
face.face_attributes.quality_for_recognition == QualityForRecognition.MEDIUM:
face_ids.append(face.face_id)

# Identify faces
results = face_client.identify_from_person_group(face_ids=face_ids, person_group_id=PERSON_GROUP_ID)
print("Identifying faces in image")
if not results:
print("No person identified in the person group")
for identifiedFace in results:
if len(identifiedFace.candidates) > 0:
print(f"Person is identified for face ID {identifiedFace.face_id} in image, with a confidence of "
f"{identifiedFace.candidates[0].confidence}.") # Get topmost confidence score

# Verify faces
verify_result = face_client.verify_from_person_group(
face_id=identifiedFace.face_id,
person_group_id=PERSON_GROUP_ID,
person_id=identifiedFace.candidates[0].person_id)
print(f"verification result: {verify_result.is_identical}. confidence: {verify_result.confidence}")
else:
print(f"No person identified for face ID {identifiedFace.face_id} in image.")

print()
if (training_status.status is TrainingStatusType.succeeded):
break
elif (training_status.status is TrainingStatusType.failed):
face_client.person_group.delete(person_group_id=PERSON_GROUP_ID)
sys.exit('Training the person group has failed.')
time.sleep(5)

'''
Identify a face against a defined PersonGroup
'''
# Group image for testing against
test_image = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/Face/images/identification1.jpg"

print('Pausing for 10 seconds to avoid triggering rate limit on free account...')
time.sleep (10)

# Detect faces
face_ids = []
# We use detection model 3 to get better performance, recognition model 4 to support quality for recognition attribute.
faces = face_client.face.detect_with_url(test_image, detection_model='detection_03', recognition_model='recognition_04', return_face_attributes=['qualityForRecognition'])
for face in faces:
# Only take the face if it is of sufficient quality.
if face.face_attributes.quality_for_recognition == QualityForRecognition.high or face.face_attributes.quality_for_recognition == QualityForRecognition.medium:
face_ids.append(face.face_id)

# Identify faces
results = face_client.face.identify(face_ids, PERSON_GROUP_ID)
print('Identifying faces in image')
if not results:
print('No person identified in the person group')
for identifiedFace in results:
if len(identifiedFace.candidates) > 0:
print('Person is identified for face ID {} in image, with a confidence of {}.'.format(identifiedFace.face_id, identifiedFace.candidates[0].confidence)) # Get topmost confidence score

# Verify faces
verify_result = face_client.face.verify_face_to_person(identifiedFace.face_id, identifiedFace.candidates[0].person_id, PERSON_GROUP_ID)
print('verification result: {}. confidence: {}'.format(verify_result.is_identical, verify_result.confidence))
else:
print('No person identified for face ID {} in image.'.format(identifiedFace.face_id))


print()
print('End of quickstart.')

# </snippet_single>

# Delete the person group
face_admin_client.delete_person_group(person_group_id=PERSON_GROUP_ID)
print(f"The person group {PERSON_GROUP_ID} is deleted.")

print()
print("End of quickstart.")

# </snippet_single>
28 changes: 11 additions & 17 deletions python/Face/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
# Face quickstart and examples

This quickstart and example use the [Face SDK](https://docs.microsoft.com/en-us/python/api/azure-cognitiveservices-vision-face/?view=azure-python) for Cognitive Services.
This quickstart and example use the [Azure Ai Vision Face SDK](https://learn.microsoft.com/en-us/python/api/overview/azure/ai-vision-face-readme?view=azure-python).

## Prerequisites
- Python 3+
- Install Face SDK: pip install azure-cognitiveservices-vision-face

### Images
- To your root folder, add all images downloaded mentioned in the quickstart.
- For the DetectIdentifyFace example, download all "woman" and "child" images.

https://github.com/Azure-examples/cognitive-services-sample-data-files/tree/master/Face/images

- Python 3.8+
- Install Face SDK: python -m pip install azure-ai-vision-face
- Please refer to [SDK source code](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/face/azure-ai-vision-face) to get more information.

## Run
- Run from your favorite IDE, or using this from the command line:

python FaceQuickstart.py
python DetectIdentifyFace.py


python FaceQuickstart-single.py

## References
- Documentation: https://docs.microsoft.com/en-us/azure/cognitive-services/face/
- SDK: https://docs.microsoft.com/en-us/python/api/azure-cognitiveservices-vision-face/azure.cognitiveservices.vision.face?view=azure-python
- All Face APIs: https://docs.microsoft.com/en-us/azure/cognitive-services/face/APIReference
- Documentation: https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/overview-identity
- SDK: https://learn.microsoft.com/en-us/python/api/overview/azure/ai-vision-face-readme?view=azure-python
- All Face APIs: https://learn.microsoft.com/en-us/azure/ai-services/computer-vision/identity-api-reference
Loading