Skip to content

Support for Common Data Types

When using Chassis, you must define a process function that serves as your model inference function. The sole parameter to this function, let's call it input_bytes, represents the input data for your model. This parameter will always be of type bytes, which means the beginning of your process function must account for this and be able to convert it to the expected data type for your model. This guide includes examples of how to decode raw bytes for common data types.

Assume input_bytes is the parameter to your process function for each.

Text

text = input_bytes.decode()
def process(input_bytes):
    text = input_bytes.decode()
    '''
    Perform processing and inference on text
    '''
    return output

Imagery

OpenCV

import cv2
import numpy as np
img = cv2.imdecode(np.frombuffer(input_bytes, np.uint8), -1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def process(input_bytes):
    img = cv2.imdecode(np.frombuffer(input_bytes, np.uint8), -1)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    '''
    Perform processing and inference on img
    '''
    return output

Pillow

import io
from PIL import Image
img = Image.open(io.BytesIO(input_bytes)).convert("RGB")
def process(input_bytes):
    img = Image.open(io.BytesIO(input_bytes)).convert("RGB")
    '''
    Perform processing and inference on img
    '''
    return output

See also:

Tabular

from io import StringIO
import pandas as pd
input_table = pd.read_csv(StringIO(str(input_bytes, "utf-8")))
def process(input_bytes):
    input_table = pd.read_csv(StringIO(str(input_bytes, "utf-8")))
    '''
    Perform processing and inference on input_table
    '''
    return output