Detection Interfaces

Here we list and briefly describe the high level algorithm interfaces which SMQTK-Detection provides. Some implementations will require additional dependencies that cannot be packaged with SMQTK-Detection.

ObjectDetector

This interface defines a method to generate object detections (smqtk_detection.interfaces.detection_element.DetectionElement) over a given smqtk_dataprovider.interfaces.data_element.DataElement.

class smqtk_detection.interfaces.object_detector.ObjectDetector(*args: Any, **kwargs: Any)[source]

Abstract interface to an object detection algorithm.

An object detection algorithm is one that can take in data and output zero or more detection elements, where each detection represents a spatial region in the data.

This high level interface only requires detection element returns (spatial bounding-boxes with associated classification elements).

detect_objects(data_element: smqtk_dataprovider.interfaces.data_element.DataElement, de_factory: smqtk_detection.detection_element_factory.DetectionElementFactory = <smqtk_detection.detection_element_factory.DetectionElementFactory object>, ce_factory: smqtk_classifier.classification_element_factory.ClassificationElementFactory = <smqtk_classifier.classification_element_factory.ClassificationElementFactory object>) Iterator[smqtk_detection.interfaces.detection_element.DetectionElement][source]

Detect objects in the given data.

UUIDs of detections are based on the hash produced from the combination of:

  • Detection bounding-box bounding coordinates

  • Classification label set predicted for a bounding box.

Parameters
  • data_element (smqtk.representation.DataElement) – Source data from which to detect objects within.

  • de_factory (smqtk.representation.DetectionElementFactory) – Factory for generating DetectionElement instances. The default factory yields MemoryClassificationElement instances.

  • ce_factory (smqtk.representation.ClassificationElementFactory) – Factory for generating ClassificationElement instances for detections. The default factory yields MemoryClassificationElement instances.

Raises

ValueError – Given data element content was not of a valid content type that this class reports as valid for object detection.

Returns

Iterator over result DetectionElement instances as generated by the given DetectionElementFactory, containing classification elements as generated by the given ClassificationElementFactory.

Return type

collections.abc.Iterable[smqtk.representation.DetectionElement]

Detection Element

Data structure used by Detector

class smqtk_detection.interfaces.detection_element.DetectionElement(*args: Any, **kwargs: Any)[source]

Representation of a spatial detection.

classmethod from_config(config_dict: Dict[Any, Any], uuid: collections.abc.Hashable, merge_default: bool = True) smqtk_detection.interfaces.detection_element.DetectionElement[source]

Override of smqtk.utils.configuration.Configurable.from_config() with the added runtime argument uuid. See parent method documentation for details.

Parameters
  • config_dict (dict) – JSON compliant dictionary encapsulating a configuration.

  • uuid (collections.abc.Hashable) – UUID to assign to the produced DetectionElement.

  • merge_default (bool) – Merge the given configuration on top of the default provided by get_default_config.

Returns

Constructed instance from the provided config.

Return type

DetectionElement

abstract get_bbox() Optional[smqtk_detection.utils.bbox.AxisAlignedBoundingBox][source]
Returns

The spatial bounding box of this detection.

Return type

smqtk.representation.AxisAlignedBoundingBox

Raises

NoDetectionError – No detection AxisAlignedBoundingBox set yet.

abstract get_classification() Optional[smqtk_classifier.interfaces.classification_element.ClassificationElement][source]
Returns

The classification element of this detection.

Return type

smqtk.representation.ClassificationElement

Raises

NoDetectionError – No detection ClassificationElement set yet or the element is empty.

classmethod get_default_config() Dict[str, Any][source]

Generate and return a default configuration dictionary for this class. This will be primarily used for generating what the configuration dictionary would look like for this class without instantiating it.

By default, we observe what this class’s constructor takes as arguments, turning those argument names into configuration dictionary keys. If any of those arguments have defaults, we will add those values into the configuration dictionary appropriately. The dictionary returned should only contain JSON compliant value types.

It is not be guaranteed that the configuration dictionary returned from this method is valid for construction of an instance of this class.

Returns

Default configuration dictionary for the class.

Return type

dict

