-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
55 lines (41 loc) · 1.44 KB
/
Copy pathindex.py
File metadata and controls
55 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#
# Lambda function detect labels in image using Amazon Rekognition
#
from __future__ import print_function
import boto3
import json
import os
from boto3.dynamodb.conditions import Key, Attr
minCofidence = 60
def handler(event, context):
for record in event['Records']:
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
rekFunction(bucket, key)
def rekFunction(bucket, key):
print("Detected the following image in S3")
print("Bucket: " + bucket + " key name: " + key)
client = boto3.client("rekognition")
response = client.detect_labels(Image={"S3Object": {"Bucket": bucket, "Name": key}},
MaxLabels=10, MinConfidence=minCofidence)
# Get the service resource
dynamodb = boto3.resource("dynamodb")
# Instantiate a table resource object
imageLabelsTable = os.environ["TABLE"]
table = dynamodb.Table(imageLabelsTable)
# Put item into table
table.put_item(
Item={"Image": key}
)
objectsDetected = []
for label in response["Labels"]:
newItem = label["Name"]
objectsDetected.append(newItem)
objectNum = len(objectsDetected)
itemAtt = f"object{objectNum}"
response = table.update_item(
Key={"Image": key},
UpdateExpression=f"set {itemAtt} = :r",
ExpressionAttributeValues={":r": f"{newItem}"},
ReturnValues="UPDATED_NEW"
)