davis
davis

Reputation: 1336

How to convert interpreter output array back to base64 image?

I'm loading an image and processing it with a tflite I trained, and I want to extract the base64 from the output. How can I do it?

Before converting the model to tflite, the base64 result was just a value in the ouput dict. Now the output is different and I don't know how to find it.

Here is my input and output details from the interceptor for reference

[{'name': 'TFLiteInput', 'dtype': <class 'numpy.float32'>, 'shape': array([  1, 256, 256,   3]), 'index': 1, 'quantization': (0.0, 0)}]

[{'name': 'TFLiteOutput', 'dtype': <class 'numpy.float32'>, 'shape': array([  1, 256, 256,   3]), 'index': 2, 'quantization': (0.0, 0)}]

Output from console:

[[[[1. 1. 0.]
   [1. 1. 1.]
   [1. 1. 1.]
   ...
   [1. 1. 1.]
   [1. 1. 0.]
   [1. 1. 1.]]

  [[1. 1. 0.]
   [1. 1. 1.]
   [1. 1. 1.]
   ...

<class 'numpy.ndarray'>

My code:

import numpy as np
import tensorflow as tf
import base64, json, cv2

interpreter = tf.contrib.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

input_file = "309.png"
im = cv2.imread(input_file)
im = im.astype(np.float32, copy=False)
input_image = im
input_image = np.array(input_image, dtype=np.uint8)
input_image = np.expand_dims(input_image, axis=0)

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

input_data = np.array(input_image, dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])

print(output_data)
print(type(output_data))

Upvotes: 0

Views: 305

Answers (1)

miaout17
miaout17

Reputation: 4875

The problem isn't really related to TFLite Interpreter. Essentially you want serialize a Numpy array which contains an image.

Base64 is an encoding format, and it could be raw data or other format inside (e.g. PNG, JPEG...etc). As an example, below is an example snippet for writing a PNG and encode it as Base64:

import numpy as np
from PIL import Image
import io
import base64

image = Image.fromarray(output_data)
with io.BytesIO() as output:
  image.save(output, format="PNG")
  base64_img = base64.b64encode(output.getvalue())

You can easily change the code to use JPEG or raw data.

Upvotes: 1

Related Questions