-
Notifications
You must be signed in to change notification settings - Fork 0
/
classification_lambda.py
39 lines (30 loc) · 1.15 KB
/
classification_lambda.py
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
import json
import base64
#from sagemaker.serializers import IdentitySerializer
# Fill this in with the name of your deployed model
ENDPOINT = 'image-classification-2022-12-15-11-03-18-834' ## TODO: fill in
import boto3
def lambda_handler(event, context):
# Decode the image data
image = base64.b64decode(event['body']["image_data"])
bucket = event['body']["s3_bucket"]
key = event['body']["s3_key"]
runtime= boto3.client('runtime.sagemaker')
response = runtime.invoke_endpoint(EndpointName=ENDPOINT,
ContentType='image/png',
Body=image)
# Make a prediction:
predictions = json.loads(response['Body'].read().decode()) ## TODO: fill in
#print(predictions)
# We return the data back to the Step Function
event["inferences"] = predictions
print(event)
return {
'statusCode': 200,
'body': {
"image_data": event['body']["image_data"],
"s3_bucket": bucket,
"s3_key": key,
"inferences": predictions
}
}