in rekognitionlambda/index.py [0:0]
def rekFunction(ourBucket, ourKey):
# Clean the string to add the colon back into requested name which was substitued by Amplify Library.
safeKey = replaceSubstringWithColon(ourKey)
print('Currently processing the following image')
print('Bucket: ' + ourBucket + ' key name: ' + safeKey)
detectLabelsResults = {}
# Try and retrieve labels from Amazon Rekognition, using the confidence level we set in minConfidence var
try:
detectLabelsResults = rekognition_client.detect_labels(Image={'S3Object': {'Bucket':ourBucket, 'Name':safeKey}},
MaxLabels=10,
MinConfidence=minConfidence)
except ClientError as e:
logging.error(e)
# Create our array and dict for our label construction
objectsDetected = []
imageLabels = {
'image': safeKey
}
# Add all of our labels into imageLabels by iterating over response['Labels']
for label in detectLabelsResults['Labels']:
newItem = label['Name']
objectsDetected.append(newItem)
objectNum = len(objectsDetected)
itemAtt = f"object{objectNum}"
# We now have our shiny new item ready to put into DynamoDB
imageLabels[itemAtt] = newItem
# Instantiate a table resource object of our environment variable
imageLabelsTable = os.environ['TABLE']
table = dynamodb.Table(imageLabelsTable)
# Put item into table
try:
table.put_item(Item=imageLabels)
except ClientError as e:
logging.error(e)
return