From a9e4f5d419372cb5bca1868f30dc7dd967ea3a49 Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Mon, 12 Nov 2018 14:47:02 -0800 Subject: [PATCH 01/17] added working samples for common and detection --- .../cognitive-services-python-sdk-samples.iml | 8 + .idea/misc.xml | 4 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 340 ++++++++++++++++++ samples/vision/face/common.py | 23 ++ samples/vision/face/detection.py | 147 ++++++++ samples/vision/face/find_similar.py | 28 ++ samples/vision/face_samples.py | 5 +- 9 files changed, 567 insertions(+), 2 deletions(-) create mode 100644 .idea/cognitive-services-python-sdk-samples.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 samples/vision/face/common.py create mode 100644 samples/vision/face/detection.py create mode 100644 samples/vision/face/find_similar.py diff --git a/.idea/cognitive-services-python-sdk-samples.iml b/.idea/cognitive-services-python-sdk-samples.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/cognitive-services-python-sdk-samples.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..65531ca --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0cb4c96 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..0ebf230 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,340 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + SUBSCRIPTION_KEY_ENV_NAME + subscription_key_env + for i in range + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + - + + + + + + + - @@ -248,14 +327,15 @@ - + + - + - + @@ -280,6 +360,11 @@ 49 @@ -288,7 +373,6 @@ - @@ -303,7 +387,6 @@ - @@ -314,25 +397,100 @@ - - + - - + + - + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/vision/face/__init__.py b/samples/vision/face/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/samples/vision/face/common.py b/samples/vision/face/common.py index 26a2c62..332ebb8 100644 --- a/samples/vision/face/common.py +++ b/samples/vision/face/common.py @@ -8,16 +8,19 @@ -def detect_faces(image_url, subscription_key): - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) +def detect_faces(face_client, image_url): detected_faces = face_client.face.detect_with_url(image_url, True, False, None, None, False) if detected_faces == None or len(detected_faces) == 0: raise Exception('No face detected from image {}'.format(image_url)) print("{} faces detected from image {}".format(len(detected_faces), image_url)) + if detected_faces[0] is None: + raise Exception("Parameter return_face_id of detect_with_stream or detect_with_url must be set to true (by default) for recognition purpose.") + return list(detected_faces) if __name__ == "__main__": SUBSCRIPTION_KEY_ENV_NAME = os.environ.get("SUBSCRIPTION_KEY_ENV_NAME") FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") - detect_faces("https://www.elsworth.com/wp/wp-content/uploads/2018/04/8ODT-e1524703208444-768x1024.jpg", "e312068d12604dee97f59230ff788d60") \ No newline at end of file + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials("e312068d12604dee97f59230ff788d60")) + detect_faces(face_client, "https://www.elsworth.com/wp/wp-content/uploads/2018/04/8ODT-e1524703208444-768x1024.jpg") \ No newline at end of file diff --git a/samples/vision/face/detection.py b/samples/vision/face/detection.py index 7c13dd3..0685546 100644 --- a/samples/vision/face/detection.py +++ b/samples/vision/face/detection.py @@ -6,9 +6,9 @@ SUBSCRIPTION_KEY_ENV_NAME = "FACE_SUBSCRIPTION_KEY" FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") -def face_detection_run(subscription_key): +def face_detection_run(face_base_url, subscription_key): print("Sample of face detection.") - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" image_file_names = ["detection1.jpg", @@ -122,7 +122,8 @@ def get_hair(hair): if __name__ == "__main__": - face_detection_run("e312068d12604dee97f59230ff788d60") + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_detection_run(face_base_url, "e312068d12604dee97f59230ff788d60") diff --git a/samples/vision/face/find_similar.py b/samples/vision/face/find_similar.py index c90b2b5..f73162e 100644 --- a/samples/vision/face/find_similar.py +++ b/samples/vision/face/find_similar.py @@ -1,9 +1,19 @@ from azure.cognitiveservices.vision.face import FaceClient +from azure.cognitiveservices.vision.face.models import TrainingStatusType from msrest.authentication import CognitiveServicesCredentials +from samples.vision.face.common import detect_faces -def find_similar_in_face_ids(endpoint, subscription_key): +import os +import uuid +import time + +SUBSCRIPTION_KEY_ENV_NAME = "FACE_SUBSCRIPTION_KEY" +FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") + +def find_similar_in_face_ids(subscription_key): print("Sample of finding similar faces in face ids.") - face_client = FaceClient(endpoint, CognitiveServicesCredentials(subscription_key)) + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" target_image_file_names = ["Family1-Dad1.jpg", "Family1-Daughter1.jpg", @@ -18,11 +28,134 @@ def find_similar_in_face_ids(endpoint, subscription_key): target_face_ids = [] for target_image_file_name in target_image_file_names: + # Detect faces from target image url. faces = face_client.face.detect_with_url(image_url_prefix + target_image_file_name) - target_face_ids.append(faces[0].face_id.value) - detected_faces = face_client.face.detect_with_url(source_image_file_name) - similar_results = face_client.face.find_similar(detected_faces[0].face_id.value, None, None, target_face_ids) + # Add detected face id to target_face_ids + target_face_ids.append(faces[0].face_id) + # Detect faces from source image url. + detected_faces = face_client.face.detect_with_url(image_url_prefix + source_image_file_name) + # Find similar example of face id to face ids. + similar_results = face_client.face.find_similar(detected_faces[0].face_id, None, None, target_face_ids) if len(similar_results) == 0: print("No similar faces to {}.".format(source_image_file_name)) for similar_result in similar_results: - print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, similar_result.face_id, similar_result.confidence)) \ No newline at end of file + print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, similar_result.face_id, similar_result.confidence)) + print("") + +def find_similar_in_face_list(subscription_key): + print("Sample of finding similar faces in face list.") + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", + "Family1-Daughter1.jpg", + "Family1-Mom1.jpg", + "Family1-Son1.jpg", + "Family2-Lady1.jpg", + "Family2-Man1.jpg", + "Family3-Lady1.jpg", + "Family3-Man1.jpg"] + + source_image_file_name = "findsimilar.jpg" + # Create a face list. + face_list_id = str(uuid.uuid4()) + print("Create face list {}.".format(face_list_id)) + face_client.face_list.create(face_list_id, "face list for find_similar_in_face_list sample", "face list for find_similar_in_face_list sample") + + for target_image_file_name in target_image_file_names: + # Add face to face list. + faces = face_client.face_list.add_face_from_url(face_list_id, image_url_prefix + target_image_file_name, target_image_file_name) + if faces is None: + raise Exception("No face detected from image {}".format(target_image_file_name)) + print("Face from image {} is successfully added to the face list.".format(target_image_file_name)) + # Get persisted faces from the face list. + persisted_faces = list(face_client.face_list.get(face_list_id).persisted_faces) + if len(persisted_faces) == 0: + raise Exception("No persisted face in face list {}".format(face_list_id)) + + # Detect faces from source image url. + detected_faces = face_client.face.detect_with_url(image_url_prefix + source_image_file_name) + + # Find similar example of face id to face list. + similar_results = face_client.face.find_similar(detected_faces[0].face_id, face_list_id) + for similar_result in similar_results: + persisted_face = [pf for pf in persisted_faces if pf.persisted_face_id == similar_result.persisted_face_id][0] + if persisted_face is None: + print("persisted face not found in similar result.") + continue + print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, persisted_face.user_data, similar_result.confidence)) + + # Delete the face list. + face_client.face_list.delete(face_list_id) + print("Delete face list {}.".format(face_list_id)) + print("") + +def find_similar_in_large_face_list(subscription_key): + print("Sample of finding similar faces in large face list.") + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", + "Family1-Daughter1.jpg", + "Family1-Mom1.jpg", + "Family1-Son1.jpg", + "Family2-Lady1.jpg", + "Family2-Man1.jpg", + "Family3-Lady1.jpg", + "Family3-Man1.jpg"] + + source_image_file_name = "findsimilar.jpg" + + # Create a large face list. + large_face_list_id = str(uuid.uuid4()) + print("Create large face list {}.".format(large_face_list_id)) + face_client.large_face_list.create(large_face_list_id, "large face list for find_similar_in_large_face_list sample", "large face list for find_similar_in_large_face_list sample") + + for target_image_file_name in target_image_file_names: + faces = face_client.large_face_list.add_face_from_url(large_face_list_id, image_url_prefix + target_image_file_name, target_image_file_name) + if faces is None: + raise Exception("No face detected from image {}.".format(target_image_file_name)) + print("Face from image {} is successfully added to the large face list.".format(target_image_file_name)) + + # Start to train the large face list. + print("Train large face list {}".format(large_face_list_id)) + face_client.large_face_list.train(large_face_list_id) + + # Wait until the training is completed. + while True: + time.sleep(1) + training_status = face_client.large_face_list.get_training_status(large_face_list_id) + print("Training status is {}.".format(training_status.status)) + if training_status.status != TrainingStatusType.running: + if training_status.status == TrainingStatusType.failed: + raise Exception("Training failed with message {}.".format(training_status.message)) + break + + # Get persisted faces from the large face list. + persisted_faces = list(face_client.large_face_list.list_faces(large_face_list_id)) + if len(persisted_faces) == 0: + raise Exception("No persisted face in large face list {}.".format(large_face_list_id)) + + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + + # Find similar example of face id to large face list. + similar_results = face_client.face.find_similar(detected_faces[0].face_id, None, large_face_list_id) + + for similar_result in similar_results: + persisted_face = [pf for pf in persisted_faces if pf.persisted_face_id == similar_result.persisted_face_id][0] + if persisted_face is None: + print("persisted face not found in similar result.") + continue + print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, persisted_face.user_data, similar_result.confidence)) + + # Delete the large face list. + face_client.large_face_list.delete(large_face_list_id) + print("Delete large face list {}.".format(large_face_list_id)) + print("") + + +if __name__ == "__main__": + #find_similar_in_face_ids("e312068d12604dee97f59230ff788d60") + #find_similar_in_face_list("e312068d12604dee97f59230ff788d60") + find_similar_in_large_face_list("e312068d12604dee97f59230ff788d60") \ No newline at end of file From 02bc0fc5cc0a266a2af8fc23479e1cf58ade45b7 Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Tue, 13 Nov 2018 11:34:21 -0800 Subject: [PATCH 03/17] edited find_similar sample and added group sample --- .idea/workspace.xml | 144 ++++++++++++++++------------ samples/vision/face/find_similar.py | 22 ++--- samples/vision/face/group.py | 57 +++++++++++ 3 files changed, 151 insertions(+), 72 deletions(-) create mode 100644 samples/vision/face/group.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6a7d736..512d1b2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -20,22 +20,22 @@ - + - + - + - + @@ -55,53 +55,37 @@ - + - - + + - + - - - - - - + - - - - - - - - - - - - - - + + + - + - - + + @@ -134,8 +118,9 @@ @@ -213,7 +198,7 @@ - + + + + + + @@ -330,13 +338,13 @@ - + - - + + @@ -361,9 +369,14 @@ @@ -198,7 +222,7 @@ - + - + - + - + @@ -338,13 +362,13 @@ - + - - + + @@ -479,41 +503,60 @@ - + + + + + + + + - + + - + - - + + + + + + - + - - + + - - + + + + + + + + - - + + - + + diff --git a/samples/vision/face/identify.py b/samples/vision/face/identify.py new file mode 100644 index 0000000..a18237b --- /dev/null +++ b/samples/vision/face/identify.py @@ -0,0 +1,160 @@ +from azure.cognitiveservices.vision.face import FaceClient +from azure.cognitiveservices.vision.face.models import Person, TrainingStatusType +from msrest.authentication import CognitiveServicesCredentials +from samples.vision.face.common import detect_faces +import os +import uuid +import time + +SUBSCRIPTION_KEY_ENV_NAME = "FACE_SUBSCRIPTION_KEY" +FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") + + +def identify_in_person_group(face_base_url, subscription_key): + print("Sample of identifying faces in a group of people.") + + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_dictionary = { + "Family1-Dad" : ["Family1-Dad1.jpg", "Family1-Dad2.jpg"], + "Family1-Mom" : ["Family1-Mom1.jpg", "Family1-Mom2.jpg"], + "Family1-Son" : ["Family1-Son1.jpg", "Family1-Son2.jpg"], + "Family1-Daughter" : ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"], + "Family2-Lady" : ["Family2-Lady1.jpg", "Family2-Lady2.jpg"], + "Family2-Man" : ["Family2-Man1.jpg", "Family2-Man2.jpg"] + } + source_image_file_name = "identification1.jpg" + + # Create a person group. + person_group_id = str(uuid.uuid4()) + print("Create a person group {}.".format(person_group_id)) + face_client.person_group.create(person_group_id, person_group_id) + + for target_image_file_dictionary_name in target_image_file_dictionary.keys(): + person_id = face_client.person_group_person.create(person_group_id, target_image_file_dictionary_name).person_id + + # Limit TPS + time.sleep(0.25) + + # Create a person group person. + person = Person(name=target_image_file_dictionary_name, user_data="Person for sample", person_id=person_id) + + print("Create a person group person {}.".format(person.name)) + + for target_image_file_name in target_image_file_dictionary[target_image_file_dictionary_name]: + # Add face to the person group person + print("Add face to the person group person {} from image.".format(target_image_file_dictionary_name, target_image_file_name)) + face = face_client.person_group_person.add_face_from_url(person_group_id, person.person_id, image_url_prefix + target_image_file_name, target_image_file_name) + if face is None: + raise Exception("No persisted face from image {}".format(target_image_file_name)) + + # Start to train the person group. + print("Train person group {}".format(person_group_id)) + face_client.person_group.train(person_group_id) + training_status = face_client.person_group.get_training_status(person_group_id) + print("Training status is {}".format(training_status.status)) + if training_status.status != TrainingStatusType.running: + if training_status.status == TrainingStatusType.failed: + raise Exception("Training failed with message {}.".format(training_status.message)) + + source_face_ids = [] + + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + + # Add detected face id to source_face_ids + for detected_face in detected_faces: + source_face_ids.append(detected_face.face_id) + + # Identify example of identifying faces towards person group. + identify_results = face_client.face.identify(source_face_ids, person_group_id) + if identify_results is None: + print("No person identified in the person group for faces from the {}.".format(source_image_file_name)) + return + + for identify_result in identify_results: + person = face_client.person_group_person.get(person_group_id, identify_result.candidates[0].person_id) + print("Person {} is identified for face: {} - {}, confidence: {}.".format(person.name, source_image_file_name, identify_result.face_id, identify_result.candidates[0].confidence)) + + # Delete the person group. + face_client.person_group.delete(person_group_id) + print("Delete the person group {}.".format(person_group_id)) + print("") + + +def identify_in_large_person_group(face_base_url, subscription_key): + print("Sample of identify faces in large person group.") + + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_dictionary = { + "Family1-Dad": ["Family1-Dad1.jpg", "Family1-Dad2.jpg"], + "Family1-Mom": ["Family1-Mom1.jpg", "Family1-Mom2.jpg"], + "Family1-Son": ["Family1-Son1.jpg", "Family1-Son2.jpg"], + "Family1-Daughter": ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"], + "Family2-Lady": ["Family2-Lady1.jpg", "Family2-Lady2.jpg"], + "Family2-Man": ["Family2-Man1.jpg", "Family2-Man2.jpg"] + } + source_image_file_name = "identification1.jpg" + + # Create a large person group. + large_person_group_id = str(uuid.uuid4()) + print("Create a large person group {}.".format(large_person_group_id)) + face_client.large_person_group.create(large_person_group_id, large_person_group_id) + + for target_image_file_dictionary_name in target_image_file_dictionary.keys(): + person_id = face_client.large_person_group_person.create(large_person_group_id, target_image_file_dictionary_name).person_id + + # Limit TPS + time.sleep(0.25) + + # Create a person group person. + person = Person(name=target_image_file_dictionary_name, user_data="Person for sample", person_id=person_id) + + print("Create a large person group person {}.".format(person.name)) + + for target_image_file_name in target_image_file_dictionary[target_image_file_dictionary_name]: + # Add face to the person group person + print("Add face to the large person group person {} from image.".format(target_image_file_dictionary_name, target_image_file_name)) + face = face_client.large_person_group_person.add_face_from_url(large_person_group_id, person.person_id, image_url_prefix + target_image_file_name, target_image_file_name) + if face is None: + raise Exception("No persisted face from image {}".format(target_image_file_name)) + + # Start to train the large person group. + print("Train large person group {}.".format(large_person_group_id)) + face_client.large_person_group.train(large_person_group_id) + training_status = face_client.large_person_group.get_training_status(large_person_group_id) + print("Training status is {}".format(training_status.status)) + if training_status.status == TrainingStatusType.failed: + raise Exception("Training failed with message {}.".format(training_status.message)) + + + source_face_ids = [] + + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + + # Add detected face ids to source_face_ids + for detected_face in detected_faces: + source_face_ids.append(detected_face.face_id) + + # Identify example of identifying faces towards large person group. + identify_results = face_client.face.identify(source_face_ids, None, large_person_group_id) + if identify_results is None: + print("No person identified in the large person group for faces from the {}.".format(source_image_file_name)) + return + + for identify_result in identify_results: + person = face_client.large_person_group_person.get(large_person_group_id, identify_result.candidates[0].person_id) + print ("Person {} is identified for face: {} - {}, confidence: {}.".format(person.name, source_image_file_name, identify_result.face_id, identify_result.candidates[0].confidence)) + + # Delete the person group. + face_client.large_person_group.delete(large_person_group_id) + print("Delete the large person group {}.".format(large_person_group_id)) + print("") + + +if __name__ == "__main__": + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + identify_in_person_group(face_base_url, "e312068d12604dee97f59230ff788d60") + identify_in_large_person_group(face_base_url, "e312068d12604dee97f59230ff788d60") \ No newline at end of file From 8b5ae88a12bf08a62dc67f34a17767b4e0fd8894 Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Tue, 13 Nov 2018 16:05:56 -0800 Subject: [PATCH 05/17] added samples for verify --- .idea/workspace.xml | 114 ++++++++++++------------- samples/vision/face/verify.py | 154 ++++++++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+), 60 deletions(-) create mode 100644 samples/vision/face/verify.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6652bb3..680a64d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -20,22 +20,22 @@ - + - + - + - + @@ -58,8 +58,8 @@ - - + + @@ -68,51 +68,33 @@ - + - - + + - - + - - - - - - - - - - - + + - - + + - + + - - - - - - - - - @@ -145,6 +127,7 @@ @@ -222,7 +205,7 @@ - + - + - + - + - + - + + - @@ -521,17 +504,6 @@ - - - - - - - - - - - @@ -551,12 +523,34 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + - - + + diff --git a/samples/vision/face/verify.py b/samples/vision/face/verify.py new file mode 100644 index 0000000..dbad86d --- /dev/null +++ b/samples/vision/face/verify.py @@ -0,0 +1,154 @@ +from azure.cognitiveservices.vision.face import FaceClient +from azure.cognitiveservices.vision.face.models import Person, TrainingStatusType +from msrest.authentication import CognitiveServicesCredentials +from samples.vision.face.common import detect_faces +import os +import uuid +import time + +SUBSCRIPTION_KEY_ENV_NAME = "FACE_SUBSCRIPTION_KEY" +FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") + +def verify_face_to_face(face_base_url, subscription_key): + print("Sample of verify face to face.") + + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] + source_image_file_name1 = "Family1-Dad3.jpg" + source_image_file_name2 = "Family1-Son1.jpg" + + target_face_ids = [] + for image_file_name in target_image_file_names: + # Detect faces from target image url. + detected_faces = detect_faces(face_client, image_url_prefix + image_file_name) + target_face_ids.append(detected_faces[0].face_id) + + # Detect faces from source image file 1. + detected_faces1 = detect_faces(face_client, image_url_prefix + source_image_file_name1) + source_face_id1 = detected_faces1[0].face_id + + # Detect faces from source image file 2. + detected_faces2 = detect_faces(face_client, image_url_prefix + source_image_file_name2) + source_face_id2 = detected_faces2[0].face_id + + # Verification example for faces of the same person. + verify_result1 = face_client.face.verify_face_to_face(source_face_id1, target_face_ids[0]) + if verify_result1.is_identical: + print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(source_image_file_name1, target_image_file_names[0], verify_result1.confidence)) + else: + print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(source_image_file_name1, target_image_file_names[0], verify_result1.confidence)) + + # Verification example for faces of different persons. + verify_result2 = face_client.face.verify_face_to_face(source_face_id2, target_face_ids[0]) + if verify_result2.is_identical: + print("Faces from {} & {} are of the same (Negative) person, similarity confidence: {}.".format( + source_image_file_name2, target_image_file_names[0], verify_result2.confidence)) + else: + print("Faces from {} & {} are of different (Positive) persons, similarity confidence: {}.".format( + source_image_file_name2, target_image_file_names[0], verify_result2.confidence)) + print("") + + +def verify_in_person_group(face_base_url, subscription_key): + print("Sample of verify face to person group.") + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] + source_image_file_name1 = "Family1-Dad3.jpg" + + # Create a person group. + person_group_id = str(uuid.uuid4()) + print("Create a person group {}.".format(person_group_id)) + face_client.person_group.create(person_group_id, person_group_id) + + person_id = face_client.person_group_person.create(person_group_id, "Dad").person_id + + # Create a person group person. + p = Person(name="Dad", user_data="Person for sample", person_id=person_id) + print("Create a person group person {}.".format(p.name)) + + for target_image_file_name in target_image_file_names: + # Add face to the person group. + print("Add face to the person group person {} from image {}.".format(p.name, target_image_file_name)) + faces = face_client.person_group_person.add_face_from_url(person_group_id, p.person_id, image_url_prefix + target_image_file_name, target_image_file_name) + + if faces is None: + raise Exception("No persisted face from image {}.".format(target_image_file_name)) + + face_ids = [] + + # Add detected face id to face_ids + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name1) + face_ids.append(detected_faces[0].face_id) + + # Verification example for faces of the same person. + verify_result = face_client.face.verify_face_to_person(face_ids[0], p.person_id, person_group_id) + if verify_result.is_identical: + print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(source_image_file_name1, p.name, verify_result.confidence)) + else: + print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(source_image_file_name1, p.name, verify_result.confidence)) + + # Delete the person group. + print("Delete the person group {}.".format(person_group_id)) + face_client.person_group.delete(person_group_id) + print("") + + +def verify_in_large_person_group(face_base_url, subscription_key): + print("Sample of verify face to large person group.") + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] + source_image_file_name1 = "Family1-Dad3.jpg" + + # Create a large person group. + large_person_group_id = str(uuid.uuid4()) + print("Create a large person group {}.".format(large_person_group_id)) + face_client.large_person_group.create(large_person_group_id, large_person_group_id) + + person_id = face_client.large_person_group_person.create(large_person_group_id, "Dad").person_id + + # Create a large person group person. + p = Person(name="Dad", user_data="Person for sample", person_id=person_id) + print("Create a large person group person {}.".format(p.name)) + + for target_image_file_name in target_image_file_names: + # Add face to the large person group. + print("Add face to the large person group person {} from image {}.".format(p.name, target_image_file_name)) + faces = face_client.large_person_group_person.add_face_from_url(large_person_group_id, p.person_id, + image_url_prefix + target_image_file_name, + target_image_file_name) + + if faces is None: + raise Exception("No persisted face from image {}.".format(target_image_file_name)) + + face_ids = [] + + # Add detected face id to face_ids + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name1) + face_ids.append(detected_faces[0].face_id) + + # Verification example for faces of the same person. + verify_result = face_client.face.verify_face_to_person(face_ids[0], p.person_id, None, large_person_group_id) + if verify_result.is_identical: + print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format( + source_image_file_name1, p.name, verify_result.confidence)) + else: + print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format( + source_image_file_name1, p.name, verify_result.confidence)) + + # Delete the person group. + print("Delete the large person group {}.".format(large_person_group_id)) + face_client.large_person_group.delete(large_person_group_id) + print("") + + +if __name__ == "__main__": + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + verify_face_to_face(face_base_url, "e312068d12604dee97f59230ff788d60") + verify_in_person_group(face_base_url, "e312068d12604dee97f59230ff788d60") + verify_in_large_person_group(face_base_url, "e312068d12604dee97f59230ff788d60") \ No newline at end of file From d4e40c5765e26a49888bd172ec14ff4a18b733d9 Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Wed, 14 Nov 2018 11:33:46 -0800 Subject: [PATCH 06/17] finished samples and edited retrieval method of environment variables --- .../cognitive-services-python-sdk-samples.iml | 2 +- .idea/misc.xml | 2 +- .idea/workspace.xml | 187 ++++++++++++------ samples/vision/face/common.py | 7 +- samples/vision/face/detection.py | 4 +- samples/vision/face/find_similar.py | 8 +- samples/vision/face/group.py | 4 +- samples/vision/face/identify.py | 2 +- samples/vision/face/verify.py | 9 +- 9 files changed, 143 insertions(+), 82 deletions(-) diff --git a/.idea/cognitive-services-python-sdk-samples.iml b/.idea/cognitive-services-python-sdk-samples.iml index d0876a7..bd50bdc 100644 --- a/.idea/cognitive-services-python-sdk-samples.iml +++ b/.idea/cognitive-services-python-sdk-samples.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 65531ca..e82ee89 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 680a64d..96ee6d9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -20,22 +20,22 @@ - + - + - + - + @@ -46,8 +46,8 @@ - - + + @@ -58,11 +58,24 @@ - - + + - + + + + + + + + + + + + + + @@ -71,11 +84,11 @@ - - + + - + @@ -84,12 +97,36 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - @@ -107,11 +144,14 @@ SUBSCRIPTION_KEY_ENV_NAME - subscription_key_env for i in range face_list Guid detect + username + subscription + subscription_key + subscription_key_env @@ -122,10 +162,10 @@ @@ -212,6 +252,7 @@ @@ -345,6 +389,25 @@ + + + + + + + + + + + + + + + + + + + @@ -362,7 +425,7 @@ - + - + - - + + @@ -58,7 +58,7 @@ - + @@ -71,11 +71,11 @@ - - + + - + @@ -84,7 +84,7 @@ - + @@ -94,15 +94,14 @@ - + - - + + - - - + + @@ -111,11 +110,8 @@ - - - - - + + @@ -123,8 +119,8 @@ - - + + @@ -161,13 +157,13 @@ @@ -438,16 +434,6 @@ 49 - - + + - + - + - + - + - - - + + + + + + - - - @@ -385,12 +350,12 @@ - + - - + + @@ -430,9 +395,14 @@ - file://$PROJECT_DIR$/samples/vision/face_samples.py - 49 - + + file://$PROJECT_DIR$/samples/search/visual_search_samples.py + 34 + @@ -449,13 +419,6 @@ - - - - - - - @@ -529,71 +492,130 @@ - + - - + - - + - + + + + - - + + + + - + - - + + + + + + + + + + + + + + + + + + + - + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + - - + + - - + - - + + - - + + + + + + + + - - + + diff --git a/samples/language/text_analytics_samples.py b/samples/language/text_analytics_samples.py index eac531f..4f33fe5 100644 --- a/samples/language/text_analytics_samples.py +++ b/samples/language/text_analytics_samples.py @@ -121,7 +121,8 @@ def sentiment(subscription_key): if __name__ == "__main__": - import sys, os.path - sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) - from tools import execute_samples - execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) + # import sys, os.path + # sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) + # from tools import execute_samples + #execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) + language_extraction("027b004824b34b009f533ab4274f2dbb") diff --git a/samples/vision/face/__init__.py b/samples/vision/face/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/samples/vision/face/common.py b/samples/vision/face/common.py deleted file mode 100644 index ba5b8f9..0000000 --- a/samples/vision/face/common.py +++ /dev/null @@ -1,25 +0,0 @@ -from azure.cognitiveservices.vision.face import FaceClient -from msrest.authentication import CognitiveServicesCredentials -import os - -SUBSCRIPTION_KEY_ENV_NAME = os.environ.get("FACE_SUBSCRIPTION_KEY") -FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") - - - - - -def detect_faces(face_client, image_url): - detected_faces = face_client.face.detect_with_url(image_url, True, False, None, None, False) - if detected_faces == None or len(detected_faces) == 0: - raise Exception('No face detected from image {}'.format(image_url)) - print("{} faces detected from image {}".format(len(detected_faces), image_url)) - if detected_faces[0] is None: - raise Exception("Parameter return_face_id of detect_with_stream or detect_with_url must be set to true (by default) for recognition purpose.") - return list(detected_faces) - - -if __name__ == "__main__": - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(SUBSCRIPTION_KEY_ENV_NAME)) - detect_faces(face_client, "https://www.elsworth.com/wp/wp-content/uploads/2018/04/8ODT-e1524703208444-768x1024.jpg") \ No newline at end of file diff --git a/samples/vision/face/detection.py b/samples/vision/face/detection.py deleted file mode 100644 index 8bd694d..0000000 --- a/samples/vision/face/detection.py +++ /dev/null @@ -1,148 +0,0 @@ -from azure.cognitiveservices.vision.face import FaceClient -from msrest.authentication import CognitiveServicesCredentials -from azure.cognitiveservices.vision.face.models import FaceAttributeType, HairColorType -import os - -SUBSCRIPTION_KEY_ENV_NAME = os.environ.get("FACE_SUBSCRIPTION_KEY") -FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") - -def face_detection_run(face_base_url, subscription_key): - print("Sample of face detection.") - - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - image_file_names = ["detection1.jpg", - "detection2.jpg", - "detection3.jpg", - "detection4.jpg", - "detection5.jpg", - "detection6.jpg"] - for image_file_name in image_file_names: - detected_faces = face_client.face.detect_with_url(image_url_prefix + image_file_name, True, return_face_attributes = [ - FaceAttributeType.accessories, - 'age', - 'blur', - 'emotion', - 'exposure', - 'facialHair', - 'gender', - 'glasses', - 'hair', - 'headPose', - 'makeup', - 'noise', - 'occlusion', - 'smile' - ]) - if detected_faces == None or len(detected_faces) == 0: - raise Exception("No face detected from image {}".format(image_file_name)) - print("{} faces detected from image {}".format(len(detected_faces), image_file_name)) - if detected_faces[0].face_attributes is None: - raise Exception("Parameter return_face_attributes of detect_with_stream_async must be set to get face attributes.") - - for face in detected_faces: - print("Face attributes of {} Rectangle(Left/Top/Width/Height) : {} {} {} {}".format(image_file_name, face.face_rectangle.left, face.face_rectangle.top, face.face_rectangle.width, face.face_rectangle.height)) - print("Face attributes of {} Accessories : {}".format(image_file_name, get_accessories(face.face_attributes.accessories))) - print("Face attributes of {} Age : {}".format(image_file_name, face.face_attributes.age)) - print("Face attributes of {} Blur : {}".format(image_file_name, face.face_attributes.blur.blur_level)) - print("Face attributes of {} Emotion : {}".format(image_file_name, get_emotion(face.face_attributes.emotion))) - print("Face attributes of {} Exposure : {}".format(image_file_name, face.face_attributes.exposure.exposure_level)) - if face.face_attributes.facial_hair.moustache + face.face_attributes.facial_hair.beard + face.face_attributes.facial_hair.sideburns > 0: - print("Face attributes of {} FacialHair : Yes".format(image_file_name)) - else: - print("Face attributes of {} FacialHair : No".format(image_file_name)) - print("Face attributes of {} Gender : {}".format(image_file_name, face.face_attributes.gender)) - print("Face attributes of {} Glasses : {}".format(image_file_name, face.face_attributes.glasses)) - print("Face attributes of {} Hair : {}".format(image_file_name, get_hair(face.face_attributes.hair))) - print("Face attributes of {} HeadPose : Pitch: {}, Roll: {}, Yaw: {}".format(image_file_name, round(face.face_attributes.head_pose.pitch, 2), round(face.face_attributes.head_pose.roll, 2), round(face.face_attributes.head_pose.yaw, 2))) - if face.face_attributes.makeup.eye_makeup or face.face_attributes.makeup.lip_makeup: - print("Face attributes of {} Makeup : Yes".format(image_file_name)) - else: - print("Face attributes of {} Makeup : No".format(image_file_name)) - print("Face attributes of {} Noise : {}".format(image_file_name, face.face_attributes.noise.noise_level)) - print("Face attributes of {} Occlusion : EyeOccluded: {}, ForeheadOccluded: {}, MouthOccluded: {}".format(image_file_name, "Yes" if face.face_attributes.occlusion.eye_occluded else "No", - "Yes" if face.face_attributes.occlusion.forehead_occluded else "No", "Yes" if face.face_attributes.occlusion.mouth_occluded else "No")) - - print("Face attributes of {} Smile : {}".format(image_file_name, face.face_attributes.smile)) - print() - - -def get_accessories(accessories): - if len(accessories) == 0: - return "No accessories" - accessory_array = [] - - for i in range(len(accessories)): - accessory_array.append(str(accessories[i])) - return ",".join(accessory_array) - - -def get_emotion(emotion): - emotion_type = "" - emotion_value = 0.0 - if emotion.anger > emotion_value: - emotion_type = "anger" - emotion_value = emotion.anger - if emotion.contempt > emotion_value: - emotion_type = "contempt" - emotion_value = emotion.contempt - if emotion.disgust > emotion_value: - emotion_type = "disgust" - emotion_value = emotion.disgust - if emotion.fear > emotion_value: - emotion_type = "fear" - emotion_value = emotion.fear - if emotion.happiness > emotion_value: - emotion_type = "happiness" - emotion_value = emotion.happiness - if emotion.neutral > emotion_value: - emotion_type = "neutral" - emotion_value = emotion.neutral - if emotion.sadness > emotion_value: - emotion_type = "sadness" - emotion_value = emotion.sadness - if emotion.surprise > emotion_value: - emotion_type = "surprise" - return emotion_type - - -def get_hair(hair): - if len(hair.hair_color) == 0: - return "invisible" if hair.invisible else "bald" - return_color = HairColorType.unknown - max_confidence = 0.0 - - for hair_color in hair.hair_color: - if hair_color.confidence <= max_confidence: - continue; - max_confidence = hair_color.confidence - return_color = hair_color.color; - - return str(return_color) - - -if __name__ == "__main__": - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) - face_detection_run(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) - - - - - - - - - - - - - - - - - - - - - - diff --git a/samples/vision/face/find_similar.py b/samples/vision/face/find_similar.py deleted file mode 100644 index 041451f..0000000 --- a/samples/vision/face/find_similar.py +++ /dev/null @@ -1,159 +0,0 @@ -from azure.cognitiveservices.vision.face import FaceClient -from azure.cognitiveservices.vision.face.models import TrainingStatusType -from msrest.authentication import CognitiveServicesCredentials -from samples.vision.face.common import detect_faces - -import os -import uuid -import time - -SUBSCRIPTION_KEY_ENV_NAME = os.environ.get("FACE_SUBSCRIPTION_KEY") -FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") - -def find_similar_in_face_ids(face_base_url, subscription_key): - print("Sample of finding similar faces in face ids.") - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_names = ["Family1-Dad1.jpg", - "Family1-Daughter1.jpg", - "Family1-Mom1.jpg", - "Family1-Son1.jpg", - "Family2-Lady1.jpg", - "Family2-Man1.jpg", - "Family3-Lady1.jpg", - "Family3-Man1.jpg"] - - source_image_file_name = "findsimilar.jpg" - target_face_ids = [] - - for target_image_file_name in target_image_file_names: - # Detect faces from target image url. - faces = detect_faces(face_client, image_url_prefix + target_image_file_name) - # Add detected face id to target_face_ids - target_face_ids.append(faces[0].face_id) - # Detect faces from source image url. - detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) - # Find similar example of face id to face ids. - similar_results = face_client.face.find_similar(detected_faces[0].face_id, None, None, target_face_ids) - if len(similar_results) == 0: - print("No similar faces to {}.".format(source_image_file_name)) - for similar_result in similar_results: - print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, similar_result.face_id, similar_result.confidence)) - print("") - -def find_similar_in_face_list(face_base_url, subscription_key): - print("Sample of finding similar faces in face list.") - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_names = ["Family1-Dad1.jpg", - "Family1-Daughter1.jpg", - "Family1-Mom1.jpg", - "Family1-Son1.jpg", - "Family2-Lady1.jpg", - "Family2-Man1.jpg", - "Family3-Lady1.jpg", - "Family3-Man1.jpg"] - - source_image_file_name = "findsimilar.jpg" - # Create a face list. - face_list_id = str(uuid.uuid4()) - print("Create face list {}.".format(face_list_id)) - face_client.face_list.create(face_list_id, "face list for find_similar_in_face_list sample", "face list for find_similar_in_face_list sample") - - for target_image_file_name in target_image_file_names: - # Add face to face list. - faces = face_client.face_list.add_face_from_url(face_list_id, image_url_prefix + target_image_file_name, target_image_file_name) - if faces is None: - raise Exception("No face detected from image {}".format(target_image_file_name)) - print("Face from image {} is successfully added to the face list.".format(target_image_file_name)) - # Get persisted faces from the face list. - persisted_faces = list(face_client.face_list.get(face_list_id).persisted_faces) - if len(persisted_faces) == 0: - raise Exception("No persisted face in face list {}".format(face_list_id)) - - # Detect faces from source image url. - detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) - - # Find similar example of face id to face list. - similar_results = face_client.face.find_similar(detected_faces[0].face_id, face_list_id) - for similar_result in similar_results: - persisted_face = [pf for pf in persisted_faces if pf.persisted_face_id == similar_result.persisted_face_id][0] - if persisted_face is None: - print("persisted face not found in similar result.") - continue - print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, persisted_face.user_data, similar_result.confidence)) - - # Delete the face list. - face_client.face_list.delete(face_list_id) - print("Delete face list {}.".format(face_list_id)) - print("") - -def find_similar_in_large_face_list(face_base_url, subscription_key): - print("Sample of finding similar faces in large face list.") - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_names = ["Family1-Dad1.jpg", - "Family1-Daughter1.jpg", - "Family1-Mom1.jpg", - "Family1-Son1.jpg", - "Family2-Lady1.jpg", - "Family2-Man1.jpg", - "Family3-Lady1.jpg", - "Family3-Man1.jpg"] - - source_image_file_name = "findsimilar.jpg" - - # Create a large face list. - large_face_list_id = str(uuid.uuid4()) - print("Create large face list {}.".format(large_face_list_id)) - face_client.large_face_list.create(large_face_list_id, "large face list for find_similar_in_large_face_list sample", "large face list for find_similar_in_large_face_list sample") - - for target_image_file_name in target_image_file_names: - faces = face_client.large_face_list.add_face_from_url(large_face_list_id, image_url_prefix + target_image_file_name, target_image_file_name) - if faces is None: - raise Exception("No face detected from image {}.".format(target_image_file_name)) - print("Face from image {} is successfully added to the large face list.".format(target_image_file_name)) - - # Start to train the large face list. - print("Train large face list {}".format(large_face_list_id)) - face_client.large_face_list.train(large_face_list_id) - - # Wait until the training is completed. - while True: - time.sleep(1) - training_status = face_client.large_face_list.get_training_status(large_face_list_id) - print("Training status is {}.".format(training_status.status)) - if training_status.status != TrainingStatusType.running: - if training_status.status == TrainingStatusType.failed: - raise Exception("Training failed with message {}.".format(training_status.message)) - break - - # Get persisted faces from the large face list. - persisted_faces = list(face_client.large_face_list.list_faces(large_face_list_id)) - if len(persisted_faces) == 0: - raise Exception("No persisted face in large face list {}.".format(large_face_list_id)) - - # Detect faces from source image url. - detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) - - # Find similar example of face id to large face list. - similar_results = face_client.face.find_similar(detected_faces[0].face_id, None, large_face_list_id) - - for similar_result in similar_results: - persisted_face = [pf for pf in persisted_faces if pf.persisted_face_id == similar_result.persisted_face_id][0] - if persisted_face is None: - print("persisted face not found in similar result.") - continue - print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, persisted_face.user_data, similar_result.confidence)) - - # Delete the large face list. - face_client.large_face_list.delete(large_face_list_id) - print("Delete large face list {}.".format(large_face_list_id)) - print("") - - -if __name__ == "__main__": - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) - find_similar_in_face_ids(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) - find_similar_in_face_list(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) - find_similar_in_large_face_list(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) \ No newline at end of file diff --git a/samples/vision/face/group.py b/samples/vision/face/group.py deleted file mode 100644 index cb27580..0000000 --- a/samples/vision/face/group.py +++ /dev/null @@ -1,56 +0,0 @@ -from azure.cognitiveservices.vision.face import FaceClient -from msrest.authentication import CognitiveServicesCredentials -from samples.vision.face.common import detect_faces -import os - -SUBSCRIPTION_KEY_ENV_NAME = os.environ.get("FACE_SUBSCRIPTION_KEY") -FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") - -def group_run(face_base_url, subscription_key): - print("Sample of grouping faces.") - - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - image_file_names = ["Family1-Dad1.jpg", - "Family1-Daughter1.jpg", - "Family1-Mom1.jpg", - "Family1-Son1.jpg", - "Family2-Lady1.jpg", - "Family2-Man1.jpg", - "Family3-Lady1.jpg", - "Family3-Man1.jpg"] - - faces = {} - face_ids = [] - - for image_file_name in image_file_names: - # Detect faces from target image url. - detected_faces = detect_faces(face_client, image_url_prefix + image_file_name) - - # Add detected face id to face_ids and faces. - face_ids.append(detected_faces[0].face_id) - faces[str(detected_faces[0].face_id)] = image_file_name - - # Call grouping, the grouping result is a group collection, each group contains similar faces. - group_result = face_client.face.group(face_ids) - - # Face groups containing faces that are similar. - for i in range(len(group_result.groups)): - to_print = "Found face group {}: ".format(i + 1) - for face_id in group_result.groups[i]: - to_print = to_print + "{}".format(str(faces[str(face_id)])) - print(to_print + ".") - - # Messy group contains all faces which are not similar to any other faces. - if len(group_result.messy_group) > 0: - to_print = "Found messy face group: " - for face_id in group_result.messy_group: - to_print = to_print + str(faces[face_id]) + " " - print(to_print + ".") - - print("") - - -if __name__ == "__main__": - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) - group_run(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) diff --git a/samples/vision/face/identify.py b/samples/vision/face/identify.py deleted file mode 100644 index 997e9da..0000000 --- a/samples/vision/face/identify.py +++ /dev/null @@ -1,160 +0,0 @@ -from azure.cognitiveservices.vision.face import FaceClient -from azure.cognitiveservices.vision.face.models import Person, TrainingStatusType -from msrest.authentication import CognitiveServicesCredentials -from samples.vision.face.common import detect_faces -import os -import uuid -import time - -SUBSCRIPTION_KEY_ENV_NAME = os.environ.get("FACE_SUBSCRIPTION_KEY") -FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") - - -def identify_in_person_group(face_base_url, subscription_key): - print("Sample of identifying faces in a group of people.") - - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_dictionary = { - "Family1-Dad" : ["Family1-Dad1.jpg", "Family1-Dad2.jpg"], - "Family1-Mom" : ["Family1-Mom1.jpg", "Family1-Mom2.jpg"], - "Family1-Son" : ["Family1-Son1.jpg", "Family1-Son2.jpg"], - "Family1-Daughter" : ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"], - "Family2-Lady" : ["Family2-Lady1.jpg", "Family2-Lady2.jpg"], - "Family2-Man" : ["Family2-Man1.jpg", "Family2-Man2.jpg"] - } - source_image_file_name = "identification1.jpg" - - # Create a person group. - person_group_id = str(uuid.uuid4()) - print("Create a person group {}.".format(person_group_id)) - face_client.person_group.create(person_group_id, person_group_id) - - for target_image_file_dictionary_name in target_image_file_dictionary.keys(): - person_id = face_client.person_group_person.create(person_group_id, target_image_file_dictionary_name).person_id - - # Limit TPS - time.sleep(0.25) - - # Create a person group person. - person = Person(name=target_image_file_dictionary_name, user_data="Person for sample", person_id=person_id) - - print("Create a person group person {}.".format(person.name)) - - for target_image_file_name in target_image_file_dictionary[target_image_file_dictionary_name]: - # Add face to the person group person - print("Add face to the person group person {} from image.".format(target_image_file_dictionary_name, target_image_file_name)) - face = face_client.person_group_person.add_face_from_url(person_group_id, person.person_id, image_url_prefix + target_image_file_name, target_image_file_name) - if face is None: - raise Exception("No persisted face from image {}".format(target_image_file_name)) - - # Start to train the person group. - print("Train person group {}".format(person_group_id)) - face_client.person_group.train(person_group_id) - training_status = face_client.person_group.get_training_status(person_group_id) - print("Training status is {}".format(training_status.status)) - if training_status.status != TrainingStatusType.running: - if training_status.status == TrainingStatusType.failed: - raise Exception("Training failed with message {}.".format(training_status.message)) - - source_face_ids = [] - - # Detect faces from source image url. - detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) - - # Add detected face id to source_face_ids - for detected_face in detected_faces: - source_face_ids.append(detected_face.face_id) - - # Identify example of identifying faces towards person group. - identify_results = face_client.face.identify(source_face_ids, person_group_id) - if identify_results is None: - print("No person identified in the person group for faces from the {}.".format(source_image_file_name)) - return - - for identify_result in identify_results: - person = face_client.person_group_person.get(person_group_id, identify_result.candidates[0].person_id) - print("Person {} is identified for face: {} - {}, confidence: {}.".format(person.name, source_image_file_name, identify_result.face_id, identify_result.candidates[0].confidence)) - - # Delete the person group. - face_client.person_group.delete(person_group_id) - print("Delete the person group {}.".format(person_group_id)) - print("") - - -def identify_in_large_person_group(face_base_url, subscription_key): - print("Sample of identify faces in large person group.") - - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_dictionary = { - "Family1-Dad": ["Family1-Dad1.jpg", "Family1-Dad2.jpg"], - "Family1-Mom": ["Family1-Mom1.jpg", "Family1-Mom2.jpg"], - "Family1-Son": ["Family1-Son1.jpg", "Family1-Son2.jpg"], - "Family1-Daughter": ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"], - "Family2-Lady": ["Family2-Lady1.jpg", "Family2-Lady2.jpg"], - "Family2-Man": ["Family2-Man1.jpg", "Family2-Man2.jpg"] - } - source_image_file_name = "identification1.jpg" - - # Create a large person group. - large_person_group_id = str(uuid.uuid4()) - print("Create a large person group {}.".format(large_person_group_id)) - face_client.large_person_group.create(large_person_group_id, large_person_group_id) - - for target_image_file_dictionary_name in target_image_file_dictionary.keys(): - person_id = face_client.large_person_group_person.create(large_person_group_id, target_image_file_dictionary_name).person_id - - # Limit TPS - time.sleep(0.25) - - # Create a person group person. - person = Person(name=target_image_file_dictionary_name, user_data="Person for sample", person_id=person_id) - - print("Create a large person group person {}.".format(person.name)) - - for target_image_file_name in target_image_file_dictionary[target_image_file_dictionary_name]: - # Add face to the person group person - print("Add face to the large person group person {} from image.".format(target_image_file_dictionary_name, target_image_file_name)) - face = face_client.large_person_group_person.add_face_from_url(large_person_group_id, person.person_id, image_url_prefix + target_image_file_name, target_image_file_name) - if face is None: - raise Exception("No persisted face from image {}".format(target_image_file_name)) - - # Start to train the large person group. - print("Train large person group {}.".format(large_person_group_id)) - face_client.large_person_group.train(large_person_group_id) - training_status = face_client.large_person_group.get_training_status(large_person_group_id) - print("Training status is {}".format(training_status.status)) - if training_status.status == TrainingStatusType.failed: - raise Exception("Training failed with message {}.".format(training_status.message)) - - - source_face_ids = [] - - # Detect faces from source image url. - detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) - - # Add detected face ids to source_face_ids - for detected_face in detected_faces: - source_face_ids.append(detected_face.face_id) - - # Identify example of identifying faces towards large person group. - identify_results = face_client.face.identify(source_face_ids, None, large_person_group_id) - if identify_results is None: - print("No person identified in the large person group for faces from the {}.".format(source_image_file_name)) - return - - for identify_result in identify_results: - person = face_client.large_person_group_person.get(large_person_group_id, identify_result.candidates[0].person_id) - print ("Person {} is identified for face: {} - {}, confidence: {}.".format(person.name, source_image_file_name, identify_result.face_id, identify_result.candidates[0].confidence)) - - # Delete the person group. - face_client.large_person_group.delete(large_person_group_id) - print("Delete the large person group {}.".format(large_person_group_id)) - print("") - - -if __name__ == "__main__": - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) - identify_in_person_group(face_base_url, "e312068d12604dee97f59230ff788d60") - identify_in_large_person_group(face_base_url, "e312068d12604dee97f59230ff788d60") \ No newline at end of file diff --git a/samples/vision/face/verify.py b/samples/vision/face/verify.py deleted file mode 100644 index 16c62ef..0000000 --- a/samples/vision/face/verify.py +++ /dev/null @@ -1,153 +0,0 @@ -from azure.cognitiveservices.vision.face import FaceClient -from azure.cognitiveservices.vision.face.models import Person -from msrest.authentication import CognitiveServicesCredentials -from samples.vision.face.common import detect_faces -import os -import uuid - -SUBSCRIPTION_KEY_ENV_NAME = os.environ.get("FACE_SUBSCRIPTION_KEY") -FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") - -def verify_face_to_face(face_base_url, subscription_key): - print("Sample of verify face to face.") - - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] - source_image_file_name1 = "Family1-Dad3.jpg" - source_image_file_name2 = "Family1-Son1.jpg" - - target_face_ids = [] - for image_file_name in target_image_file_names: - # Detect faces from target image url. - detected_faces = detect_faces(face_client, image_url_prefix + image_file_name) - target_face_ids.append(detected_faces[0].face_id) - - # Detect faces from source image file 1. - detected_faces1 = detect_faces(face_client, image_url_prefix + source_image_file_name1) - source_face_id1 = detected_faces1[0].face_id - - # Detect faces from source image file 2. - detected_faces2 = detect_faces(face_client, image_url_prefix + source_image_file_name2) - source_face_id2 = detected_faces2[0].face_id - - # Verification example for faces of the same person. - verify_result1 = face_client.face.verify_face_to_face(source_face_id1, target_face_ids[0]) - if verify_result1.is_identical: - print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(source_image_file_name1, target_image_file_names[0], verify_result1.confidence)) - else: - print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(source_image_file_name1, target_image_file_names[0], verify_result1.confidence)) - - # Verification example for faces of different persons. - verify_result2 = face_client.face.verify_face_to_face(source_face_id2, target_face_ids[0]) - if verify_result2.is_identical: - print("Faces from {} & {} are of the same (Negative) person, similarity confidence: {}.".format( - source_image_file_name2, target_image_file_names[0], verify_result2.confidence)) - else: - print("Faces from {} & {} are of different (Positive) persons, similarity confidence: {}.".format( - source_image_file_name2, target_image_file_names[0], verify_result2.confidence)) - print("") - - -def verify_in_person_group(face_base_url, subscription_key): - print("Sample of verify face to person group.") - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] - source_image_file_name1 = "Family1-Dad3.jpg" - - # Create a person group. - person_group_id = str(uuid.uuid4()) - print("Create a person group {}.".format(person_group_id)) - face_client.person_group.create(person_group_id, person_group_id) - - person_id = face_client.person_group_person.create(person_group_id, "Dad").person_id - - # Create a person group person. - p = Person(name="Dad", user_data="Person for sample", person_id=person_id) - print("Create a person group person {}.".format(p.name)) - - for target_image_file_name in target_image_file_names: - # Add face to the person group. - print("Add face to the person group person {} from image {}.".format(p.name, target_image_file_name)) - faces = face_client.person_group_person.add_face_from_url(person_group_id, p.person_id, image_url_prefix + target_image_file_name, target_image_file_name) - - if faces is None: - raise Exception("No persisted face from image {}.".format(target_image_file_name)) - - face_ids = [] - - # Add detected face id to face_ids - detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name1) - face_ids.append(detected_faces[0].face_id) - - # Verification example for faces of the same person. - verify_result = face_client.face.verify_face_to_person(face_ids[0], p.person_id, person_group_id) - if verify_result.is_identical: - print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(source_image_file_name1, p.name, verify_result.confidence)) - else: - print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(source_image_file_name1, p.name, verify_result.confidence)) - - # Delete the person group. - print("Delete the person group {}.".format(person_group_id)) - face_client.person_group.delete(person_group_id) - print("") - - -def verify_in_large_person_group(face_base_url, subscription_key): - print("Sample of verify face to large person group.") - face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) - - image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" - target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] - source_image_file_name1 = "Family1-Dad3.jpg" - - # Create a large person group. - large_person_group_id = str(uuid.uuid4()) - print("Create a large person group {}.".format(large_person_group_id)) - face_client.large_person_group.create(large_person_group_id, large_person_group_id) - - person_id = face_client.large_person_group_person.create(large_person_group_id, "Dad").person_id - - # Create a large person group person. - p = Person(name="Dad", user_data="Person for sample", person_id=person_id) - print("Create a large person group person {}.".format(p.name)) - - for target_image_file_name in target_image_file_names: - # Add face to the large person group. - print("Add face to the large person group person {} from image {}.".format(p.name, target_image_file_name)) - faces = face_client.large_person_group_person.add_face_from_url(large_person_group_id, p.person_id, - image_url_prefix + target_image_file_name, - target_image_file_name) - - if faces is None: - raise Exception("No persisted face from image {}.".format(target_image_file_name)) - - face_ids = [] - - # Add detected face id to face_ids - detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name1) - face_ids.append(detected_faces[0].face_id) - - # Verification example for faces of the same person. - verify_result = face_client.face.verify_face_to_person(face_ids[0], p.person_id, None, large_person_group_id) - if verify_result.is_identical: - print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format( - source_image_file_name1, p.name, verify_result.confidence)) - else: - print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format( - source_image_file_name1, p.name, verify_result.confidence)) - - # Delete the person group. - print("Delete the large person group {}.".format(large_person_group_id)) - face_client.large_person_group.delete(large_person_group_id) - print("") - - -if __name__ == "__main__": - face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) - verify_face_to_face(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) - verify_in_person_group(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) - verify_in_large_person_group(face_base_url, SUBSCRIPTION_KEY_ENV_NAME) \ No newline at end of file diff --git a/samples/vision/face_samples.py b/samples/vision/face_samples.py index f8595de..fecd930 100644 --- a/samples/vision/face_samples.py +++ b/samples/vision/face_samples.py @@ -1,84 +1,717 @@ -import os.path - from azure.cognitiveservices.vision.face import FaceClient -from azure.cognitiveservices.vision.face.models import FaceAttributeType from msrest.authentication import CognitiveServicesCredentials +from azure.cognitiveservices.vision.face.models import FaceAttributeType, HairColorType, TrainingStatusType, Person +import os +import uuid +import time SUBSCRIPTION_KEY_ENV_NAME = "FACE_SUBSCRIPTION_KEY" FACE_LOCATION = os.environ.get("FACE_LOCATION", "westcentralus") IMAGES_FOLDER = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images", "Face") -def face_detect(subscription_key): - """ImageAnalysisInStream. - This will analysis an image from a stream and return all available features. - """ +''' +Sample that returns the faces detected in an image + +Parameter face_client: a FaceClient instance. +Parameter image_url: the url for the image where faces are to be detected. +Returns: a list of faces detected in the image +''' +def detect_faces(face_client, image_url): + '''Detect faces in an image''' + + detected_faces = face_client.face.detect_with_url(image_url, True, False, None, None, False) + if detected_faces is None or len(detected_faces) == 0: + raise Exception('No face detected from image {}'.format(image_url)) + print("{} faces detected from image {}".format(len(detected_faces), image_url)) + if detected_faces[0] is None: + raise Exception("Parameter return_face_id of detect_with_stream or detect_with_url must be set to true (by default) for recognition purpose.") + return list(detected_faces) + + + +''' +Sample that prints out all of facial attributes for a list of images. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def face_detection(subscription_key): + '''Face detection and attributes''' + + ''' + Helper function for face_detection sample that returns a string representation of a person's accessories. + + Parameter accessories: the accessories detected in an image. + Return: string representation of a person's accessories. + ''' + + def get_accessories(accessories): + if len(accessories) == 0: + return "No accessories" + accessory_array = [] + + for i in range(len(accessories)): + accessory_array.append(str(accessories[i])) + return ",".join(accessory_array) + + ''' + Helper function for face_detection sample that determines the emotion a person is showing. + + Parameter emotion: the emotion object detected in an image. + Return: the emotion that the face is showing in the image. + ''' + + def get_emotion(emotion): + emotion_type = "" + emotion_value = 0.0 + if emotion.anger > emotion_value: + emotion_type = "anger" + emotion_value = emotion.anger + if emotion.contempt > emotion_value: + emotion_type = "contempt" + emotion_value = emotion.contempt + if emotion.disgust > emotion_value: + emotion_type = "disgust" + emotion_value = emotion.disgust + if emotion.fear > emotion_value: + emotion_type = "fear" + emotion_value = emotion.fear + if emotion.happiness > emotion_value: + emotion_type = "happiness" + emotion_value = emotion.happiness + if emotion.neutral > emotion_value: + emotion_type = "neutral" + emotion_value = emotion.neutral + if emotion.sadness > emotion_value: + emotion_type = "sadness" + emotion_value = emotion.sadness + if emotion.surprise > emotion_value: + emotion_type = "surprise" + return emotion_type + + ''' + Helper function for face_detection sample that determines the hair color detected for a face in an image. + + Parameter hair: hair object from a face detected in an image. + Return: the hair color. + ''' + + def get_hair(hair): + if len(hair.hair_color) == 0: + return "invisible" if hair.invisible else "bald" + return_color = HairColorType.unknown + max_confidence = 0.0 + + for hair_color in hair.hair_color: + if hair_color.confidence <= max_confidence: + continue; + max_confidence = hair_color.confidence + return_color = hair_color.color; + + return str(return_color) + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + image_file_names = ["detection1.jpg", + "detection2.jpg", + "detection3.jpg", + "detection4.jpg", + "detection5.jpg", + "detection6.jpg"] + for image_file_name in image_file_names: + detected_faces = face_client.face.detect_with_url(image_url_prefix + image_file_name, True, return_face_attributes = [ + FaceAttributeType.accessories, + 'age', + 'blur', + 'emotion', + 'exposure', + 'facialHair', + 'gender', + 'glasses', + 'hair', + 'headPose', + 'makeup', + 'noise', + 'occlusion', + 'smile' + ]) + if detected_faces == None or len(detected_faces) == 0: + raise Exception("No face detected from image {}".format(image_file_name)) + print("{} faces detected from image {}".format(len(detected_faces), image_file_name)) + if detected_faces[0].face_attributes is None: + raise Exception("Parameter return_face_attributes of detect_with_stream_async must be set to get face attributes.") + + for face in detected_faces: + print("Face attributes of {} Rectangle(Left/Top/Width/Height) : {} {} {} {}".format(image_file_name, face.face_rectangle.left, face.face_rectangle.top, face.face_rectangle.width, face.face_rectangle.height)) + print("Face attributes of {} Accessories : {}".format(image_file_name, get_accessories(face.face_attributes.accessories))) + print("Face attributes of {} Age : {}".format(image_file_name, face.face_attributes.age)) + print("Face attributes of {} Blur : {}".format(image_file_name, face.face_attributes.blur.blur_level)) + print("Face attributes of {} Emotion : {}".format(image_file_name, get_emotion(face.face_attributes.emotion))) + print("Face attributes of {} Exposure : {}".format(image_file_name, face.face_attributes.exposure.exposure_level)) + if face.face_attributes.facial_hair.moustache + face.face_attributes.facial_hair.beard + face.face_attributes.facial_hair.sideburns > 0: + print("Face attributes of {} FacialHair : Yes".format(image_file_name)) + else: + print("Face attributes of {} FacialHair : No".format(image_file_name)) + print("Face attributes of {} Gender : {}".format(image_file_name, face.face_attributes.gender)) + print("Face attributes of {} Glasses : {}".format(image_file_name, face.face_attributes.glasses)) + print("Face attributes of {} Hair : {}".format(image_file_name, get_hair(face.face_attributes.hair))) + print("Face attributes of {} HeadPose : Pitch: {}, Roll: {}, Yaw: {}".format(image_file_name, round(face.face_attributes.head_pose.pitch, 2), round(face.face_attributes.head_pose.roll, 2), round(face.face_attributes.head_pose.yaw, 2))) + if face.face_attributes.makeup.eye_makeup or face.face_attributes.makeup.lip_makeup: + print("Face attributes of {} Makeup : Yes".format(image_file_name)) + else: + print("Face attributes of {} Makeup : No".format(image_file_name)) + print("Face attributes of {} Noise : {}".format(image_file_name, face.face_attributes.noise.noise_level)) + print("Face attributes of {} Occlusion : EyeOccluded: {}, ForeheadOccluded: {}, MouthOccluded: {}".format(image_file_name, "Yes" if face.face_attributes.occlusion.eye_occluded else "No", + "Yes" if face.face_attributes.occlusion.forehead_occluded else "No", "Yes" if face.face_attributes.occlusion.mouth_occluded else "No")) + + print("Face attributes of {} Smile : {}".format(image_file_name, face.face_attributes.smile)) + print() + + +''' +Sample that shows how to detect similar faces from a list of images against a single image using face ids. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def find_similar_in_face_ids(subscription_key): + '''Finding similar faces using face ids''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", + "Family1-Daughter1.jpg", + "Family1-Mom1.jpg", + "Family1-Son1.jpg", + "Family2-Lady1.jpg", + "Family2-Man1.jpg", + "Family3-Lady1.jpg", + "Family3-Man1.jpg"] + + source_image_file_name = "findsimilar.jpg" + target_face_ids = [] + + for target_image_file_name in target_image_file_names: + # Detect faces from target image url. + faces = detect_faces(face_client, image_url_prefix + target_image_file_name) + # Add detected face id to target_face_ids + target_face_ids.append(faces[0].face_id) + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + # Find similar example of face id to face ids. + similar_results = face_client.face.find_similar(detected_faces[0].face_id, None, None, target_face_ids) + if len(similar_results) == 0: + print("No similar faces to {}.".format(source_image_file_name)) + for similar_result in similar_results: + print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, similar_result.face_id, similar_result.confidence)) + print("") + + +''' +Sample that shows how to detect similar faces from a list of images against a single image by placing the list of images in a face list. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def find_similar_in_face_list(subscription_key): + '''Detecting similar faces in a face list''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", + "Family1-Daughter1.jpg", + "Family1-Mom1.jpg", + "Family1-Son1.jpg", + "Family2-Lady1.jpg", + "Family2-Man1.jpg", + "Family3-Lady1.jpg", + "Family3-Man1.jpg"] + + source_image_file_name = "findsimilar.jpg" + # Create a face list. + face_list_id = str(uuid.uuid4()) + print("Create face list {}.".format(face_list_id)) + face_client.face_list.create(face_list_id, "face list for find_similar_in_face_list sample", "face list for find_similar_in_face_list sample") + + for target_image_file_name in target_image_file_names: + # Add face to face list. + faces = face_client.face_list.add_face_from_url(face_list_id, image_url_prefix + target_image_file_name, target_image_file_name) + if faces is None: + raise Exception("No face detected from image {}".format(target_image_file_name)) + print("Face from image {} is successfully added to the face list.".format(target_image_file_name)) + # Get persisted faces from the face list. + persisted_faces = list(face_client.face_list.get(face_list_id).persisted_faces) + if len(persisted_faces) == 0: + raise Exception("No persisted face in face list {}".format(face_list_id)) + + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + + # Find similar example of face id to face list. + similar_results = face_client.face.find_similar(detected_faces[0].face_id, face_list_id) + for similar_result in similar_results: + persisted_face = [pf for pf in persisted_faces if pf.persisted_face_id == similar_result.persisted_face_id][0] + if persisted_face is None: + print("persisted face not found in similar result.") + continue + print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, persisted_face.user_data, similar_result.confidence)) + + # Delete the face list. + face_client.face_list.delete(face_list_id) + print("Delete face list {}.".format(face_list_id)) + print("") + + + +''' +Sample that shows how to detect similar faces from a list of images against a single image by placing the list of images in a large face list. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def find_similar_in_large_face_list(subscription_key): + '''Detecting similar faces in a large face list''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", + "Family1-Daughter1.jpg", + "Family1-Mom1.jpg", + "Family1-Son1.jpg", + "Family2-Lady1.jpg", + "Family2-Man1.jpg", + "Family3-Lady1.jpg", + "Family3-Man1.jpg"] + + source_image_file_name = "findsimilar.jpg" + + # Create a large face list. + large_face_list_id = str(uuid.uuid4()) + print("Create large face list {}.".format(large_face_list_id)) + face_client.large_face_list.create(large_face_list_id, "large face list for find_similar_in_large_face_list sample", "large face list for find_similar_in_large_face_list sample") + + for target_image_file_name in target_image_file_names: + faces = face_client.large_face_list.add_face_from_url(large_face_list_id, image_url_prefix + target_image_file_name, target_image_file_name) + if faces is None: + raise Exception("No face detected from image {}.".format(target_image_file_name)) + print("Face from image {} is successfully added to the large face list.".format(target_image_file_name)) + + # Start to train the large face list. + print("Train large face list {}".format(large_face_list_id)) + face_client.large_face_list.train(large_face_list_id) + + # Wait until the training is completed. + while True: + time.sleep(1) + training_status = face_client.large_face_list.get_training_status(large_face_list_id) + print("Training status is {}.".format(training_status.status)) + if training_status.status != TrainingStatusType.running: + if training_status.status == TrainingStatusType.failed: + raise Exception("Training failed with message {}.".format(training_status.message)) + break + + # Get persisted faces from the large face list. + persisted_faces = list(face_client.large_face_list.list_faces(large_face_list_id)) + if len(persisted_faces) == 0: + raise Exception("No persisted face in large face list {}.".format(large_face_list_id)) + + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + + # Find similar example of face id to large face list. + similar_results = face_client.face.find_similar(detected_faces[0].face_id, None, large_face_list_id) + + for similar_result in similar_results: + persisted_face = [pf for pf in persisted_faces if pf.persisted_face_id == similar_result.persisted_face_id][0] + if persisted_face is None: + print("persisted face not found in similar result.") + continue + print("Faces from {} & {} are similar with confidence: {}.".format(source_image_file_name, persisted_face.user_data, similar_result.confidence)) + + # Delete the large face list. + face_client.large_face_list.delete(large_face_list_id) + print("Delete large face list {}.".format(large_face_list_id)) + print("") + + +''' +Sample of grouping faces based on similarity in a group and of grouping faces that don't have any other faces with similar features into a messy group. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def group_run(subscription_key): + '''Sample of grouping faces''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + image_file_names = ["Family1-Dad1.jpg", + "Family1-Daughter1.jpg", + "Family1-Mom1.jpg", + "Family1-Son1.jpg", + "Family2-Lady1.jpg", + "Family2-Man1.jpg", + "Family3-Lady1.jpg", + "Family3-Man1.jpg"] + + faces = {} + face_ids = [] + + for image_file_name in image_file_names: + # Detect faces from target image url. + detected_faces = detect_faces(face_client, image_url_prefix + image_file_name) + + # Add detected face id to face_ids and faces. + face_ids.append(detected_faces[0].face_id) + faces[str(detected_faces[0].face_id)] = image_file_name + + # Call grouping, the grouping result is a group collection, each group contains similar faces. + group_result = face_client.face.group(face_ids) + + # Face groups containing faces that are similar. + for i in range(len(group_result.groups)): + to_print = "Found face group {}: ".format(i + 1) + for face_id in group_result.groups[i]: + to_print = to_print + "{}".format(str(faces[str(face_id)])) + print(to_print + ".") + + # Messy group contains all faces which are not similar to any other faces. + if len(group_result.messy_group) > 0: + to_print = "Found messy face group: " + for face_id in group_result.messy_group: + to_print = to_print + str(faces[face_id]) + " " + print(to_print + ".") + + print("") + + +''' +Sample of how to identify faces in a group of people. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def identify_in_person_group(subscription_key): + '''Identifying faces in a group of people''' + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_dictionary = { + "Family1-Dad" : ["Family1-Dad1.jpg", "Family1-Dad2.jpg"], + "Family1-Mom" : ["Family1-Mom1.jpg", "Family1-Mom2.jpg"], + "Family1-Son" : ["Family1-Son1.jpg", "Family1-Son2.jpg"], + "Family1-Daughter" : ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"], + "Family2-Lady" : ["Family2-Lady1.jpg", "Family2-Lady2.jpg"], + "Family2-Man" : ["Family2-Man1.jpg", "Family2-Man2.jpg"] + } + source_image_file_name = "identification1.jpg" + + # Create a person group. + person_group_id = str(uuid.uuid4()) + print("Create a person group {}.".format(person_group_id)) + face_client.person_group.create(person_group_id, person_group_id) + + for target_image_file_dictionary_name in target_image_file_dictionary.keys(): + person_id = face_client.person_group_person.create(person_group_id, target_image_file_dictionary_name).person_id + + # Limit TPS + time.sleep(0.25) - faces = [jpgfile for jpgfile in os.listdir(IMAGES_FOLDER) if jpgfile.startswith("Family1")] - faces_ids = [] - - for face in faces: - with open(os.path.join(IMAGES_FOLDER, face), "rb") as face_fd: - # result type: azure.cognitiveservices.vision.face.models.DetectedFace - result = face_client.face.detect_with_stream( - face_fd, - # You can use enum from FaceAttributeType, or direct string - return_face_attributes=[ - FaceAttributeType.age, # Could have been the string 'age' - 'gender', - 'headPose', - 'smile', - 'facialHair', - 'glasses', - 'emotion', - 'hair', - 'makeup', - 'occlusion', - 'accessories', - 'blur', - 'exposure', - 'noise' - ] - ) - - if not result: - print("Unable to detect any face in {}".format(face)) - - detected_face = result[0] - faces_ids.append(detected_face.face_id) - - print("\nImage {}".format(face)) - print("Detected age: {}".format(detected_face.face_attributes.age)) - print("Detected gender: {}".format(detected_face.face_attributes.gender)) - print("Detected emotion: {}".format(detected_face.face_attributes.emotion.happiness)) - print("\n") + # Create a person group person. + person = Person(name=target_image_file_dictionary_name, user_data="Person for sample", person_id=person_id) + + print("Create a person group person {}.".format(person.name)) + + for target_image_file_name in target_image_file_dictionary[target_image_file_dictionary_name]: + # Add face to the person group person + print("Add face to the person group person {} from image.".format(target_image_file_dictionary_name, target_image_file_name)) + face = face_client.person_group_person.add_face_from_url(person_group_id, person.person_id, image_url_prefix + target_image_file_name, target_image_file_name) + if face is None: + raise Exception("No persisted face from image {}".format(target_image_file_name)) + + # Start to train the person group. + print("Train person group {}".format(person_group_id)) + face_client.person_group.train(person_group_id) + training_status = face_client.person_group.get_training_status(person_group_id) + print("Training status is {}".format(training_status.status)) + if training_status.status != TrainingStatusType.running: + if training_status.status == TrainingStatusType.failed: + raise Exception("Training failed with message {}.".format(training_status.message)) + + source_face_ids = [] + + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + + # Add detected face id to source_face_ids + for detected_face in detected_faces: + source_face_ids.append(detected_face.face_id) + + # Identify example of identifying faces towards person group. + identify_results = face_client.face.identify(source_face_ids, person_group_id) + if identify_results is None: + print("No person identified in the person group for faces from the {}.".format(source_image_file_name)) + return + + for identify_result in identify_results: + person = face_client.person_group_person.get(person_group_id, identify_result.candidates[0].person_id) + print("Person {} is identified for face: {} - {}, confidence: {}.".format(person.name, source_image_file_name, identify_result.face_id, identify_result.candidates[0].confidence)) + + # Delete the person group. + face_client.person_group.delete(person_group_id) + print("Delete the person group {}.".format(person_group_id)) + print("") + + +''' +Sample of how to identify faces in a large person group. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def identify_in_large_person_group(subscription_key): + '''Identifying faces in a large person group''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_dictionary = { + "Family1-Dad": ["Family1-Dad1.jpg", "Family1-Dad2.jpg"], + "Family1-Mom": ["Family1-Mom1.jpg", "Family1-Mom2.jpg"], + "Family1-Son": ["Family1-Son1.jpg", "Family1-Son2.jpg"], + "Family1-Daughter": ["Family1-Daughter1.jpg", "Family1-Daughter2.jpg"], + "Family2-Lady": ["Family2-Lady1.jpg", "Family2-Lady2.jpg"], + "Family2-Man": ["Family2-Man1.jpg", "Family2-Man2.jpg"] + } + source_image_file_name = "identification1.jpg" + + # Create a large person group. + large_person_group_id = str(uuid.uuid4()) + print("Create a large person group {}.".format(large_person_group_id)) + face_client.large_person_group.create(large_person_group_id, large_person_group_id) + + for target_image_file_dictionary_name in target_image_file_dictionary.keys(): + person_id = face_client.large_person_group_person.create(large_person_group_id, target_image_file_dictionary_name).person_id + + # Limit TPS + time.sleep(0.25) + + # Create a person group person. + person = Person(name=target_image_file_dictionary_name, user_data="Person for sample", person_id=person_id) + + print("Create a large person group person {}.".format(person.name)) + + for target_image_file_name in target_image_file_dictionary[target_image_file_dictionary_name]: + # Add face to the person group person + print("Add face to the large person group person {} from image.".format(target_image_file_dictionary_name, target_image_file_name)) + face = face_client.large_person_group_person.add_face_from_url(large_person_group_id, person.person_id, image_url_prefix + target_image_file_name, target_image_file_name) + if face is None: + raise Exception("No persisted face from image {}".format(target_image_file_name)) + + # Start to train the large person group. + print("Train large person group {}.".format(large_person_group_id)) + face_client.large_person_group.train(large_person_group_id) + training_status = face_client.large_person_group.get_training_status(large_person_group_id) + print("Training status is {}".format(training_status.status)) + if training_status.status == TrainingStatusType.failed: + raise Exception("Training failed with message {}.".format(training_status.message)) + + + source_face_ids = [] + + # Detect faces from source image url. + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name) + + # Add detected face ids to source_face_ids + for detected_face in detected_faces: + source_face_ids.append(detected_face.face_id) + + # Identify example of identifying faces towards large person group. + identify_results = face_client.face.identify(source_face_ids, None, large_person_group_id) + if identify_results is None: + print("No person identified in the large person group for faces from the {}.".format(source_image_file_name)) + return + + for identify_result in identify_results: + person = face_client.large_person_group_person.get(large_person_group_id, identify_result.candidates[0].person_id) + print ("Person {} is identified for face: {} - {}, confidence: {}.".format(person.name, source_image_file_name, identify_result.face_id, identify_result.candidates[0].confidence)) + + # Delete the person group. + face_client.large_person_group.delete(large_person_group_id) + print("Delete the large person group {}.".format(large_person_group_id)) + print("") + +''' +Sample of verifying whether faces detected as similar are the same person. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def verify_face_to_face(subscription_key): + '''Verifying face to face''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] + source_image_file_name1 = "Family1-Dad3.jpg" + source_image_file_name2 = "Family1-Son1.jpg" + + target_face_ids = [] + for image_file_name in target_image_file_names: + # Detect faces from target image url. + detected_faces = detect_faces(face_client, image_url_prefix + image_file_name) + target_face_ids.append(detected_faces[0].face_id) + + # Detect faces from source image file 1. + detected_faces1 = detect_faces(face_client, image_url_prefix + source_image_file_name1) + source_face_id1 = detected_faces1[0].face_id + + # Detect faces from source image file 2. + detected_faces2 = detect_faces(face_client, image_url_prefix + source_image_file_name2) + source_face_id2 = detected_faces2[0].face_id # Verification example for faces of the same person. - verify_result = face_client.face.verify_face_to_face( - faces_ids[0], - faces_ids[1], - ) - if verify_result.is_identical: - print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(faces[0], faces[1], verify_result.confidence)) + verify_result1 = face_client.face.verify_face_to_face(source_face_id1, target_face_ids[0]) + if verify_result1.is_identical: + print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(source_image_file_name1, target_image_file_names[0], verify_result1.confidence)) else: - print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(faces[0], faces[1], verify_result.confidence)) + print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(source_image_file_name1, target_image_file_names[0], verify_result1.confidence)) # Verification example for faces of different persons. - verify_result = face_client.face.verify_face_to_face( - faces_ids[1], - faces_ids[2], - ) + verify_result2 = face_client.face.verify_face_to_face(source_face_id2, target_face_ids[0]) + if verify_result2.is_identical: + print("Faces from {} & {} are of the same (Negative) person, similarity confidence: {}.".format( + source_image_file_name2, target_image_file_names[0], verify_result2.confidence)) + else: + print("Faces from {} & {} are of different (Positive) persons, similarity confidence: {}.".format( + source_image_file_name2, target_image_file_names[0], verify_result2.confidence)) + print("") + + +''' +Sample of verifying whether faces detected as similar in a group are of the same person. + +Parameter face_base_url: the endpoint for the Face API calls. +Parameter subscription_key: the Face API key. +''' +def verify_in_person_group(subscription_key): + '''Verifying face to person group''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] + source_image_file_name1 = "Family1-Dad3.jpg" + + # Create a person group. + person_group_id = str(uuid.uuid4()) + print("Create a person group {}.".format(person_group_id)) + face_client.person_group.create(person_group_id, person_group_id) + + person_id = face_client.person_group_person.create(person_group_id, "Dad").person_id + + # Create a person group person. + p = Person(name="Dad", user_data="Person for sample", person_id=person_id) + print("Create a person group person {}.".format(p.name)) + + for target_image_file_name in target_image_file_names: + # Add face to the person group. + print("Add face to the person group person {} from image {}.".format(p.name, target_image_file_name)) + faces = face_client.person_group_person.add_face_from_url(person_group_id, p.person_id, image_url_prefix + target_image_file_name, target_image_file_name) + + if faces is None: + raise Exception("No persisted face from image {}.".format(target_image_file_name)) + + face_ids = [] + + # Add detected face id to face_ids + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name1) + face_ids.append(detected_faces[0].face_id) + + # Verification example for faces of the same person. + verify_result = face_client.face.verify_face_to_person(face_ids[0], p.person_id, person_group_id) if verify_result.is_identical: - print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(faces[1], faces[2], verify_result.confidence)) + print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format(source_image_file_name1, p.name, verify_result.confidence)) else: - print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(faces[1], faces[2], verify_result.confidence)) + print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format(source_image_file_name1, p.name, verify_result.confidence)) + + # Delete the person group. + print("Delete the person group {}.".format(person_group_id)) + face_client.person_group.delete(person_group_id) + print("") + + +''' +Sample of verifying whether faces detected as similar in a large group are of the same person. +''' +def verify_in_large_person_group(subscription_key): + '''Verifying person to large person group''' + + face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) + face_client = FaceClient(face_base_url, CognitiveServicesCredentials(subscription_key)) + + image_url_prefix = "https://csdx.blob.core.windows.net/resources/Face/Images/" + target_image_file_names = ["Family1-Dad1.jpg", "Family1-Dad2.jpg"] + source_image_file_name1 = "Family1-Dad3.jpg" + + # Create a large person group. + large_person_group_id = str(uuid.uuid4()) + print("Create a large person group {}.".format(large_person_group_id)) + face_client.large_person_group.create(large_person_group_id, large_person_group_id) + + person_id = face_client.large_person_group_person.create(large_person_group_id, "Dad").person_id + + # Create a large person group person. + p = Person(name="Dad", user_data="Person for sample", person_id=person_id) + print("Create a large person group person {}.".format(p.name)) + + for target_image_file_name in target_image_file_names: + # Add face to the large person group. + print("Add face to the large person group person {} from image {}.".format(p.name, target_image_file_name)) + faces = face_client.large_person_group_person.add_face_from_url(large_person_group_id, p.person_id, + image_url_prefix + target_image_file_name, + target_image_file_name) + + if faces is None: + raise Exception("No persisted face from image {}.".format(target_image_file_name)) + + face_ids = [] + + # Add detected face id to face_ids + detected_faces = detect_faces(face_client, image_url_prefix + source_image_file_name1) + face_ids.append(detected_faces[0].face_id) + + # Verification example for faces of the same person. + verify_result = face_client.face.verify_face_to_person(face_ids[0], p.person_id, None, large_person_group_id) + if verify_result.is_identical: + print("Faces from {} & {} are of the same (Positive) person, similarity confidence: {}.".format( + source_image_file_name1, p.name, verify_result.confidence)) + else: + print("Faces from {} & {} are of different (Negative) persons, similarity confidence: {}.".format( + source_image_file_name1, p.name, verify_result.confidence)) + + # Delete the person group. + print("Delete the large person group {}.".format(large_person_group_id)) + face_client.large_person_group.delete(large_person_group_id) + print("") + + + + if __name__ == "__main__": import sys, os.path sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) - #from tools import execute_samples - face_detect("e312068d12604dee97f59230ff788d60") - #execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) \ No newline at end of file + from samples.tools import execute_samples + execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) \ No newline at end of file From c6f6987b3f309c4656a4d7588e3d01e509888ff9 Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Fri, 16 Nov 2018 16:10:10 -0800 Subject: [PATCH 09/17] edited Custom Search Samples, as CustomSearchAPI has been switched to CustomSearchClient --- .idea/workspace.xml | 321 +++++++++++++-------- samples/language/text_analytics_samples.py | 2 +- samples/search/custom_search_samples.py | 8 +- samples/search/visual_search_samples.py | 9 +- 4 files changed, 206 insertions(+), 134 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f1fbe51..b3c42b9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -12,32 +12,41 @@ - + - - + + + + + - + + + + - + + + + - + - + @@ -45,50 +54,74 @@ + + + + + + + + + + + + - + - - + + - - + + - - - - - + + - + - - + + - + - - + + + + + - + - - + + + + + + + + + + + + + + @@ -128,10 +161,11 @@ @@ -155,11 +189,6 @@ - - - - - @@ -169,7 +198,7 @@ - + @@ -208,21 +237,21 @@ - - + + - + - + - - + + + - @@ -404,6 +434,16 @@ 34 @@ -433,13 +473,6 @@ - - - - - - - @@ -447,9 +480,6 @@ - - - @@ -457,17 +487,9 @@ - - - - - - - - - + @@ -492,132 +514,181 @@ - - - - - - - - - + + + + + + + + + - + + - + + - + - - - - - + + - + - - + + - + - - - - - + + - + - - + + - + - - + + - + + + + + + + + + + + - + - + + + + - - + + - + - - + + + + + + + + + + + + + - + + + + + + + + + + + + - - + + - + - + - - - - - + + - + - - + + + + + + + + + - + - - + + - + + + + + + + + - - + + + + + + + + + - + diff --git a/samples/language/text_analytics_samples.py b/samples/language/text_analytics_samples.py index 4f33fe5..e25faa1 100644 --- a/samples/language/text_analytics_samples.py +++ b/samples/language/text_analytics_samples.py @@ -125,4 +125,4 @@ def sentiment(subscription_key): # sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) # from tools import execute_samples #execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) - language_extraction("027b004824b34b009f533ab4274f2dbb") + sentiment("027b004824b34b009f533ab4274f2dbb") diff --git a/samples/search/custom_search_samples.py b/samples/search/custom_search_samples.py index 83265c0..7028cda 100644 --- a/samples/search/custom_search_samples.py +++ b/samples/search/custom_search_samples.py @@ -1,4 +1,4 @@ -from azure.cognitiveservices.search.customsearch import CustomSearchAPI +from azure.cognitiveservices.search.customsearch import CustomSearchClient from msrest.authentication import CognitiveServicesCredentials SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMSEARCH_SUBSCRIPTION_KEY" @@ -8,7 +8,7 @@ def custom_search_web_page_result_lookup(subscription_key): This will look up a single query (Xbox) and print out name and url for first web result. """ - client = CustomSearchAPI(CognitiveServicesCredentials(subscription_key)) + client = CustomSearchClient(CognitiveServicesCredentials(subscription_key)) try: web_data = client.custom_instance.search(query="xbox", custom_config=1) @@ -27,6 +27,6 @@ def custom_search_web_page_result_lookup(subscription_key): if __name__ == "__main__": import sys, os.path - sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) - from tools import execute_samples + sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) + from samples.tools import execute_samples execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) diff --git a/samples/search/visual_search_samples.py b/samples/search/visual_search_samples.py index 98a7464..a516920 100644 --- a/samples/search/visual_search_samples.py +++ b/samples/search/visual_search_samples.py @@ -196,7 +196,8 @@ def search_insights_token_with_crop_area(subscription_key): print("Couldn't find image tags!") if __name__ == "__main__": - import sys, os.path - sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) - from tools import execute_samples - execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) \ No newline at end of file + # import sys, os.path + # sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) + # from tools import execute_samples + # execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) + search_url_with_filters("a0568b2af2ad4db3b0756035e2b43ad6") From ba597d4d14833c5e0eab2cc4b1c5237d0a82e914 Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Fri, 16 Nov 2018 16:31:37 -0800 Subject: [PATCH 10/17] got rid of changes I accidentally made to text analytics and visual search samples --- .idea/workspace.xml | 97 ++++++++++++---------- samples/language/text_analytics_samples.py | 9 +- samples/search/visual_search_samples.py | 9 +- 3 files changed, 63 insertions(+), 52 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index b3c42b9..22a8f66 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -55,12 +55,25 @@ + + + + + + + + + + + + + - + @@ -88,28 +101,28 @@ - + - + - - - - - + + - + - - + + + + + @@ -117,8 +130,8 @@ - - + + @@ -163,9 +176,9 @@ @@ -198,13 +211,13 @@ - + - + @@ -526,17 +539,6 @@ - - - - - - - - - - - @@ -628,16 +630,6 @@ - - - - - - - - - - @@ -654,8 +646,8 @@ - - + + @@ -672,7 +664,7 @@ - + @@ -688,7 +680,28 @@ - + + + + + + + + + + + + + + + + + + + + + + diff --git a/samples/language/text_analytics_samples.py b/samples/language/text_analytics_samples.py index e25faa1..17b87fe 100644 --- a/samples/language/text_analytics_samples.py +++ b/samples/language/text_analytics_samples.py @@ -121,8 +121,7 @@ def sentiment(subscription_key): if __name__ == "__main__": - # import sys, os.path - # sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) - # from tools import execute_samples - #execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) - sentiment("027b004824b34b009f533ab4274f2dbb") + import sys, os.path + sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) + from tools import execute_samples + execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) diff --git a/samples/search/visual_search_samples.py b/samples/search/visual_search_samples.py index a516920..f0b632a 100644 --- a/samples/search/visual_search_samples.py +++ b/samples/search/visual_search_samples.py @@ -196,8 +196,7 @@ def search_insights_token_with_crop_area(subscription_key): print("Couldn't find image tags!") if __name__ == "__main__": - # import sys, os.path - # sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) - # from tools import execute_samples - # execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) - search_url_with_filters("a0568b2af2ad4db3b0756035e2b43ad6") + import sys, os.path + sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) + from tools import execute_samples + execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) From b7f3c9f790d4586141e9024088b38a7684890fcc Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Mon, 19 Nov 2018 15:02:22 -0800 Subject: [PATCH 11/17] working on custom image search --- .idea/workspace.xml | 304 +++++++++++------- samples/search/custom_image_search_samples.py | 36 +++ samples/search/visual_search_samples.py | 4 +- 3 files changed, 231 insertions(+), 113 deletions(-) create mode 100644 samples/search/custom_image_search_samples.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 22a8f66..12670d2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -12,10 +12,10 @@ - + - - + + @@ -25,7 +25,7 @@ - + @@ -35,18 +35,18 @@ - + - + - + @@ -54,10 +54,10 @@ - + - + @@ -68,72 +68,79 @@ - + + + + + + - - - - - + + - + - - + + + + + - + - - + + + + + + + - + - - + + - + - - + + - - + + - - - - - + + - + - - + + - + @@ -177,14 +184,15 @@ - @@ -194,7 +202,6 @@ - @@ -223,14 +230,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -310,6 +280,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -585,13 +518,7 @@ - - - - - - - + @@ -710,14 +637,11 @@ - + - - - @@ -728,63 +652,115 @@ - - - - - - - - - + + + + + + + + + - - - - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - + - - + + - - - + - + + + + + + + + + + + - - + + + + + + + + + + + + diff --git a/README.md b/README.md index 6b41a9b..582e89c 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ This project framework provides examples for the following services: * Using the **Bing Autosuggest SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-autosuggest) for the [Autosuggest API](https://azure.microsoft.com/services/cognitive-services/autosuggest/) * Using the **Bing Custom Search SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customsearch) for the [Custom Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/) +* Using the **Bing Custom Image Search SDK** [azure-cognitiveservices-search-customsearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-customimagesearch) for the [Custom Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-custom-search/) * Using the **Bing Entity Search SDK** [azure-cognitiveservices-search-entitysearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-entitysearch) for the [Entity Search API](https://azure.microsoft.com/services/cognitive-services/bing-entity-search-api/) * Using the **Bing Image Search SDK** [azure-cognitiveservices-search-imagesearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-imagesearch) for the [Image Search API](https://azure.microsoft.com/services/cognitive-services/bing-image-search-api/) * Using the **Bing News Search SDK** [azure-cognitiveservices-search-newssearch](http://pypi.python.org/pypi/azure-cognitiveservices-search-newssearch) for the [News Search API](https://azure.microsoft.com/services/cognitive-services/bing-news-search-api/) @@ -85,6 +86,7 @@ We provide several meta-packages to help you install several packages at a time. 4. Set up the environment variable `TEXTANALYTICS_SUBSCRIPTION_KEY` with your key if you want to execute TextAnalytics tests. You might override too `TEXTANALYTICS_LOCATION` (westcentralus by default). 3. Set up the environment variable `AUTOSUGGEST_SUBSCRIPTION_KEY` with your key if you want to execute Autosuggest tests. 3. Set up the environment variable `CUSTOMSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomSearch tests. +3. Set up the environment variable `CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute CustomImageSearch tests. 3. Set up the environment variable `ENTITYSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute EntitySearch tests. 4. Set up the environment variable `IMAGESEARCH_SUBSCRIPTION_KEY` with your key if you want to execute ImageSearch tests. 4. Set up the environment variable `NEWSSEARCH_SUBSCRIPTION_KEY` with your key if you want to execute NewsSearch tests. diff --git a/samples/search/custom_image_search_samples.py b/samples/search/custom_image_search_samples.py index 29f3e6c..92e2b9b 100644 --- a/samples/search/custom_image_search_samples.py +++ b/samples/search/custom_image_search_samples.py @@ -1,25 +1,26 @@ -from azure.cognitiveservices.search.customsearch import CustomSearchClient -from azure.cognitiveservices.search.imagesearch import ImageSearchAPI +from azure.cognitiveservices.search.customimagesearch import CustomImageSearchAPI from msrest.authentication import CognitiveServicesCredentials +SUBSCRIPTION_KEY_ENV_NAME = "CUSTOMIMAGESEARCH_SUBSCRIPTION_KEY" -def custom_image_search_result_lookup(subscription_key, custom_config): + +def custom_image_search_result_lookup(subscription_key): """CustomImageSearchResultLookup This will look up a single query (Xbox) and print out number of results, insights token, thumbnail url, content url for the first image result """ - client = ImageSearchAPI(credentials=CognitiveServicesCredentials(subscription_key)) + client = CustomImageSearchAPI(credentials=CognitiveServicesCredentials(subscription_key)) try: - image_results = client.images.search(query="Xbox", custom_config=1) + image_results = client.custom_instance.image_search(query="Xbox", custom_config=1) print("Searched for Query \" Xbox \"") # WebPages - if (len(image_results) > 0): + if (len(image_results.value) > 0): # find the first web page - first_image_result = image_results[0] + first_image_result = image_results.value[0] if (first_image_result): - print("Image result count: {}".format(len(image_results))) + print("Image result count: {}".format(len(image_results.value))) print("First image insights token: {}".format(first_image_result.image_insights_token)) print("First image thumbnail url: {}".format(first_image_result.thumbnail_url)) print("First image content url: {}".format(first_image_result.content_url)) @@ -33,4 +34,7 @@ def custom_image_search_result_lookup(subscription_key, custom_config): if __name__ == "__main__": - custom_image_search_result_lookup("65bcdb9d6e9e4b7faf271ef2e2dc0fa4", 1) \ No newline at end of file + import sys, os.path + sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) + from tools import execute_samples + execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) \ No newline at end of file From f3d59397ab55c53bed421e9b56f02763e69aee48 Mon Sep 17 00:00:00 2001 From: Isabella Cai Date: Tue, 20 Nov 2018 12:57:02 -0800 Subject: [PATCH 13/17] added .idea folder to gitignore --- .gitignore | 3 + .../cognitive-services-python-sdk-samples.iml | 8 - .idea/misc.xml | 4 - .idea/modules.xml | 8 - .idea/vcs.xml | 6 - .idea/workspace.xml | 768 ------------------ 6 files changed, 3 insertions(+), 794 deletions(-) delete mode 100644 .idea/cognitive-services-python-sdk-samples.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 7bbc71c..fde5395 100644 --- a/.gitignore +++ b/.gitignore @@ -99,3 +99,6 @@ ENV/ # mypy .mypy_cache/ + +# idea +.idea/ diff --git a/.idea/cognitive-services-python-sdk-samples.iml b/.idea/cognitive-services-python-sdk-samples.iml deleted file mode 100644 index bd50bdc..0000000 --- a/.idea/cognitive-services-python-sdk-samples.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index e82ee89..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 0cb4c96..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index fbefcba..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,768 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SUBSCRIPTION_KEY_ENV_NAME - for i in range - face_list - Guid - detect - username - subscription - subscription_key - subscription_key_env - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -