Utils

nnio.Preprocessing

class nnio.Preprocessing(resize=None, dtype=None, divide_by_255=None, means=None, stds=None, scales=None, imagenet_scaling=False, to_gray=None, padding=False, channels_first=False, batch_dimension=False, bgr=False)

This class provides functionality of the image preprocessing.

Example:

preproc = nnio.Preprocessing(
    resize=(224, 224),
    dtype='float32',
    divide_by_255=True,
    means=[0.485, 0.456, 0.406],
    stds=[0.229, 0.224, 0.225],
    batch_dimension=True,
    channels_first=True,
)

# Use with numpy image
image_preprocessed = preproc(image_rgb)

# Or use to read image from disk
image_preprocessed = preproc('path/to/image.png')

# Or use to read image from the web
image_preprocessed = preproc('http://www.example.com/image.png')

Object of this type is returned every time you call get_preprocessing() method of any model from Model Zoo.

__eq__(other)

Compare two Preprocessing objects. Returns True only if all preprocessing parameters are the same.

__init__(resize=None, dtype=None, divide_by_255=None, means=None, stds=None, scales=None, imagenet_scaling=False, to_gray=None, padding=False, channels_first=False, batch_dimension=False, bgr=False)
Parameters
  • resizeNone or tuple. (width, height) - the new size of image

  • dtypestr or np.dtype. Data type. By default will use uint8.

  • divide_by_255bool. Divide input image by 255. This is applied before means, stds and scales.

  • meansfloat or iterable or None. Substract these values from each channel

  • stdsfloat` or iterable or None. Divide each channel by these values

  • scalesfloat or iterable or None. Multipy each channel by these values

  • imagenet_scaling – apply imagenet scaling. It is equivalent to divide_by_255=True, means=[0.485, 0.456, 0.406], stds=[0.229, 0.224, 0.225]. If this is specified, arguments divide_by_255, means, stds, scales must be None.

  • to_gray – if int, then convert rgb image to grayscale with specified number of channels (usually 1 or 3).

  • paddingbool. If True, images will be resized with the same aspect ratio

  • channels_firstbool. If True, image will be returned in [B]CHW format. If False, [B]HWC.

  • batch_dimensionbool. If True, add first dimension of size 1.

  • bgrbool. If True, change channels to BRG order. If False, keep the RGB order.

__str__()
Returns

full description of the Preprocessing object

forward(image, return_original=False)

Preprocess the image.

Parameters
  • image – np.ndarray of type uint8 or str RGB image If str, it will be concerned as image path.

  • return_originalbool. If True, will return tuple of (preprocessed_image, original_image)

nnio.DetectionBox

class nnio.DetectionBox(x_min, y_min, x_max, y_max, label=None, score=1.0)
__init__(x_min, y_min, x_max, y_max, label=None, score=1.0)
Parameters
  • x_minfloat in range [0, 1]. Relative x (width) coordinate of top-left corner.

  • y_minfloat in range [0, 1]. Relative y (height) coordinate of top-left corner.

  • x_maxfloat in range [0, 1]. Relative x (width) coordinate of bottom-right corner.

  • y_maxfloat in range [0, 1]. Relative y (height) coordinate of bottom-right corner.

  • labelstr or None. Class label of the detected object.

  • scorefloat. Detection score

__str__()

Return str(self).

__weakref__

list of weak references to the object (if defined)

draw(image, color=(255, 0, 0), stroke_width=2, text_color=(255, 0, 0), text_width=2)

Draws the detection box on an image

Parameters
  • image – numpy array.

  • color – RGB color of the frame.

  • stroke_width – boldness of the frame.

  • text_color – RGB color of the text.

  • text_width – boldness of the text.

Returns

Image with the box drawn on it.