import requests import cv2 import json import base64 # Load an image from file im = cv2.imread("im.png") # Base64 encode it # (you can also load the raw png data from file using open instead of opencv) encoded = base64.b64encode(cv2.imencode(".png",im)[1].tostring()) # Wrap it in json (tf-serving compatible with instances and b64) instance =[{"b64":encoded.decode("utf-8")}] data = json.dumps({"instances": instance}) # Standard port for tf-serving rest interface is 8501 # Request Format: http://domain:[tf-serving-port]/v1/models/[model_name]:predict resp = requests.post("http://127.0.0.1:8501/v1/models/test_model:predict",data) # Load base 64 encoded image string from response prediction = resp.json()["predictions"][0]["b64"] # Decode base64 to binary string png_str = base64.b64decode(prediction) # Write image to disk with open("pred.png","wb") as file: file.write(png_str)