Zones

class Zone(name, points, image_width, image_height, description='', **kwargs)

A region of interest in a static view.

Parameters
  • name (str) – The name for the zone.

  • points (List[Tuple[int, int]]) – The coordinates of the zone.

  • image_width (int) – The width of the image that was used to generate the zone.

  • image_height (int) – The height of the image that was used to generate the zone.

  • description (str) – A short description of the zone.

classmethod from_bounding_box(name, box, image_width, image_height, description='')

Create a Zone from a BoundingBox.

Parameters
  • name (str) – The name for the zone.

  • box (BoundingBox) – A bounding box to be used to generate a zone.

  • image_width (int) – The width of the image that was used to generate the zone.

  • image_height (int) – The height of the image that was used to generate the zone.

  • description (str) – A short description of the zone.

Return type

Zone

property image_width

The width of the image that was used to generate the zone.

Return type

int

property image_height

The height of the image that was used to generate the zone.

Return type

int

property mask

The binary mask representing the zone.

This is a numpy array of 0 and 1 with data type np.uint8. It’s dimensions match the width and height of the image it was generated with.

Return type

ndarray

property name

The zone name.

Return type

str

property points

The zone coordinates.

Return type

List[Tuple[int, int]]

property description

A short description of the zone.

Return type

str

property type

The type for the zone.

Return type

ZoneType

to_bounding_box()

Get a BoundingBox representing the zone.

A box that fits the outermost edges of the Zone will be generated.

Return type

BoundingBox

Returns

A BoundingBox representing the Zone

update_image_dimensions(image_width=None, image_height=None)

Update the dimensions of the ZoneList and all included Zone objects.

Parameters
  • image_width (Optional[int]) – The new width to update dimensions to

  • image_height (Optional[int]) – The new height to update dimensions to

check_prediction_within_zone(prediction, **kwargs)

Check whether a prediction is inside the Zone.

Parameters

prediction (ObjectDetectionPrediction) – The prediction to check for zone affiliation.

Return type

bool

Returns

True if the prediction is in the Zone, and False otherwise.

get_predictions_in_zone(results, **kwargs)

Get the predictions that are within this zone.

Parameters

results (ObjectDetectionResults) – The ObjectDetectionResults to check against the given zone.

Return type

ObjectDetectionResults

Returns

A :class:ObjectDetectionResults containing predictions that are within this zone

get_tracked_objects_in_zone(objects)

Get the tracked objects that are within this zone.

Parameters

results – The TrackingResults to check against the given zone.

Return type

TrackingResults

Returns

A TrackingResults containing predictions that are within this zone.

compute_prediction_overlap_with_zone(prediction)

Compute the fraction of prediction box which is overlapped by the Zone.

Parameters

prediction (ObjectDetectionPrediction) – The prediction to compute overlap.

Return type

float

Returns

A value [0.0, 1.0] indicating the fraction of prediction box which is overlapped by the Zone.

class ZoneType(value)

Supported Zone types.

BOX = 'BOX'
POLYGON = 'POLYGON'
class ZoneList(zones, image_width, image_height)

A managed list of Zone objects.

Work with regions of interest in your applications. Each Zone must have a unique name and they all must have matching image dimensions.

Typical usage:

my_zones = edgeiq.ZoneList.from_config_file("zone_config.json")

<get image>
results = obj_detect.detect_objects(image, confidence_level=.5)
image = edgeiq.markup_image(
        image, results.predictions, colors=obj_detect.colors)

obj_detect.publish_analytics(zone_detect_results)

image = my_zones.markup_image_with_zones(
        image,
        fill_zones=True,
        color=(255, 0, 0))
Parameters
  • zones (List[Zone]) – A list of Zone to organize as a ZoneList.

  • image_width (int) – The width of the image the zones were based on.

  • image_height (int) – The height of the image the zones were based on.

Raises

ValueError if zones are invalid (duplicate name or mismatching image dimensions)

classmethod from_config_file(filepath)

Create ZoneList from a Zone Creator configuration file.

The Zone Creator tools is available with the alwaysAI CLI.

