Rek_lambda.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. """
  2. This is the starter code to detecting the faces/labels
  3. in our images of Kyle.
  4. It's mostly complete but we didn't know all the specifics
  5. to the API calls.
  6. If you look at the boto3 documentation you will be able to find
  7. all the answers!
  8. """
  9. import boto3
  10. import json
  11. import os
  12. ######
  13. # Getting our environment variables
  14. # Make sure to set these in your lambda function
  15. ######
  16. # this is the name of the rekognition collection you've created
  17. rekognition_collection_id = os.environ['collection']
  18. # output on the dashboard
  19. sns_topic_arn = os.environ['sns_arn']
  20. # this is your "Team ID" you see at the top of the player
  21. # dashboard like: 5a0e59338b894b57b48828b315a40afb
  22. # **IT IS NOT YOUR TEAM NAME**
  23. team_id = os.environ['team_id']
  24. # Rekognition allows you to specify a "tag" with your image.
  25. # so later when we detect a matching face, we read this tag
  26. # so we know the name or title of the person we've matched
  27. external_image_id = 'Kyle'
  28. # our boto3 rekognition client
  29. rekognition_client=boto3.client('rekognition')
  30. sns_client=boto3.client('sns')
  31. def facial_recognition(key, bucket):
  32. response = rekognition_client.index_faces(
  33. CollectionId=rekognition_collection_id,
  34. Image={
  35. 'S3Object': {
  36. 'Bucket': bucket,
  37. 'Name': key
  38. }
  39. }
  40. )
  41. print "Index Faces response:\n %s" % response
  42. # see if Rekognition detected any faces in this image
  43. if not response['FaceRecords']:
  44. # no faces detected, so we send back a false
  45. return False
  46. # we found faces, so let's see if they match our CEO
  47. # iterating through the faces found in the submitted image
  48. for face in response['FaceRecords']:
  49. face_id = face['Face']['FaceId']
  50. print "Face ID: %s" % face_id
  51. # send the detected face to Rekognition to see if it matches
  52. # anything in our collection
  53. response = rekognition_client.search_faces(
  54. CollectionId=rekognition_collection_id,
  55. FaceId=face_id
  56. )
  57. print "Searching faces response:\n %s" % response
  58. # checking if there were any matches
  59. if not response['FaceMatches']:
  60. # not our CEO
  61. return False
  62. # we recognized a face, let's see if it matches our CEO
  63. for match in response['FaceMatches']:
  64. if "ExternalImageId" in match['Face'] and match['Face']["ExternalImageId"] == external_image_id:
  65. # we have a picture of our CEO
  66. print "We've found our CEO!! Huzzah!"
  67. return True
  68. # At this point, we have other faces in our collection that
  69. # match this face, but it didn't match our CEO
  70. print "not kyle :("
  71. return False
  72. def get_labels(key, bucket):
  73. response = rekognition_client.detect_labels(
  74. Image={
  75. 'S3Object': {
  76. 'Bucket': bucket,
  77. 'Name': key
  78. }
  79. },
  80. MinConfidence=50
  81. )
  82. raw_labels = response['Labels']
  83. top_five=[]
  84. for x in range(0,5):
  85. top_five.append(raw_labels[x]['Name'])
  86. return top_five
  87. def send_sns(message):
  88. """
  89. We'll use SNS to send our response back to the master account
  90. with our labels
  91. """
  92. print message
  93. response = sns_client.publish(
  94. TopicArn=sns_topic_arn,
  95. Message=json.dumps(message)
  96. )
  97. return
  98. def lambda_handler(event, context):
  99. """
  100. Main Lambda handler
  101. """
  102. print json.dumps(event)
  103. # our incoming event is the S3 put event notification
  104. s3_message = event
  105. # get the object key and bucket name
  106. key = s3_message['Records'][0]['s3']['object']['key']
  107. bucket = s3_message['Records'][0]['s3']['bucket']['name']
  108. # first we need to see if our CEO is in this picture
  109. proceed = facial_recognition(key, bucket)
  110. return_message={
  111. "key":key,
  112. "team_id":team_id
  113. }
  114. # now we move on to detecting what's in the image
  115. if proceed:
  116. labels = get_labels(key, bucket)
  117. return_message['labels']=labels
  118. return_message['kyle_present']=True
  119. else:
  120. # we need to signal back that our CEO wasn't in the picture
  121. return_message['kyle_present']=False
  122. send_sns(return_message)