in lambda/app.py [0:0]
def verify_challenge(challenge_id):
if not challenge_id:
return 422, {'message': 'Missing path parameter \'challengeId\''}
# Looking up challenge on DynamoDB table
item = table.get_item(Key={'id': challenge_id})
if 'Item' not in item:
return 404, {'message': 'Challenge not found'}
challenge = read_item(item['Item'])
# Getting frames from challenge
frames = challenge['frames']
# Invoking Rekognition with parallel threads
with ThreadPoolExecutor(max_workers=10) as pool:
futures = [
pool.submit(
detect_faces, frame
) for frame in frames
]
frames = [r.result() for r in as_completed(futures)]
frames.sort(key=lambda frame: frame['key'])
# Setting up state manager
first_state = FaceState(challenge)
state_manager = StateManager(first_state)
current_state_name = state_manager.get_current_state_name()
# Processing Rekognition results with state manager
for frame in frames:
state_manager.process(frame)
current_state_name = state_manager.get_current_state_name()
if current_state_name in {"SuccessState", "FailState"}: # Final state
break
# Returning result based on final state
response = {'success': current_state_name == "SuccessState"}
# Updating challenge on DynamoDB table
table.update_item(
Key={'id': challenge_id},
UpdateExpression='set #frames = :frames, #success = :success',
ExpressionAttributeNames={
'#frames': 'frames',
'#success': 'success'
},
ExpressionAttributeValues={
':frames': write_item(frames),
':success': response['success']
},
ReturnValues='NONE'
)
return 200, response