[Azure]Face model doesn't work

LoaKang 25 Reputation points
2024-01-22T19:07:23.43+00:00

I can detecting faces but when finding similar faces, it has request error. key, locations, images were all set correctly. Even if i try few sec or min later, also has same error. How can i fix it?

Traceback (most recent call last):

  File "/Users/loa/Desktop/Code/azure-python/face_api.py", line 61, in <module>

    similar_faces = find_similar_faces(face_client, face_ID, list(map(lambda x: x.face_id, faces_2)))

                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/Users/loa/Desktop/Code/azure-python/face_api.py", line 39, in find_similar_faces

    return face_client.face.find_similar(face_id=face_ID, face_ids=face_IDs)

           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  File "/Users/loa/anaconda3/envs/codezero/lib/python3.11/site-packages/azure/cognitiveservices/vision/face/operations/_face_operations.py", line 136, in find_similar

    raise models.APIErrorException(self._deserialize, response)

azure.cognitiveservices.vision.face.models._models_py3.APIErrorException: (InvalidRequest) Invalid request has been sent.
Azure Face
Azure Face
An Azure service that provides artificial intelligence algorithms that detect, recognize, and analyze human faces in images.
154 questions
{count} votes

Accepted answer
  1. navba-MSFT 17,395 Reputation points Microsoft Employee
    2024-01-23T06:50:42.0633333+00:00

    @강다영 Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    .

    Could you please confirm the if you have submitted the Face Recognition intake form and got the approval ? This is part of Microsoft’s Responsible AI principles to ensure the service is used appropriately. Once the form is approved, you can use this face service. .
    If you are using an older version of azure-cognitiveservices-vision-face python SDK, make sure to upgrade it to most recent.

    pip install --upgrade azure-cognitiveservices-vision-face

    . I tried the find_similar operation using my below code and it worked fine. Could you please use the below sample and check if that helps ?

    import os
    import uuid
    import time
    
    from azure.cognitiveservices.vision.face import FaceClient
    from msrest.authentication import CognitiveServicesCredentials
    from azure.cognitiveservices.vision.face.models import TrainingStatusType, Person
    
    # NOTE: Replace this with a valid Face subscription key.
    SUBSCRIPTION_KEY = "31b48fXXXXXXXXX30072b87"
    
    # You must use the same region as you used to get your subscription
    # keys. For example, if you got your subscription keys from westus,
    # replace "westcentralus" with "westus".
    FACE_LOCATION = "eastus"
    
    face_base_url = "https://{}.api.cognitive.microsoft.com".format(FACE_LOCATION)
    face_client = FaceClient(face_base_url, CognitiveServicesCredentials(SUBSCRIPTION_KEY))
    
    # This image should contain a single face.
    remote_image_URL_1 = "https://labXXXXage.blob.core.windows.net/training/pic1.jpg"
    
    # This image should contain several faces, at least one of which is similar to the face in remote_image_URL_1.
    remote_image_URL_2 = "https://labXXXXage.blob.core.windows.net/training/pic2.jpg"
    
    # Detect faces in a remote image.
    def detect_faces(face_client, image_url):
    	print ("Detecting faces...")
    	detected_faces = face_client.face.detect_with_url(url=image_url)
    	if not detected_faces:
    		raise Exception('No face detected from image {}'.format(image_url))
    	if not detected_faces[0]:
    		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 detected_faces
    
    # Find similar faces to @face_ID in @face_IDs.
    def find_similar_faces(face_client, face_ID, face_IDs):
    	print("Finding similar faces ...")
    	return face_client.face.find_similar(face_id=face_ID, face_ids=face_IDs)
    
    # Detect a face in the first image.
    faces_1 = detect_faces(face_client, remote_image_URL_1)
    if not faces_1[0]:
    	print("No faces detected in " + remote_image_URL_1 + ".")
    else:
    	print("Face IDs of faces detected in " + remote_image_URL_1 + ":")
    	for x in faces_1: print (x.face_id)
    
    	print("Using first face ID.")
    	face_ID = faces_1[0].face_id
    
    	# Detect a list of faces in the second image.
    	faces_2 = detect_faces(face_client, remote_image_URL_2)
    	if not faces_2[0]:
    		print("No faces detected in " + remote_image_URL_2 + ".")
    	else:
    		print("Face IDs of faces detected in " + remote_image_URL_2 + ":")
    		for x in faces_2: print (x.face_id)
    
    		# Search the faces detected in the second image to find a similar face to the first one.
    		similar_faces = find_similar_faces(face_client, face_ID, list(map(lambda x: x.face_id, faces_2)))
    		if not similar_faces[0]:
    			print("No similar faces found in " + remote_image_URL_2 + ".")
    		else:
    			print("Similar faces found in " + remote_image_URL_2 + ":")
    			for face in similar_faces:
    				face_ID = face.face_id
    				# SimilarFace only contains a Face ID, Persisted Face ID, and confidence score.
    				# So we look up the Face ID in the list of DetectedFaces found in
    				# remote_image_URL_2 to get the rest of the face information.
    				face_info = next(x for x in faces_2 if x.face_id == face_ID)
    				if face_info:
    					print("Face ID: " + face_ID)
    					print("Face rectangle:")
    					print("Left: " + str(face_info.face_rectangle.left))
    					print("Top: " + str(face_info.face_rectangle.top))
    					print("Width: " + str(face_info.face_rectangle.width))
    					print("Height: " + str(face_info.face_rectangle.height))
    

    Hope this helps. If you have any follow-up questions, please let me know. I would be happy to help.


0 additional answers

Sort by: Most helpful