Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more.
0

aio.send(image)
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

aio.send(image)

by groznigaspr on Tue Mar 17, 2020 1:06 pm

i am having trouble with uploading image into the feed. i capture image on raspberry camera and lower it's quality and then encode it. i try to send it withaio.send and get following error. i found out that picture is encoded correctly(becasue i dragged the same picture into the feed and cheched its value it's the same as encoded picture. I tried to decode that image and it was displayed correctly)

i am getting error

Traceback (most recent call last):
File "uplodepicture.py", line 31, in <module>
aio.send(picam_feed,value)
File "/home/pi/.local/lib/python3.5/site-packages/Adafruit_IO/client.py", line 154, in send_data
return self.create_data(feed, payload)
File "/home/pi/.local/lib/python3.5/site-packages/Adafruit_IO/client.py", line 254, in create_data
return Data.from_dict(self._post(path, data._asdict()))
File "/home/pi/.local/lib/python3.5/site-packages/Adafruit_IO/client.py", line 126, in _post
data=json.dumps(data))
File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'/9j/4AAQSkZJRgABAQAAAQABAAD........ a lot of text.........j/2Q==' is not JSON serializable



CODE:


import io
from Adafruit_IO import*
from PIL import Image
import base64
import picamera

aio = Client("xxxxxxxxxxx", "xxxxxxxxxxxxxxx")

camera=picamera.PiCamera()
picam_feed=aio.feeds('picam')

stream = io.BytesIO()
camera.capture(stream, format='jpeg', resize=(800, 600))
stream.seek(0)

print('> optimizing')
image = Image.open(stream, 'r')

optim_stream = io.BytesIO()
image.save(optim_stream, format='jpeg', quality=70, optimize=True)
optim_stream.seek(0)

print('> converting')
value = base64.b64encode(optim_stream.read())

if len(value) > 102400:
print ("image is too big!")
time.sleep(2)

aio.send(picam_feed,value)

groznigaspr
 
Posts: 1
Joined: Tue Mar 17, 2020 12:49 pm

Re: aio.send(image)

by brubell on Thu Mar 19, 2020 10:19 am

JSON can't handle raw bytes, that why you're getting this error. Could you try converting the base64 bytes object into a string for the call to aio.send:

Code: Select all | TOGGLE FULL SIZE
b64_string = value.decode('utf-8')

brubell
 
Posts: 792
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.