|
15 | 15 | ''' |
16 | 16 | # Group image for testing against |
17 | 17 | 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__))) |
20 | 20 |
|
21 | 21 | ''' |
22 | 22 | Authentication |
23 | 23 | ''' |
24 | 24 | # Replace with a valid subscription key (keeping the quotes in place). |
25 | | -KEY = 'FACE_SUBSCRIPTION_KEY' |
| 25 | +KEY = 'cb2ea73e0f074a0e83e0ee50dc129f4b' |
26 | 26 | # Replace westus if it's not your region |
27 | 27 | BASE_URL = 'https://westus.api.cognitive.microsoft.com' |
28 | 28 | face_client = FaceClient(BASE_URL, CognitiveServicesCredentials(KEY)) |
|
31 | 31 | Create the PersonGroup |
32 | 32 | ''' |
33 | 33 | # 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) |
35 | 37 | face_client.person_group.create(person_group_id=person_group_id, name=person_group_id) |
36 | 38 |
|
37 | 39 | # Define woman friend |
|
70 | 72 | # Train the person group |
71 | 73 | face_client.person_group.train(person_group_id) |
72 | 74 | 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): |
74 | 78 | raise Exception('Training failed with message {}.'.format(training_status.message)) |
75 | | - |
| 79 | +print(training_status.status) |
76 | 80 |
|
77 | 81 | ''' |
78 | 82 | Identify a face against a defined PersonGroup |
79 | 83 | ''' |
80 | 84 | # Get test image |
81 | 85 | 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') |
83 | 87 |
|
84 | 88 | # Detect faces |
85 | 89 | face_ids = [] |
86 | | -faces = face_client.face.detect_with_stream(ti) |
| 90 | +faces = face_client.face.detect_with_stream(image) |
87 | 91 | for face in faces: |
88 | 92 | face_ids.append(face.face_id) |
89 | 93 |
|
90 | 94 | # Identify faces |
91 | 95 | results = face_client.face.identify(face_ids, person_group_id) |
92 | 96 | 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))) |
94 | 98 | 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 |
101 | 100 |
|
| 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