>>> # noinspection PyUnresolvedReferences
>>> class SimpleConfig(Configurable):
...     def __init__(self, a=1, b='foo'):
...         self.a = a
...         self.b = b
...     def get_config(self):
...         return {'a': self.a, 'b': self.b}
>>> self = SimpleConfig()
>>> config = self.get_default_config()
>>> assert config == {'a': 1, 'b': 'foo'}
abstract get_detection() Tuple[smqtk_detection.utils.bbox.AxisAlignedBoundingBox, smqtk_classifier.interfaces.classification_element.ClassificationElement][source]
Returns

The paired spatial bounding box and classification element of this detection.

Return type

(smqtk.representation.AxisAlignedBoundingBox, smqtk.representation.ClassificationElement)

Raises

NoDetectionError – No detection AxisAlignedBoundingBox and ClassificationElement set yet.

abstract has_detection() bool[source]
Returns

Whether or not this container currently contains a valid detection bounding box and classification element (must be non-zero).

Return type

bool

abstract set_detection(bbox: smqtk_detection.utils.bbox.AxisAlignedBoundingBox, classification_element: smqtk_classifier.interfaces.classification_element.ClassificationElement) smqtk_detection.interfaces.detection_element.DetectionElement[source]

Set a bounding box and classification element to this detection element.

Parameters
  • bbox (smqtk.representation.AxisAlignedBoundingBox) – Spatial bounding box instance.

  • classification_element (smqtk.representation.ClassificationElement) – The classification of this detection.

Raises

ValueError – No, or invalid, AxisAlignedBoundingBox or ClassificationElement was provided.

Returns

Self

Return type

DetectionElement

ImageMatrixObjectDetector

class smqtk_detection.interfaces.object_detector.ImageMatrixObjectDetector(*args: Any, **kwargs: Any)[source]

Class of object detectors that operate over the pixel matrix of an image.

This sub abstract class standardizes the use of an smqtk.algorithms.ImageReader algorithm to read an image file’s pixels as well as determine which image formats are valid input elements. There is a special exception of MatrixDataElement types as they directly provide a matrix.

We define an alternate abstract method for implementing classes to define: _detect_objects_matrix. This method is given a numpy ndarray instance for the implementing class to utilize. The return requirements are the same as the _detect_objects method.

classmethod from_config(config_dict: dict, merge_default: bool = True) ImMatObDet[source]

Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments.

This method should not be called via super unless an instance of the class is desired.

Parameters
  • config_dict (dict) – JSON compliant dictionary encapsulating a configuration.

  • merge_default (bool) – Merge the given configuration on top of the default provided by get_default_config.

Returns

Constructed instance from the provided config.

Return type

ImageMatrixObjectDetector

abstract get_config() dict[source]

Return a JSON-compliant dictionary that could be passed to this class’s from_config method to produce an instance with identical configuration.

In the most cases, this involves naming the keys of the dictionary based on the initialization argument names as if it were to be passed to the constructor via dictionary expansion. In some cases, where it doesn’t make sense to store some object constructor parameters are expected to be supplied at as configuration values (i.e. must be supplied at runtime), this method’s returned dictionary may leave those parameters out. In such cases, the object’s from_config class-method would also take additional positional arguments to fill in for the parameters that this returned configuration lacks.

Returns

JSON type compliant configuration dictionary.

Return type

dict

classmethod get_default_config() dict[source]

Generate and return a default configuration dictionary for this class. This will be primarily used for generating what the configuration dictionary would look like for this class without instantiating it.

By default, we observe what this class’s constructor takes as arguments, turning those argument names into configuration dictionary keys. If any of those arguments have defaults, we will add those values into the configuration dictionary appropriately. The dictionary returned should only contain JSON compliant value types.

It is not be guaranteed that the configuration dictionary returned from this method is valid for construction of an instance of this class.

Returns

Default configuration dictionary for the class.

Return type

dict

is_valid_element(data_element: smqtk_dataprovider.interfaces.data_element.DataElement) bool[source]

Check if the given DataElement instance reports a content type that matches one of the MIME types reported by valid_content_types.

This override uses our stored ImageReader algorithm instance to define what DataElement instances are valid.

Parameters

data_element (smqtk.representation.DataElement) – Data element instance to check.

Returns

True if the given element has a valid content type as reported by valid_content_types, and False if not.

Return type

bool

valid_content_types() Set[str][source]
Returns

A set valid MIME types that are “valid” within the implementing class’ context.

Return type

set[str]