0

Where am I going wrong? I'm using Python 3.

Error: Top-level exception occurred in callback while processing a message Traceback (most recent call last): File "pollyNode.py", line 394, in callback converted_sound = convertAudioToByteArray(("sounds" + device.get_id() + "/speech" + str(count) + ".mp3")) File "pollyNode.py", line 274, in convertAudioToByteArray image_data = base64.b64encode(audio).decode('utf-8').encode('ascii') File "/usr/lib/python3.8/base64.py", line 58, in b64encode encoded = binascii.b2a_base64(s, newline=False) TypeError: a bytes-like object is required, not 'str' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/ubuntu/.local/lib/python3.8/site-packages/google/cloud/pubsub_v1/subscriber/_protocol/streaming_pull_manager.py", line 71, in _wrap_callback_errors callback(message) File "pollyNode.py", line 415, in callback except binascii.Error: NameError: name 'binascii' is not defined 

Code:

def convertAudioToByteArray(audio): image_data = base64.b64encode(audio).decode('utf-8').encode('ascii') return image_data converted_sound = convertAudioToByteArray(("sounds" + device.get_id() + "/speech" + str(count) + ".mp3")) payload_json = {'type': 'POLRES', 'img_name':data['img_name'], 'audio': converted_sound, 'node_id': device.get_id()} payload = json.dumps(payload_json) device_project_id = args.project_id device_registry_id = args.registry_id device_id = data['dev_id'] device_region = args.cloud_region print("Publishing Polly results to device " + data['dev_id'] + "for image " + data['img_name']) 

1 Answer 1

1

Encode your string audio with .encode("utf8"):

image_data = base64.b64encode(audio.encode("utf8")).decode('utf-8').encode('ascii') 
Sign up to request clarification or add additional context in comments.

1 Comment

This resolved it but it gave me a new error on the payload line below: payload_json = {'type': 'POLRES', 'img_name':data['img_name'], 'audio': converted_sound, 'node_id': device.get_id()} payload = json.dumps(payload_json) Error: TypeError: Object of type bytes is not JSON serializable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.