AgentX
AgentX

Reputation: 1462

Boto3 and Python3: Can't convert 'bytes' object to str implicitly

I am trying to use AWS Rekognition, detect_text API. I am using Boto3 along with Python 3.

Here is my relevant code:

with open(file_path, 'rb') as file:
  data = file.read()

response = self._rekognition.detect_text(Image={'Bytes': data})

This code worked with Python2.7 but is failing with Python3. I am getting the following error:

File "...", line 39, in extract_text
response = self._rekognition.detect_text(Image={'Bytes': data})
...
...
k_date = self._sign(('AWS4' + key).encode('utf-8'),
TypeError: Can't convert 'bytes' object to str implicitly

Any ideas what I need to change here.

Upvotes: 2

Views: 2838

Answers (1)

Rahul
Rahul

Reputation: 11550

In python 3 you may need to convert bytes to str using.

data.decode('utf-8')

or your can read the text file as text itself.

try:

with open(file_path, encoding='utf-8') as file:
  data = file.read()
response = self._rekognition.detect_text(Image={'Bytes': data})

I have no Idea about what _rekognition.detect accepts but you can try.

Upvotes: 2

Related Questions