Skip to content

Commit 1d0d848

Browse files
authored
Fixed identify function, added delete function
1 parent dfb7755 commit 1d0d848

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

samples/vision/face_person_group_samples.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
'''
1616
# Group image for testing against
1717
group_photo = 'test-image.jpg'
18-
19-
IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images", "Face")
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__)))
2020

2121
'''
2222
Authentication
2323
'''
2424
# Replace with a valid subscription key (keeping the quotes in place).
25-
KEY = 'FACE_SUBSCRIPTION_KEY'
25+
KEY = 'cb2ea73e0f074a0e83e0ee50dc129f4b'
2626
# Replace westus if it's not your region
2727
BASE_URL = 'https://westus.api.cognitive.microsoft.com'
2828
face_client = FaceClient(BASE_URL, CognitiveServicesCredentials(KEY))
@@ -31,7 +31,9 @@
3131
Create the PersonGroup
3232
'''
3333
# Create empty person group
34-
person_group_id = str(uuid.uuid4()) # generate random ID
34+
# person_group_id = str(uuid.uuid4()) # Uncomment to generate a random ID
35+
person_group_id = 'mypersongroup'
36+
print(person_group_id)
3537
face_client.person_group.create(person_group_id=person_group_id, name=person_group_id)
3638

3739
# Define woman friend
@@ -70,32 +72,31 @@
7072
# Train the person group
7173
face_client.person_group.train(person_group_id)
7274
training_status = face_client.person_group.get_training_status(person_group_id)
73-
if training_status.status == TrainingStatusType.failed:
75+
if (training_status.status == TrainingStatusType.running):
76+
print(training_status.status)
77+
elif (training_status.status == TrainingStatusType.failed):
7478
raise Exception('Training failed with message {}.'.format(training_status.message))
75-
79+
print(training_status.status)
7680

7781
'''
7882
Identify a face against a defined PersonGroup
7983
'''
8084
# Get test image
8185
test_image_array = glob.glob(os.path.join(IMAGES_FOLDER, group_photo))
82-
ti = open(test_image_array[0], 'r+b')
86+
image = open(test_image_array[0], 'r+b')
8387

8488
# Detect faces
8589
face_ids = []
86-
faces = face_client.face.detect_with_stream(ti)
90+
faces = face_client.face.detect_with_stream(image)
8791
for face in faces:
8892
face_ids.append(face.face_id)
8993

9094
# Identify faces
9195
results = face_client.face.identify(face_ids, person_group_id)
9296
if not results:
93-
print('No person identified in the person group for faces from the {}.'.format(ti.name))
97+
print('No person identified in the person group for faces from the {}.'.format(os.path.basename(image.name)))
9498
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-
99+
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
101100

101+
# Once finished, since testing, delete the PersonGroup from your resource, otherwise when you create it again, it won't allow duplicate person groups.
102+
face_client.person_group.delete(person_group_id)

0 commit comments

Comments
 (0)