def function_main()

in src/rekognition/compare-faces/handler.py [0:0]


def function_main(event:Mapping[str,Any],_=None):
  inputRequest = InputRequest(event)

  '''
  Retrieve the face information.
  If an exact match exists, we're done.
  '''
  faces = face_table_client.get_faces(inputRequest.user_id)
  if inputRequest.face_id.lower() in faces.keys():
    return { 
      'IsMatch':True,
      'Reason': 'Known FaceId detected.'
    }
  
  '''
  Otherwise compare a historical record against the input
  '''
  historical_face_id, historical_image = choose_random_face(faces)
  try:
    response = rek_client.compare_faces(
      SimilarityThreshold=0.9,
      SourceImage={
        'Bytes': b64decode(historical_image.value)
      },
      TargetImage={
        'Bytes': inputRequest.image_bytes
      })

    '''
    Confirm these are approximately the same image.
    '''
    if len(response['FaceMatches']) == 0:
      return { 
        'IsMatch':False,
        'Reason': 'Property $.FaceMatches is empty.'
      }

    for match in response['FaceMatches']:
      similarity:float = match['Similarity']
      if similarity < SIMILARITY_THRESHOLD:
        return { 
          'IsMatch':False,
          'Reason': 'Similarity comparison was below threshold (%f < %f).' % (similarity, SIMILARITY_THRESHOLD)
        }

    return { 
      'IsMatch':True,
      'Reason': 'All checks passed.'
    }
  except Exception as error:
    print('Comparing({}) to face_id[{}] failed - {}'.format(
      inputRequest.user_id, historical_face_id, str(error)))
    raise error