Parameters

filepath (str) – The path to the JSON file to configure Zone objects.

Return type

ZoneList

property zones

The zones in this Zone List

Return type

List[Zone]

property zone_names
Return type

List[str]

property image_width

The width of the image the Zone objects were based on.

Return type

Optional[int]

property image_height

The height of the image the Zone objects were based on.

Return type

Optional[int]

property colors

The colors to draw the Zone boundaries in.

Return type

List[Tuple[int, int, int]]

property alpha

Transparency of the filled Zone objects, if the fill_zones option is selected.

The closer alpha is to 1.0, the more opaque the overlay will be. Similarly, the closer alpha is to 0.0, the more transparent the overlay will appear.

Return type

float

update_image_dimensions(image_width=None, image_height=None)

Update the dimensions of the ZoneList and all included Zone objects.

Parameters
  • image_width (Optional[int]) – The new width to update dimensions to

  • image_height (Optional[int]) – The new height to update dimensions to

get_zone_by_name(zone_name)

Get the Zone that corresponds to the given name.

Parameters

zone_name (str) – The name of the Zone to retrieve.

Return type

Zone

Returns

A Zone that corresponds to the input name.

Raises

ValueError if zone of zone_name doesn’t exist in list

get_zones_for_prediction(prediction, **kwargs)

Get the Zone objects that this prediction is currently within.

Parameters

prediction (ObjectDetectionPrediction) – The prediction to check for associated Zone objects.

Return type

ZoneList

Returns

A ZoneList containing Zone objects that this prediction is currently within.

get_zone_for_prediction(prediction, **kwargs)

Get the Zone object that this prediction is currently within. If there are intersecting zones the zone with the higher index is returned. Returns None if no zone is present

Parameters

prediction (ObjectDetectionPrediction) – The prediction to check for associated Zone object.

Return type

Optional[Zone]

Returns

A Zone object that this prediction is currently within.

get_predictions_in_zone(results, zone_name, **kwargs)

Get the predictions that are within the given zone.

Parameters
  • results (ObjectDetectionResults) – The ObjectDetectionResults to check against the given zone.

  • zone_name (str) – The name of zone to check results against.

Return type

ObjectDetectionResults

Returns

A :class:ObjectDetectionResults containing predictions that are within the given zone.

Raises

ValueError if the given zone name doesn’t match a zone.

get_tracked_objects_in_zone(results, zone_name)

Get the tracked objects that are within the given zone.

Parameters
  • results (TrackingResults) – The TrackingResults to check against the given zone.

  • zone_name (str) – The name of zone to check results against.

Return type

TrackingResults

Returns

A TrackingResults containing predictions that are within the given zone.

Raises

ValueError if the given zone name doesn’t match a zone.

create_sub_zone_list(zone_names)

Create a new ZoneList containing a subset of Zone objects.

Parameters

name_list – The list of names that correspond to Zone objects that should be in the new ZoneList

Returns

A new ZoneList containing the subset of Zone objects.

Raises

ValueError if any of the names do not match a zone

markup_image_with_zones(image, show_labels=True, show_boundaries=True, fill_zones=False, line_thickness=2, font_size=0.5, font_thickness=2, text_box_padding=10, text_box_corner_radius=0)

Mark all zones on the given input image.

Parameters
  • frame – The image to mark up.

  • show_labels (bool) – True if Zone names should be drawn.

  • show_boundaries (bool) – True if Zone boundaries should be drawn.

  • fill_zones (bool) – True if Zone objects should have a transparent overlay.

  • color – The color to draw the Zone boundaries in.

  • line_thickness (int) – The boundary thickness.

  • font_size (float) – The font size to use in marking up the labels.

  • font_thickness (int) – The font thickness to use in marking up the labels. size.

  • text_box_padding (int) – The padding around the text in each text box.

  • text_box_corner_radius (int) – The corner radius for the text boxes.

Return type

ndarray

Returns

The marked-up image.

Note: fill_zones is time-intensive. fill_zones = True will generate the zone mask upon first call.