triggerinputmyrekognitionpushsns.py
import boto3
import json
import os
from datetime import datetime
# The SNSPublisher class publishes messages to SNS topics
ORDER_DETAILS = {"image": {"S": "matsuko5.jpg"}, "label_3": {"S": "Face"}}
# STUDENT TODO 2: Set ARN for SNS topic for order messages
TOPIC_ARN_ORDER = "arn:aws:sns:us-east-1:986846499074:relables"
snsTopic = 'arn:aws:sns:us-east-1:986846499074:relables'
name = 'relable'
sns = boto3.client('sns')
ddb = boto3.client('dynamodb')
def publish_order_msgs(orderDetails):
topicArn=TOPIC_ARN_ORDER
sns = boto3.resource('sns')
publish_order(sns, topicArn, orderDetails)
print("Order topic published")
def publish_order(sns, topic_arn, json_order):
"""Sends the order message
Keyword arguments:
sns -- SNS service resource
topicArn -- ARN for the topic
jsonStr -- the order in JSON format
"""
topic = sns.Topic(topic_arn)
topic.publish(Message=json_order)
def sent_message(msg):
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName=name)
time = 1
msg_list = [{'Id' : '{}'.format(time), 'MessageBody' : '\'image\': {0}'.format(msg)}]
response = queue.send_messages(Entries=msg_list)
print(response)
return response
def querylabeltems(filter, value):
response = ddb.query(
TableName='myrekognition',
KeyConditions={
filter: {
'AttributeValueList': [
{
'S': value
}
],
'ComparisonOperator': "EQ"
}
}
)
return response
def lambda_handler(event, context):
dynamo_message = event
print(json.dumps(event))
# NewImage = dynamo_message['Records'][0]['dynamodb']['NewImage'] #type dict
Newkey = dynamo_message['Records'][0]['dynamodb']['Keys']['image']
vulue = Newkey['S']
NewImage = querylabeltems('image', vulue)
responce = json.dumps(NewImage['Items'])
responce = responce.lstrip('[')
responce = responce.rstrip(']')
json_responce = json.loads(responce)
send_flag = False
for y in json_responce:
dep = json_responce[y]
if (dep['S'] == 'Face'): #Face label ckeck
print("find Face")
send_flag = True
else :
pass
if send_flag:
#msg = dynamo_message['Records'][0]['dynamodb']['Keys']
sent_message(json_responce['image']);
else:
pass
print("Successfully")
search-face-by-image.py
import json
import boto3
import os
from boto3.session import Session
access_key = os.environ['ACCESS_KEY']
secret_key = os.environ['SECRET_KEY']
default_region = os.environ['DEFAULT_REGION']
session = Session(
aws_access_key_id = access_key,
aws_secret_access_key = secret_key,
region_name = default_region
)
def search(bucket, key):
bucket=bucket
collectionId='takahashi'
fileName=key
threshold = 80
maxFaces=2
s3 = session.resource('s3')
object = s3.Object(bucket, fileName)
print(object.key)
client=session.client('rekognition', default_region)
response=client.search_faces_by_image(CollectionId=collectionId,
Image={'S3Object':{'Bucket':bucket,'Name':fileName}},
FaceMatchThreshold=threshold,
MaxFaces=maxFaces)
faceMatches=response['FaceMatches']
print ('Matching faces')
for match in faceMatches:
print ('FaceId:' + match['Face']['FaceId'])
print ('Similarity: ' + "{:.2f}".format(match['Similarity']) + "%")
print('SearchedFaceConfidence: ' + "{:.2f}".format(response['SearchedFaceConfidence']) + "%")
data=[]
data.append(key)
data.append(collectionId)
data.append(str(round(response['SearchedFaceConfidence'], 2)))
return data
def put_db(data):
client = session.client('dynamodb')
response = client.put_item(
Item={
'key': {
'S': data[0],
},
'collectionId': {
'S': data[1],
},
'Confidence': {
'S': data[2],
},
},
ReturnConsumedCapacity='TOTAL',
TableName='Album',
)
print(response)
def buckup(bucket, fileName):
client = session.client('s3')
responce = client.copy_object(Bucket='takahashi-replica', Key='image5.jpg', CopySource={'Bucket': 'takahashiminami', 'Key': 'image5.jpg'})
responce = '{"status": "OK"}'
return responce
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
response=search(bucket, key)
put_db(response)
buckup(bucket, key)
rekognitiondep.py
import json
import urllib
import boto3
import json
import time
print('Loading function')
s3 = boto3.client('s3')
rekognition = boto3.client('rekognition')
ddb = boto3.client('dynamodb')
def get_labels(key, bucket):
response = rekognition.detect_labels(
Image={
'S3Object':{'Bucket':bucket,'Name':key}
},
MinConfidence=50
)
raw_labels = response['Labels']
top_five=[]
for x in range(0,5):
top_five.append(raw_labels[x]['Name'])
return top_five
def get_Celebrities(key, bucket):
response = rekognition.recognize_celebrities(
Image={
'S3Object':{'Bucket':bucket,'Name':key}
}
)
Celebritie=[]
for celebrity in response['CelebrityFaces']:
#print ("Name: " + str(celebrity['Name']))
#print ("celebrity: {:.2f}".format(celebrity['Face']['Confidence']))
#print ("Position:")
#print (" Left: {:.2f}".format(celebrity['Face']['BoundingBox']['Height']))
#print (" Top: {:.2f}".format(celebrity['Face']['BoundingBox']['Top']))
Celebritie.append(celebrity['Name'])
Celebritie.append(celebrity['Face']['Confidence'])
Celebritie.append(celebrity['Face']['BoundingBox']['Height'])
Celebritie.append(celebrity['Face']['BoundingBox']['Top'])
return Celebritie
def ddb_molding(key, messeage):
label_list = ['label_1', 'label_2', 'label_3', 'label_4', 'label_5']
return_message = {}
return_message["image"] = {
"S": str(key)
}
for i, label in enumerate(messeage):
return_message[str(label_list[i])] = {
"S": str(messeage[i])
}
return return_message
def ddb_celebrity_molding(ddb_json, messeage):
label_list = ['Name', 'Confidence', 'BoundingBox_Height', 'BoundingBox_Top']
return_message = ddb_json
for i, label in enumerate(messeage):
return_message[str(label_list[i])] = {
"S": str(messeage[i])
}
return return_message
def ddb_input(table, data):
ddb.put_item(TableName = table, Item = data)
return
def lambda_handler(event, context):
print(json.dumps(event))
s3_message = event
key = s3_message['Records'][0]['s3']['object']['key']
bucket = s3_message['Records'][0]['s3']['bucket']['name']
proceed = get_labels(key, bucket)
if proceed:
ddb_json = ddb_molding(key, proceed)
if "Face" in proceed:
Celebrity = get_Celebrities(key, bucket)
ddb_json = ddb_celebrity_molding(ddb_json, Celebrity)
else:
pass
print(json.dumps(ddb_json))
#print(json.dumps(Celebrity))
ddb_input("myrekognition", ddb_json)
print("end")
recognition.py
import boto3
import json
import os
rekognition_collection_id = os.environ['collection']
sns_topic_arn = os.environ['sns_arn']
team_id = os.environ['team_id']
external_image_id = 'Matsuko'
# our boto3 rekognition client
rekognition_client=boto3.client('rekognition')
sns_client=boto3.client('sns')
def facial_recognition(key, bucket):
response = rekognition_client.index_faces(
CollectionId=rekognition_collection_id,
Image={
'S3Object':{'Bucket':bucket,'Name':key}
}
)
print ("Index Faces response:\n %s" % response)
if not response['FaceRecords']:
return False
for face in response['FaceRecords']:
face_id = face['Face']['FaceId']
print ("Face ID: %s" % face_id)
# send the detected face to Rekognition to see if it matches
# anything in our collection
response = rekognition_client.search_faces(
CollectionId=rekognition_collection_id,
FaceId=face_id,
FaceMatchThreshold=50,
MaxFaces=1
)
print ("Searching faces response:\n %s" % response)
if not response['FaceMatches']:
return False
for match in response['FaceMatches']:
if "ExternalImageId" in match['Face'] and match['Face']["ExternalImageId"] == external_image_id:
print ("We've found our CEO!! Huzzah!")
return True
print ("not kyle :(")
return False
def get_labels(key, bucket):
response = rekognition_client.detect_labels(
Image={
'S3Object':{'Bucket':bucket,'Name':key}
},
MinConfidence=50
)
raw_labels = response['Labels']
top_five=[]
for x in range(0,5):
top_five.append(raw_labels[x]['Name'])
return top_five
def send_sns(message):
print (message)
response = sns_client.publish(
TopicArn=sns_topic_arn,
Message=(json.dumps(message)),
#Message=u'message',
Subject=u'subject'
)
return
def lambda_handler(event, context):
print (json.dumps(event))
s3_message = event
key = s3_message['Records'][0]['s3']['object']['key']
bucket = s3_message['Records'][0]['s3']['bucket']['name']
proceed = facial_recognition(key, bucket)
return_message={
"key":key,
"team_id":team_id
}
if proceed:
labels = get_labels(key, bucket)
return_message['labels']=labels
return_message['kyle_present']=True
else:
return_message['kyle_present']=False
send_sns(return_message)
replace.py
from __future__ import print_function
import json
import base64
print('Loading function')
def lambda_handler(event, context):
output = []
for record in event['records']:
payload = base64.b64decode(record['data'])
payload = json.loads(payload)
s = payload['StatusTime']
s = s.replace(' ', 'T')
s = s[:-4]
data = {
'Name': payload['Name'],
'StatusTime': s,
'Distance': payload['Distance'],
'MinMagicPoints': payload['MinMagicPoints'],
'MaxMagicPoints': payload['MaxMagicPoints'],
'MinHealthPoints': payload['MinHealthPoints'],
'MaxHealthPoints': payload['MaxHealthPoints'],
}
data = json.dumps(data)
b_data=data.encode()
b64encoded=base64.b64encode(b_data)
b64encoded = b64encoded.decode()
print(b64encoded)
output_record = {
'recordId': record['recordId'],
'result': 'Ok',
'data': b64encoded,
'approximateArrivalTimestamp': record['approximateArrivalTimestamp']
}
output.append(output_record)
print('Successfully processed {} records.'.format(output))
return {'records': output}
PoringSQSandDeleteObject.py
import json
import boto3
BUCKET_NAME = 'recognitoneventbacket'
TOPIC_ARN = 'arn:aws:sns:us-east-1:986846499074:mygmail'
msg = 'send test massage Hello hamasaki'
subject = 'test now!!'
def delete_objects(key):
s3 = boto3.resource('s3')
#s3.Bucket('recognitoneventbacket')
#s3.delete_object(Bucket='bucket_name', Key=u'key')
s3.Object('recognitoneventbacket', key).delete()
return
def sent_email(key):
sns = boto3.client('sns')
request = {
'TopicArn' : TOPIC_ARN,
'Message' : 'Delete ' + key + '!!',
'Subject' : subject
}
response = sns.publish(**request)
print('response')
def lambda_handler(event, context):
sqs_message = event
image_key = sqs_message['Records'][0]['body'] # Dictionary type is more convenient
image = '{' + image_key + '}' # Format the string into json format String type now!!
iimage = image.replace('\'', '"') # Convert ' into " and format them into JSON format
image = json.loads(iimage) # Load JSON format string as dictionary type
#{'image': {'S': 'test2/matsuko.png'}}
print(image['image']['S']) # test2/matsuko.png
sent_email(image['image']['S'])
delete_objects(image['image']['S'])
recofnition2.py
import boto3
import os
import cfnresponse
import json
# grabbing env vars
sourceBucket = os.environ['sourceBucket']
destBucket = os.environ['destBucket']
playerBucket = os.environ['playerBucket']
prefix = os.environ['prefix']
# setting up clients
s3 = boto3.resource('s3')
client = boto3.client('s3')
training_images = [
'kyle_gameday.png',
'kyle_linkedin.jpg',
'kyle_phonetool.jpeg',
'kyle_random1.jpg',
'kyle_youtube1.png'
]
def deleteAll(bucket, prefix):
paginator = client.get_paginator('list_objects')
for result in paginator.paginate(Bucket=bucket, Delimiter='/', Prefix=prefix):
# checking if there are nested folders
if result.get('CommonPrefixes') is not None:
for subdir in result.get('CommonPrefixes'):
# delete files in the nested folders
deleteAll(bucket, subdir['Prefix'])
if result.get('Contents') is not None:
for file in result.get('Contents'):
print "Deleting %s" % file['Key']
client.delete_object(
Bucket=bucket,
Key=file['Key'],
)
def send_response(event, context):
responseData = {}
cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "CustomResourcePhysicalID")
def lambda_handler(event, context):
print json.dumps(event)
if event['RequestType'] == 'Create':
# copying the training images to the player bucket
for training_image in training_images:
print "Copying %s" % training_image
copy_source = {
'Bucket': sourceBucket,
'Key': "{}{}".format(prefix, training_image)
}
s3.meta.client.copy(copy_source, destBucket, training_image)
# now we signal back to CFN we're done
send_response(event, context)
if event['RequestType'] == 'Delete':
# need to delete objects in the S3 bucket so the CFN stack
# can delete the bucket
deleteAll(destBucket,'')
deleteAll(playerBucket,'')
# now we signal back to CFN we're done
send_response(event, context)
if event['RequestType'] == 'Update':
# now we signal back to CFN we're done
send_response(event, context)