Skip to content

Commit dfb7755

Browse files
author
Winona Azure
committed
Person Group sample initial commit, plus adding images
1 parent 2771a16 commit dfb7755

File tree

11 files changed

+101
-0
lines changed

11 files changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import os, io, uuid, glob
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+
19+
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images", "Face")
20+
21+
'''
22+
Authentication
23+
'''
24+
# Replace with a valid subscription key (keeping the quotes in place).
25+
KEY = 'FACE_SUBSCRIPTION_KEY'
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()) # generate random ID
35+
face_client.person_group.create(person_group_id=person_group_id, name=person_group_id)
36+
37+
# Define woman friend
38+
woman = face_client.person_group_person.create(person_group_id, "Woman")
39+
# Define man friend
40+
man = face_client.person_group_person.create(person_group_id, "Man")
41+
# Define child friend
42+
child = face_client.person_group_person.create(person_group_id, "Child")
43+
44+
'''
45+
Detect faces and register to correct person
46+
'''
47+
# Find all jpeg images of friends in working directory
48+
woman_images = [file for file in glob.glob('*.jpg') if file.startswith("woman")]
49+
man_images = [file for file in glob.glob('*.jpg') if file.startswith("man")]
50+
child_images = [file for file in glob.glob('*.jpg') if file.startswith("child")]
51+
52+
# Add to a woman person
53+
for image in woman_images:
54+
w = open(image, 'r+b')
55+
face_client.person_group_person.add_face_from_stream(person_group_id, woman.person_id, w)
56+
57+
# Add to a man person
58+
for image in man_images:
59+
m = open(image, 'r+b')
60+
face_client.person_group_person.add_face_from_stream(person_group_id, man.person_id, m)
61+
62+
# Add to a child person
63+
for image in child_images:
64+
ch = open(image, 'r+b')
65+
face_client.person_group_person.add_face_from_stream(person_group_id, child.person_id, ch)
66+
67+
'''
68+
Train PersonGroup
69+
'''
70+
# Train the person group
71+
face_client.person_group.train(person_group_id)
72+
training_status = face_client.person_group.get_training_status(person_group_id)
73+
if training_status.status == TrainingStatusType.failed:
74+
raise Exception('Training failed with message {}.'.format(training_status.message))
75+
76+
77+
'''
78+
Identify a face against a defined PersonGroup
79+
'''
80+
# Get test image
81+
test_image_array = glob.glob(os.path.join(IMAGES_FOLDER, group_photo))
82+
ti = open(test_image_array[0], 'r+b')
83+
84+
# Detect faces
85+
face_ids = []
86+
faces = face_client.face.detect_with_stream(ti)
87+
for face in faces:
88+
face_ids.append(face.face_id)
89+
90+
# Identify faces
91+
results = face_client.face.identify(face_ids, person_group_id)
92+
if not results:
93+
print('No person identified in the person group for faces from the {}.'.format(ti.name))
94+
for person in results:
95+
person_identified = face_client.person_group_person.get(person_group_id, person.candidates[0].person_id)
96+
print('Person {}\'s face is identified in {}: person ID is {}, confidence is {}.'.format(person_identified.name,
97+
ti.name,
98+
person.face_id,
99+
person.candidates[0].confidence))
100+
101+
18.1 KB
Loading
26.3 KB
Loading
26.7 KB
Loading
51.7 KB
Loading
54.9 KB
Loading
39.4 KB
Loading
79.8 KB
Loading
39.9 KB
Loading
39.9 KB
Loading

0 commit comments

Comments
 (0)