Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Passing an Image to AWS
-
Hi, all. I am very new to Pythonista and somewhat new to Python. So, apologies if this is a basic question. But, Googling and searching this forum has not helped.
I'm trying to take a photo on my iPhone via Pythonista and pass it to the AWS Rekognition API for image analysis. (I'm using the AWS Python SDK.) Taking the photo works fine. But, when I try to pass the photo to the AWS Rekognition API, I get error messages saying that I am passing an image when str, bytes, or an os.PathLike object are required.
I have tried searching for Python code to convert the image file to bytes, but I am never successful. I still get image object errors. And, attempts to covert an image to a bytearray generated error messages that an image object isn't iterable.
Can anyone give me pointers on how to convert a photo taken by Pythonista to bytes? Or, is there a better solution here that I'm not considering?
Here are four different code snippets I have tried when submitting to AWS:
with open(img, 'rb') as image: response = client.detect_faces( Image={ 'Bytes': image.read(), }, Attributes=[ 'ALL', ] ) with open(img, 'rb') as image: response = client.detect_faces( Image={ 'Bytes': image, }, Attributes=[ 'ALL', ] ) response = client.detect_faces( Image={ 'Bytes': image.read(), }, Attributes=[ 'ALL', ] ) response = client.detect_faces( Image={ 'Bytes': image, }, Attributes=[ 'ALL', ] )
My attempts to convert images to bytes have been so fruitless that I don't really have useful code to share.
Thanks for any help!
Marc :-)
-
Here is tan example from the docs:
import boto3 if __name__ == "__main__": imageFile='input.jpg' client=boto3.client('rekognition') with open(imageFile, 'rb') as image: response = client.detect_labels(Image={'Bytes': image.read()}) print('Detected labels in ' + imageFile) for label in response['Labels']: print (label['Name'] + ' : ' + str(label['Confidence'])) print('Done...')
It would be helpful if you could expand the error, and tap Print Traceback, then paste the full traceback here.
It might be a good idea to set image.read() to a variable first, so you can ensure that it is not None, etc. There is nothing obviously wrong in your first example, but you should ensure that you are passing in a real image.
-
Thanks @JonB. I finally got this working. Below is my full code. I think io.BytesIO() was what i was primarily missing. That said, if you have a better way of doing this, please feel free to share.
#Prompt user to take a photo and save to variable import photos first_img = photos.capture_image() #Scale down photo size and save to new variable import PIL from PIL import Image basewidth = 600 img = first_img wpercent = (basewidth / float(img.size[0])) hsize = int((float(img.size[1]) * float(wpercent))) img = img.resize((basewidth, hsize), PIL.Image.ANTIALIAS) #Convert photo to bytes snd save to new variable import io imgByteArr = io.BytesIO() img.save(imgByteArr, format='PNG') final_img = imgByteArr.getvalue() #Import AWS SDK and initalize client with needed credentials import boto3 client = boto3.client('rekognition') #Send photo to AWS for analysis response = client.detect_faces( Image={ 'Bytes': final_img, }, Attributes=[ 'ALL', ] ) #Print response for AWS API print(response)