Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import boto3
  2.  
  3. from io import BytesIO
  4. from PIL import Image
  5.  
  6. if __name__== "__main__":
  7.     session = boto3.Session(region_name="ap-southeast-1")
  8.    
  9.     rekognition = session.client('rekognition')
  10.     s3 = session.client('s3')
  11.    
  12.     response = rekognition.detect_faces(
  13.         Image={
  14.             'S3Object': {
  15.                 'Bucket': 'test-bucket-julo',
  16.                 'Name': '2.jpeg',
  17.             },
  18.         },
  19.     )
  20.    
  21.     file_byte_string = s3.get_object(Bucket='ml.adindabudi.me', Key='rekognition/samples/2.jpeg')['Body'].read()
  22.    
  23.     img = Image.open(BytesIO(file_byte_string))
  24.     img_width, img_height = img.size
  25.     left = response['FaceDetails'][0]['BoundingBox']['Left'] * img_width
  26.     top = response['FaceDetails'][0]['BoundingBox']['Top'] * img_height
  27.     width = response['FaceDetails'][0]['BoundingBox']['Width'] * img_width
  28.     height = response['FaceDetails'][0]['BoundingBox']['Height'] * img_height
  29.     cropped = img.crop((left , top , (left + width), (top + height)))
  30.     imgByteArr = BytesIO()
  31.     cropped.save(imgByteArr, format=img.format)
  32.     imgByteArr = imgByteArr.getvalue()
  33.     response = rekognition.index_faces(
  34.         CollectionId='julo',
  35.         Image={
  36.             'Bytes': imgByteArr,
  37.         },
  38.         QualityFilter='LOW'
  39.     )
  40.    
  41.     print(response)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement