Guest User

Untitled

a guest
Jan 22nd, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. import io
  2. import boto3
  3. import json
  4. import numpy as np
  5. from PIL import Image, ImageOps
  6. import cv2
  7. import random
  8.  
  9. runtime= boto3.client('runtime.sagemaker')
  10. s3_client = boto3.client('s3', aws_access_key_id='xxx',aws_secret_access_key='xxx')
  11.  
  12. def handler(event,context):
  13. print("Received event")
  14. jsondata={}
  15. filename = str(random.randint(1,1001))+".jpg"
  16. print(filename)
  17. obj = s3_client.get_object(Bucket='coco-valid-images', Key=filename)
  18. object_content = obj['Body'].read()
  19. img=Image.open(io.BytesIO(object_content))
  20. res = cv2.resize(np.asarray(img), dsize=(224, 224))
  21. data = np.array(res)
  22. data = data.reshape([1] + list(data.shape))
  23. print(data.shape)
  24. jsondata['instances']=data.tolist()
  25. payload = json.dumps(jsondata)
  26.  
  27. response = runtime.invoke_endpoint(EndpointName='tf-mobilenet-v2-coco-2018-03-29',
  28. ContentType='application/json', Body=payload);
  29. result = json.loads(response['Body'].read().decode())
  30.  
  31. return {
  32. 'body': result,
  33. 'headers': {
  34. 'Content-Type': 'text/plain'
  35. },
  36. 'statusCode': 200
  37. }
Add Comment
Please, Sign In to add comment