in src/inference.py [0:0]
def input_handler(data, context):
""" Pre-process request input before it is sent to TensorFlow Serving REST API
Args:
data (obj): the request data, in format of dict or string
context (Context): an object containing request and configuration details
Returns:
(dict): a JSON-serializable dict that contains request body and headers
"""
if context.request_content_type == 'application/x-image':
# pass through json (assumes it's correctly formed)
#read image as bytes
image_as_bytes = io.BytesIO(data.read())
img = Image.open(image_as_bytes)
img = img.resize((WIDTH, HEIGHT))
# convert PIL image instance to numpy array
img_array = image.img_to_array(img, data_format = "channels_first")
# the image is now in an array of shape (3, 224, 224)
# need to expand it to (1, 3, 224, 224) as it's expecting a list
expanded_img_array = tf.expand_dims(img_array, axis=0)
#preprocessing the image array with channel first
preprocessed_img = preprocess_input(expanded_img_array, data_format = "channels_first")
#converting to numpy list
preprocessed_img_lst = preprocessed_img.numpy().tolist()
return json.dumps({"instances": preprocessed_img_lst})
else:
_return_error(415, 'Unsupported content type "{}"'.format(context.request_content_type or 'Unknown'))