|
| 1 | +import os, io, uuid, glob, time |
| 2 | +from msrest.authentication import CognitiveServicesCredentials |
| 3 | +from azure.cognitiveservices.vision.face import FaceClient |
| 4 | +from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person |
| 5 | + |
| 6 | +''' |
| 7 | +PersonGroup - Face API sample |
| 8 | +References: |
| 9 | + How-to guide: https://docs.microsoft.com/en-us/azure/cognitive-services/face/face-api-how-to-topics/howtoidentifyfacesinimage |
| 10 | + SDK: https://docs.microsoft.com/en-us/python/api/azure-cognitiveservices-vision-face/azure.cognitiveservices.vision.face?view=azure-python |
| 11 | + Sample images to download: https://github.com/Microsoft/Cognitive-Face-Windows/tree/master/Data |
| 12 | +Prerequisites: |
| 13 | + Python 3+ |
| 14 | + Install Face SDK: pip install azure-cognitiveservices-vision-face |
| 15 | +''' |
| 16 | +# Group image for testing against |
| 17 | +group_photo = 'test-image.jpg' |
| 18 | +# To add subdirectories, ex: (os.path.realpath(__file__), "images-directory", "above-images-directory") |
| 19 | +IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__))) |
| 20 | + |
| 21 | +''' |
| 22 | +Authentication |
| 23 | +''' |
| 24 | +# Replace with a valid subscription key (keeping the quotes in place). |
| 25 | +KEY = '<ADD SUBSCRIPTION KEY HERE>' |
| 26 | +# Replace westus if it's not your region |
| 27 | +BASE_URL = 'https://westus.api.cognitive.microsoft.com' |
| 28 | +face_client = FaceClient(BASE_URL, CognitiveServicesCredentials(KEY)) |
| 29 | + |
| 30 | +''' |
| 31 | +Create the PersonGroup |
| 32 | +''' |
| 33 | +# Create empty person group |
| 34 | +# person_group_id = str(uuid.uuid4()) # Uncomment to generate a random ID |
| 35 | +person_group_id = 'my-unique-person-group' |
| 36 | +print(person_group_id) |
| 37 | +face_client.person_group.create(person_group_id=person_group_id, name=person_group_id) |
| 38 | + |
| 39 | +# Define woman friend |
| 40 | +woman = face_client.person_group_person.create(person_group_id, "Woman") |
| 41 | +# Define man friend |
| 42 | +man = face_client.person_group_person.create(person_group_id, "Man") |
| 43 | +# Define child friend |
| 44 | +child = face_client.person_group_person.create(person_group_id, "Child") |
| 45 | + |
| 46 | +''' |
| 47 | +Detect faces and register to correct person |
| 48 | +''' |
| 49 | +# Find all jpeg images of friends in working directory |
| 50 | +woman_images = [file for file in glob.glob('*.jpg') if file.startswith("woman")] |
| 51 | +man_images = [file for file in glob.glob('*.jpg') if file.startswith("man")] |
| 52 | +child_images = [file for file in glob.glob('*.jpg') if file.startswith("child")] |
| 53 | + |
| 54 | +# Add to a woman person |
| 55 | +for image in woman_images: |
| 56 | + w = open(image, 'r+b') |
| 57 | + face_client.person_group_person.add_face_from_stream(person_group_id, woman.person_id, w) |
| 58 | + |
| 59 | +# Add to a man person |
| 60 | +for image in man_images: |
| 61 | + m = open(image, 'r+b') |
| 62 | + face_client.person_group_person.add_face_from_stream(person_group_id, man.person_id, m) |
| 63 | + |
| 64 | +# Add to a child person |
| 65 | +for image in child_images: |
| 66 | + ch = open(image, 'r+b') |
| 67 | + face_client.person_group_person.add_face_from_stream(person_group_id, child.person_id, ch) |
| 68 | + |
| 69 | +''' |
| 70 | +Train PersonGroup |
| 71 | +''' |
| 72 | +# Train the person group |
| 73 | +face_client.person_group.train(person_group_id) |
| 74 | +training_status = face_client.person_group.get_training_status(person_group_id) |
| 75 | +while (training_status.status == TrainingStatusType.running): |
| 76 | + print(training_status.status) |
| 77 | + if (training_status.status == TrainingStatusType.failed): |
| 78 | + raise Exception('Training failed with message {}.'.format(training_status.message)) |
| 79 | + if (training_status.status == TrainingStatusType.succeeded): |
| 80 | + print(training_status.status) |
| 81 | + break |
| 82 | + time.sleep(1) |
| 83 | + |
| 84 | +''' |
| 85 | +Identify a face against a defined PersonGroup |
| 86 | +''' |
| 87 | +# Get test image |
| 88 | +test_image_array = glob.glob(os.path.join(IMAGES_FOLDER, group_photo)) |
| 89 | +image = open(test_image_array[0], 'r+b') |
| 90 | + |
| 91 | +# Detect faces |
| 92 | +face_ids = [] |
| 93 | +faces = face_client.face.detect_with_stream(image) |
| 94 | +for face in faces: |
| 95 | + face_ids.append(face.face_id) |
| 96 | + |
| 97 | +# Identify faces |
| 98 | +results = face_client.face.identify(face_ids, person_group_id) |
| 99 | +if not results: |
| 100 | + print('No person identified in the person group for faces from the {}.'.format(os.path.basename(image.name))) |
| 101 | +for person in results: |
| 102 | + print('Person for face ID {} is identified in {} with a confidence of {}.'.format(person.face_id, os.path.basename(image.name), person.candidates[0].confidence)) # Get topmost confidence score |
| 103 | + |
| 104 | +# Once finished, since testing, delete the PersonGroup from your resource, otherwise when you create it again, it won't allow duplicate person groups. |
| 105 | +face_client.person_group.delete(person_group_id) |
0 commit comments