Skip to content
Douglas Blank edited this page Mar 27, 2023 · 8 revisions

kangas.datatypes.image

Image Objects

class Image(Asset)

An Image asset.

__init__

def __init__(data=None,
             name=None,
             format="png",
             scale=1.0,
             shape=None,
             colormap=None,
             minmax=None,
             channels="last",
             metadata=None,
             source=None,
             unserialize=False,
             color_order="rgb")

Create an Image asset, ready to be added to a DataGrid.

Arguments:

  • data - Required if source not given. data is one of the following:
    • a path (string) to an image
    • a file-like object containing an image
    • a numpy matrix
    • a TensorFlow tensor
    • a PyTorch tensor
    • a list or tuple of values
    • a PIL Image
  • name - String - Optional. A custom name to be displayed on the dashboard. If not provided the filename from the data argument will be used if it is a path.
  • format - Optional. String. Default: 'png'. If the data is actually something that can be turned into an image, this is the format used. Typical values include 'png' and 'jpg'.
  • scale - Optional. Float. Default: 1.0. If the data is actually something that can be turned into an image, this will be the new scale of the image.
  • shape - Optional. Tuple. Default: None. If the data is actually something that can be turned into an image, this is the new shape of the array. Dimensions are (width, height) or (width, height, colors) where colors is 3 (RGB) or 1 (grayscale).
  • colormap - Optional. String. If the data is actually something that can be turned into an image, this is the colormap used to colorize the matrix.
  • minmax - Optional. (Number, Number). If the data is actually something that can be turned into an image, this is the (min, max) used to scale the values. Otherwise, the image is autoscaled between (array.min, array.max).
  • channels - Optional. Default 'last'. If the data is actually something that can be turned into an image, this is the setting that indicates where the color information is in the format of the 2D data. 'last' indicates that the data is in (rows, columns, channels) where 'first' indicates (channels, rows, columns).
  • color_order - Optional. Default 'rgb'. The color order of the incoming image data. Only applied when data is an array and color_order is "bgr".

to_pil

def to_pil()

Return the image as a Python Image Library (PIL) image.

Example:

>>> import kangas as kg
>>> image = kg.Image("filename.jpg").to_pil()
>>> image.show()

show

def show()

Show the image.

convert_to_source

def convert_to_source(filename=None)

A PNG filename to save the loaded image.

Under development.

add_regions

def add_regions(label,
                *regions,
                score=None,
                layer_name="(uncategorized)",
                id=None,
                **metadata)

Add polygon regions to an image.

Arguments:

  • layer_name - (str) the layer for the label and regions
  • label - (str) the label for the regions
  • regions - list or tuples of at least 3 points
  • score - (optional, number) a score associated with the regions
  • id - (optional, str) an id associated with the regions

Example:

>>> image = Image()
>>> image.add_regions("car", [(x1, y1), ...], [(x2, y2), ...], layer_name="Predictions")

add_region

def add_region(label,
               region,
               score=None,
               layer_name="(uncategorized)",
               id=None,
               **metadata)

Add a polygon region to an image.

Arguments:

  • layer_name - (str) the layer for the label and region
  • label - (str) the label for the region
  • region - list or tuple of at least 3 points
  • score - (optional, number) a score associated with the region
  • id - (optional, str) an id associated with the region

Example:

>>> image = Image()
>>> image.add_region("car", [(x1, y1), ...], layer_name="Predictions")

add_markers

def add_markers(label,
                *markers,
                score=None,
                layer_name="(uncategorized)",
                id=None,
                size=18,
                shape="raindrop",
                border_width=1.5,
                **metadata)

Add markers to an image.

Arguments:

  • layer_name - (str) the layer for the label and markers
  • label - (str) the label for the markers
  • markers - list or tuples of exactly 2 ints (x, y)
  • score - (optional, number) a score associated with the markers
  • id - (optional, str) an id associated with the markers
  • size - (int) size of markers, in pixels
  • shape - (str) "raindrop" or "circle"
  • border_width - (float) width of border around shapes

Example:

>>> image = Image()
>>> point1 = (x1, y1)
>>> point2 = (x2, y2)
>>> image.add_markers("Person", point1, point2, score=0.99)

add_marker

def add_marker(label,
               marker,
               score=None,
               layer_name="(uncategorized)",
               id=None,
               size=18,
               shape="raindrop",
               border_width=1.5,
               **metadata)

Add a marker to an image.

Arguments:

  • layer_name - (str) the layer for the label and marker
  • label - (str) the label for the marker
  • marker - a list or tuple of exactly 2 ints (x, y)
  • score - (optional, number) a score associated with the marker
  • id - (optional, str) an id associated with the marker
  • size - (int) size of marker, in pixels
  • shape - (str) "raindrop" or "circle"
  • border_width - (float) width of border around shape

Example:

>>> image = Image()
>>> point = (x, y)
>>> image.add_marker("Person", point, score=0.99)

add_lines

def add_lines(label,
              *lines,
              score=None,
              layer_name="(uncategorized)",
              id=None,
              **metadata)

Add lines to an image.

Arguments:

  • layer_name - (str) the layer for the label and lines
  • label - (str) the label for the lines
  • lines - list or tuples of exactly 2 points ((x1, y1), (x2, y2))
  • score - (optional, number) a score associated with the lines
  • id - (optional, str) an id associated with the lines

Example:

>>> image = Image()
>>> line1 = [(x1, y1), (x2, y2)]
>>> line2 = [(x3, y3), (x4, y4)]
>>> image.add_lines("Person", line1, line2, score=0.99)

add_line

def add_line(label,
             line,
             score=None,
             layer_name="(uncategorized)",
             id=None,
             **metadata)

Add a line to an image.

Arguments:

  • layer_name - (str) the layer for the label and line
  • label - (str) the label for the line
  • line - list or tuple of exactly 2 points ((x1, y1), (x2, y2))
  • score - (optional, number) a score associated with the line
  • id - (optional, str) an id associated with the line

Example:

>>> image = Image()
>>> line = [(x1, y1), (x2, y2)]
>>> image.add_line("Person", line, score=0.99)

add_bounding_boxes

def add_bounding_boxes(label,
                       *boxes,
                       score=None,
                       layer_name="(uncategorized)",
                       id=None,
                       **metadata)

Add bounding boxes to an image.

Arguments:

  • layer_name - (str) the layer for the label and bounding boxes
  • label - (str) the label for the boxes
  • boxes - list or tuples of exactly 2 points (top-left, bottom-right), or 4 ints (x, y, width, height)
  • score - (optional, number) a score associated with the boxes
  • id - (optional, str) an id associated with the boxes

Example:

>>> image = Image()
>>> box1 = [(x1, y1), (x2, y2)]
>>> box2 = [x, y, width, height]
>>> image.add_bounding_boxes("Person", box1, box2, score=0.99, layer_name="Truth")
>>> image.add_bounding_boxes("Person", box1, score=0.4, layer_name="Prediction)

add_bounding_box

def add_bounding_box(label,
                     box,
                     score=None,
                     layer_name="(uncategorized)",
                     id=None,
                     **metadata)

Add a bounding box to an image.

Arguments:

  • layer_name - (str) the layer for the label and bounding box
  • label - (str) the label for the box
  • box - exactly 2 points (top-left, bottom-right), or 4 ints (x, y, width, height)
  • score - (optional, number) a score associated with the box
  • id - (optional, str) an id associated with the box

Example:

>>> image = Image()
>>> box = [(x1, y1), (x2, y2)]
>>> image.add_bounding_box("Person", box, 0.56, layer_name="Truth")
>>> box = [x, y, w, h]
>>> image.add_bounding_box("Person", box, 0.04, layer_name="Prediction")

add_mask

def add_mask(label_map,
             mask,
             scores=None,
             layer_name="(uncategorized)",
             id=None,
             column_first=False,
             **metadata)

Add a mask to an image.

Arguments:

  • layer_name - (str) the layer for the labels and mask
  • label_map - (dict) dictionary of index (int) to label (string)
  • mask - (2D array or np.array with int values, image filename, PIL.Image, or kangas.Image) an array in row-first order (mask[row][col]) or Image. If column-first order use column_first=True
  • scores - (optional, dict) a score associated with each label
  • id - (optional, str) an id associated with the mask
  • column_first - (optional, bool) normally, mask data is given in row-first order (mask[row][col]). Use this flag to indicate that you are passing in a mask in column-first order

Example:

>>> import kangas as kg
>>> import PIL.Image
>>> image = kg.Image("source.png")
>>> mask1 = PIL.Image.open("pred.png")
>>> mask2 = PIL.Image.open("truth.png")
>>> image.add_mask({23: "person"}, mask1, layer_name="Prediction")
>>> image.add_mask({23: "person"}, mask2, layer_name="Ground Truth")
>>> dg = kg.DataGrid(name="model23")
>>> dg.append([image])
>>> dg.save()

add_mask_metric

def add_mask_metric(label,
                    mask,
                    score=None,
                    colormap="plasma",
                    layer_name="(uncategorized)",
                    id=None,
                    column_first=False,
                    colorlevels=64,
                    **metadata)

Add a metric mask to an image.

Arguments:

  • layer_name - (str) the layer for the label and mask
  • label - (str) the label for the mask
  • mask - (2D array or np.array with values 0-1, image filename, PIL.Image, or kangas.Image) an array in row-first order (mask[row][col]) or Image. If column-first order use column_first=True
  • score - (optional, number) a score associated with the mask
  • colormap - (optional, str) the name of the colormap to use (see below list)
  • colorlevels - maximum number of colors; default of 64 means that there wil be 64 gradations of the colormap
  • id - (optional, str) an id associated with the mask
  • column_first - (optional, bool) normally, mask data is given in row-first order (mask[row][col]). Use this flag to indicate that you are passing in a mask in column-first order

Notes:

The possible colormap names are:

"alpha", "autumn", "bathymetry", "blackbody", "bluered", "bone", "cdom", "chlorophyll", "cool", "copper", "cubehelix", "density", "earth", "electric", "freesurface-blue", "freesurface-red", "greens", "greys", "hot", "hsv", "inferno", "jet", "magma", "oxygen", "par", "phase", "picnic", "plasma", "portland", "rainbow", "rainbow-soft", "rdbu", "salinity", "spring", "summer", "temperature", "turbidity", "velocity-blue", "velocity-green", "viridis", "warm", "winter", "yignbu", "yiorrd"

Example:

>>> import kangas as kg
>>> import PIL.Image
>>> image = kg.Image("source.png")
>>> mask = PIL.Image.open("attention.png")
>>> image.add_mask_metric("attention", mask)
>>> dg = kg.DataGrid(name="model23")
>>> dg.append([image])
>>> dg.save()

Table of Contents

Clone this wiki locally