[DEV] feature/bytes-payload | Add examples

This commit is contained in:
Aleksandar Ivanovski 2022-10-07 09:19:17 +02:00
parent d89d1370d8
commit 29c9732e8e
2 changed files with 31 additions and 16 deletions

View File

@ -1,4 +1,5 @@
from typing import Any import io
from typing import Any, Union
import numpy as np import numpy as np
from PIL import Image, ImageOps from PIL import Image, ImageOps
@ -13,16 +14,22 @@ class Preprocess(object):
# set internal state, this will be called only once. (i.e. not per request) # set internal state, this will be called only once. (i.e. not per request)
pass pass
def preprocess(self, body: dict, state: dict, collect_custom_statistics_fn=None) -> Any: def preprocess(self, body: Union[bytes, dict], state: dict, collect_custom_statistics_fn=None) -> Any:
# we expect to get two valid on the dict x0, and x1 # we expect to get two valid on the dict x0, and x1
url = body.get("url") if isinstance(body, bytes):
if not url: # we expect to get a stream of encoded image bytes
raise ValueError("'url' entry not provided, expected http/s link to image") try:
image = Image.open(io.BytesIO(body)).convert("RGB")
except Exception:
raise ValueError("Image could not be decoded")
local_file = StorageManager.get_local_copy(remote_url=url) if isinstance(body, dict) and "url" in body.keys():
image = Image.open(local_file) # image is given as url, and is fetched
url = body.get("url")
local_file = StorageManager.get_local_copy(remote_url=url)
image = Image.open(local_file)
image = ImageOps.grayscale(image).resize((28, 28)) image = ImageOps.grayscale(image).resize((28, 28))
return np.array([np.array(image).flatten()]) return np.array([np.array(image).flatten()])
def postprocess(self, data: Any, state: dict, collect_custom_statistics_fn=None) -> dict: def postprocess(self, data: Any, state: dict, collect_custom_statistics_fn=None) -> dict:

View File

@ -1,4 +1,5 @@
from typing import Any import io
from typing import Any, Union
import numpy as np import numpy as np
from PIL import Image, ImageOps from PIL import Image, ImageOps
@ -13,16 +14,23 @@ class Preprocess(object):
# set internal state, this will be called only once. (i.e. not per request) # set internal state, this will be called only once. (i.e. not per request)
pass pass
def preprocess(self, body: dict, state: dict, collect_custom_statistics_fn=None) -> Any: def preprocess(self, body: Union[bytes, dict], state: dict, collect_custom_statistics_fn=None) -> Any:
# we expect to get two valid on the dict x0, and x1 # we expect to get two valid on the dict x0, and x1
url = body.get("url") if isinstance(body, bytes):
if not url: # we expect to get a stream of encoded image bytes
raise ValueError("'url' entry not provided, expected http/s link to image") try:
image = Image.open(io.BytesIO(body)).convert("RGB")
except Exception:
raise ValueError("Image could not be decoded")
local_file = StorageManager.get_local_copy(remote_url=url) if isinstance(body, dict) and "url" in body.keys():
image = Image.open(local_file) # image is given as url, and is fetched
url = body.get("url")
local_file = StorageManager.get_local_copy(remote_url=url)
image = Image.open(local_file)
image = ImageOps.grayscale(image).resize((28, 28)) image = ImageOps.grayscale(image).resize((28, 28))
return np.array([np.array(image)]) return np.array([np.array(image).flatten()])
def postprocess(self, data: Any, state: dict, collect_custom_statistics_fn=None) -> dict: def postprocess(self, data: Any, state: dict, collect_custom_statistics_fn=None) -> dict:
# post process the data returned from the model inference engine # post process the data returned from the model inference engine