|
| 1 | +import os |
| 2 | +import uuid |
| 3 | +import time |
| 4 | + |
| 5 | +from azure.cognitiveservices.vision.face import FaceClient |
| 6 | +from msrest.authentication import CognitiveServicesCredentials |
| 7 | +from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person |
| 8 | + |
| 9 | +# NOTE: Replace this with a valid Face subscription key. |
| 10 | +SUBSCRIPTION_KEY = "INSERT KEY HERE" |
| 11 | + |
| 12 | +# You must use the same region as you used to get your subscription |
| 13 | +# keys. For example, if you got your subscription keys from westus, |
| 14 | +# replace "westcentralus" with "westus". |
| 15 | +# |
| 16 | +# Free trial subscription keys are generated in the westcentralus |
| 17 | +# region. If you use a free trial subscription key, you shouldn't |
| 18 | +# need to change the region. |
| 19 | +FACE_LOCATION = "westcentralus" |
| 20 | + |
| 21 | +face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION) |
| 22 | +face_client = FaceClient(face_base_url, CognitiveServicesCredentials(SUBSCRIPTION_KEY)) |
| 23 | + |
| 24 | +# This image should contain a single face. |
| 25 | +remote_image_URL_1 = "https://www.biography.com/.image/t_share/MTQ1MzAyNzYzOTgxNTE0NTEz/john-f-kennedy---mini-biography.jpg" |
| 26 | + |
| 27 | +# This image should contain several faces, at least one of which is similar to the face in remote_image_URL_1. |
| 28 | +remote_image_URL_2 = "https://www.biography.com/.image/t_share/MTQ1NDY3OTIxMzExNzM3NjE3/john-f-kennedy---debating-richard-nixon.jpg" |
| 29 | + |
| 30 | +# Detect faces in a remote image. |
| 31 | +def detect_faces(face_client, image_url): |
| 32 | + print ("Detecting faces...") |
| 33 | + detected_faces = face_client.face.detect_with_url(url=image_url) |
| 34 | + if not detected_faces: |
| 35 | + raise Exception('No face detected from image {}'.format(image_url)) |
| 36 | + if not detected_faces[0]: |
| 37 | + raise Exception("Parameter return_face_id of detect_with_stream or detect_with_url must be set to true (by default) for recognition purpose.") |
| 38 | + return detected_faces |
| 39 | + |
| 40 | +# Find similar faces to @face_ID in @face_IDs. |
| 41 | +def find_similar_faces(face_client, face_ID, face_IDs): |
| 42 | + print("Finding similar faces ...") |
| 43 | + return face_client.face.find_similar(face_id=face_ID, face_ids=face_IDs) |
| 44 | + |
| 45 | +# Detect a face in the first image. |
| 46 | +faces_1 = detect_faces(face_client, remote_image_URL_1) |
| 47 | +if not faces_1[0]: |
| 48 | + print("No faces detected in " + remote_image_URL_1 + ".") |
| 49 | +else: |
| 50 | + print("Face IDs of faces detected in " + remote_image_URL_1 + ":") |
| 51 | + for x in faces_1: print (x.face_id) |
| 52 | + |
| 53 | + print("Using first face ID.") |
| 54 | + face_ID = faces_1[0].face_id |
| 55 | + |
| 56 | + # Detect a list of faces in the second image. |
| 57 | + faces_2 = detect_faces(face_client, remote_image_URL_2) |
| 58 | + if not faces_2[0]: |
| 59 | + print("No faces detected in " + remote_image_URL_2 + ".") |
| 60 | + else: |
| 61 | + print("Face IDs of faces detected in " + remote_image_URL_2 + ":") |
| 62 | + for x in faces_2: print (x.face_id) |
| 63 | + |
| 64 | + # Search the faces detected in the second image to find a similar face to the first one. |
| 65 | + similar_faces = find_similar_faces(face_client, face_ID, list(map(lambda x: x.face_id, faces_2))) |
| 66 | + if not similar_faces[0]: |
| 67 | + print("No similar faces found in " + remote_image_URL_2 + ".") |
| 68 | + else: |
| 69 | + print("Similar faces found in " + remote_image_URL_2 + ":") |
| 70 | + for face in similar_faces: |
| 71 | + face_ID = face.face_id |
| 72 | + # SimilarFace only contains a Face ID, Persisted Face ID, and confidence score. |
| 73 | + # So we look up the Face ID in the list of DetectedFaces found in |
| 74 | + # remote_image_URL_2 to get the rest of the face information. |
| 75 | + face_info = next(x for x in faces_2 if x.face_id == face_ID) |
| 76 | + if face_info: |
| 77 | + print("Face ID: " + face_ID) |
| 78 | + print("Face rectangle:") |
| 79 | + print("Left: " + str(face_info.face_rectangle.left)) |
| 80 | + print("Top: " + str(face_info.face_rectangle.top)) |
| 81 | + print("Width: " + str(face_info.face_rectangle.width)) |
| 82 | + print("Height: " + str(face_info.face_rectangle.height)) |
0 commit comments