I have a flask server that I want to listen to JSON requests for Z input (working), generate an image using that vector (working), and then send the image using localhost to another app. It should be base64 encoded, as I understand it. How do I transform the tensor into this format?
@app.route("/json", methods=['GET', 'POST', 'PUT'])
def getjsondata():
if request.method=='POST':
print("received POST")
data = request.get_json()
#print(format(data['z']))
jzf = [float(i) for i in data['z']]
jzft = torch.FloatTensor(jzf)
jzftr = jzft.reshape([1, 512])
z = jzftr.cuda()
c = None # class labels (not used in this example)
trunc = 1
img = G(z, c, trunc)
img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
# this is how I was saving image to disk, but I would rather not save before sending
# PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save('newtest.png')
# this isn't working
pil_image = PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB')
encoded_string = base64.b64encode(pil_image)
return encoded_string