I am trying to send a picture from my Pi camera to a flask web server. When I encode the image Base64 it doesn't seem to produce a valid image.
I can take the picture and process it through opencv. The Base64 encoded image is passed to the web page, but the string sent is not a valid image. To prove this I have saved the image and processed it with an online Base64 converter. Pasting this string into the web page shows the image.
def Take_Picture(camera):
stream = io.BytesIO() # saving the picture to an in-program stream
camera.resolution = (160,120) # set resolution
camera.capture(stream, format='jpeg', use_video_port=True) # capture into stream
mydata = np.fromstring(stream.getvalue(), dtype=np.uint8) # convert image into numpy array
img = cv2.imdecode(mydata, -1) # to opencv image
data = base64.b64encode(img).decode('utf-8')
print(data)
cv2.imwrite("test.jpg",img)
return data
HTML
<img src="data:image/jpeg;charset=utf-8;base64,{{img}}" alt="Camera View" width="640" height="480">
I get a result of
b'AAIAAAIAAAIAAAIAAAIAAAIAAAIAAAIAAAIAAAIAAAIAAAMAAAIAAAIAAA...
from data above. But I get
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUG...
from test.jpg from an online Base64 conversion. Putting this string in the web page displays the image.