pycoral.adapters

pycoral.adapters.common

Functions to work with any model.

pycoral.adapters.common.input_details(interpreter, key)[source]

Gets a model’s input details by specified key.

Parameters
  • interpreter – The tf.lite.Interpreter holding the model.

  • key (int) – The index position of an input tensor.

Returns

The input details.

pycoral.adapters.common.input_size(interpreter)[source]

Gets a model’s input size as (width, height) tuple.

Parameters

interpreter – The tf.lite.Interpreter holding the model.

Returns

The input tensor size as (width, height) tuple.

pycoral.adapters.common.input_tensor(interpreter)[source]

Gets a model’s input tensor view as numpy array of shape (height, width, 3).

Parameters

interpreter – The tf.lite.Interpreter holding the model.

Returns

The input tensor view as numpy.array (height, width, 3).

pycoral.adapters.common.output_tensor(interpreter, i)[source]

Gets a model’s ith output tensor.

Parameters
  • interpreter – The tf.lite.Interpreter holding the model.

  • i (int) – The index position of an output tensor.

Returns

The output tensor at the specified position.

pycoral.adapters.common.set_input(interpreter, data)[source]

Copies data to a model’s input tensor.

Parameters
  • interpreter – The tf.lite.Interpreter to update.

  • data – The input tensor.

pycoral.adapters.common.set_resized_input(interpreter, size, resize)[source]

Copies a resized and properly zero-padded image to a model’s input tensor.

Parameters
  • interpreter – The tf.lite.Interpreter to update.

  • size (tuple) – The original image size as (width, height) tuple.

  • resize – A function that takes a (width, height) tuple, and returns an image resized to those dimensions.

Returns

The resized tensor with zero-padding as tuple (resized_tensor, resize_ratio).

pycoral.adapters.classify

Functions to work with a classification model.

pycoral.adapters.classify.get_classes(interpreter, top_k=inf, score_threshold=- inf)[source]

Gets results from a classification model as a list of ordered classes.

Parameters
  • interpreter – The tf.lite.Interpreter to query for results.

  • top_k (int) – The number of top results to return.

  • score_threshold (float) – The score threshold for results. All returned results have a score greater-than-or-equal-to this value.

Returns

A list of Class objects representing the classification results, ordered by scores.

pycoral.adapters.classify.get_classes_from_scores(scores, top_k=inf, score_threshold=- inf)[source]

Gets results from a classification model as a list of ordered classes, based on given scores.

Parameters
  • scores – The output from a classification model. Must be flattened and dequantized.

  • top_k (int) – The number of top results to return.

  • score_threshold (float) – The score threshold for results. All returned results have a score greater-than-or-equal-to this value.

Returns

A list of Class objects representing the classification results, ordered by scores.

pycoral.adapters.classify.get_scores(interpreter)[source]

Gets the output (all scores) from a classification model, dequantizing it if necessary.

Parameters

interpreter – The tf.lite.Interpreter to query for output.

Returns

The output tensor (flattened and dequantized) as numpy.array.

pycoral.adapters.classify.num_classes(interpreter)[source]

Gets the number of classes output by a classification model.

Parameters

interpreter – The tf.lite.Interpreter holding the model.

Returns

The total number of classes output by the model.

class pycoral.adapters.classify.Class(id, score)[source]

Represents a single classification, with the following fields:

id

The class id.

score

The prediction score.

pycoral.adapters.detect

Functions to work with a detection model.

pycoral.adapters.detect.get_objects(interpreter, score_threshold=- inf, image_scale=1.0, 1.0)[source]

Gets results from a detection model as a list of detected objects.

Parameters
  • interpreter – The tf.lite.Interpreter to query for results.

  • score_threshold (float) – The score threshold for results. All returned results have a score greater-than-or-equal-to this value.

  • image_scale (float, float) – Scaling factor to apply to the bounding boxes as (x-scale-factor, y-scale-factor), where each factor is from 0 to 1.0.

Returns

A list of Object objects, which each contains the detected object’s id, score, and bounding box as BBox.

class pycoral.adapters.detect.Object(id, score, bbox)[source]

Represents a detected object.

id

The object’s class id.

score

The object’s prediction score.

bbox

A BBox object defining the object’s location.

class pycoral.adapters.detect.BBox[source]

The bounding box for a detected object.

xmin

X-axis start point

ymin

Y-axis start point

xmax

X-axis end point

ymax

Y-axis end point

property width

The bounding box width.

property height

The bounding box height.

property area

The bound box area.

property valid

Indicates whether bounding box is valid or not (boolean).

A valid bounding box has xmin <= xmax and ymin <= ymax (equivalent to width >= 0 and height >= 0).

scale(sx, sy)[source]

Scales the bounding box.

Parameters
  • sx (float) – Scale factor for the x-axis.

  • sy (float) – Scale factor for the y-axis.

Returns

A BBox object with the rescaled dimensions.

translate(dx, dy)[source]

Translates the bounding box position.

Parameters
  • dx (int) – Number of pixels to move the box on the x-axis.

  • dy (int) – Number of pixels to move the box on the y-axis.

Returns

A BBox object at the new position.

map(f)[source]

Maps all box coordinates to a new position using a given function.

Parameters

f – A function that takes a single coordinate and returns a new one.

Returns

A BBox with the new coordinates.

static intersect(a, b)[source]

Gets a box representing the intersection between two boxes.

Parameters
Returns

A BBox representing the area where the two boxes intersect (may be an invalid box, check with valid()).

static union(a, b)[source]

Gets a box representing the union of two boxes.

Parameters
Returns

A BBox representing the unified area of the two boxes (always a valid box).

static iou(a, b)[source]

Gets the intersection-over-union value for two boxes.

Parameters
Returns

The intersection-over-union value – 1.0 meaning the two boxes are perfectly aligned, 0 if not overlapping at all (invalid intersection).

API version 2.0