Search code examples
azureazure-cognitive-servicesface-recognitionface-api

How can we show name with face id from azure face api?


i have a persongroup where i have stored all users id and name. I looked into the azure documentation about face api but i didn't got anything for showing the name according to face id. Before a while i used to store the name and id in a different python file from where i used to show the name. But then i thought we already store the name and id in azure why to create another file and store the data for 2 times instead of that showing the name directly from face api. Can anyone guide me to how to show the name with id?

def face_identify(faceId):
    global person
    KEY = parser.get('AzCognitiveServices','Ocp-Apim-Subscription-Key')
 

    headers = {
        # Request headers
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': KEY,
    }

    params = urllib.urlencode({
    })


    if faceId is None:
        person = ""
        return

    group = parser.get('AzCognitiveServices','PersonGroupId')
    candidatesreturned = list(parser.get('AzCognitiveServices','maxNumOfCandidatesReturned'))
    threshold = parser.get('AzCognitiveServices','confidenceThreshold')

        
    body = "{'personGroupId' : '" + group + "','faceIds' : [%s],'maxNumOfCandidatesReturned' : 1,'confidenceThreshold': 0.5}" % str("'%s'"%faceId)
    logger.info(body)
    
    try:
        uri_base = parser.get('AzCognitiveServices','BaseURL')
        path_to_face_api = "/face/v1.0/identify"
        response = requests.post(uri_base + path_to_face_api,
                                 data=body,
                                 headers=headers,
                                 params=params,timeout=int(parser.get('AzCognitiveServices','Timeout')))
                       
        #response = conn.getresponse()
        data = response.json()
        logger.info('face id %s',data)
    
        candidates = data[0]["candidates"]
        
        if len(candidates) > 0:
            person = str(candidates[0]["personId"])
        else:
            person = ""

        print(person)
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))

Solution

  • If you store the name of your persons when you create those items, you just have to get your PersonGroup by id when you have found a person.

    So in your code, it is at this step:

    person = str(candidates[0]["personId"])
    

    Here you have the personId of the found person. Use it with the GET method from the PersonGroup Person, see documentation here. It needs your PersonGroupId and PersonId.

    You will retrieve the name you are looking for