Zones

load_zones_from_config(filepath)

Takes a configuration file, as generated by the Zone Creator tool, as input. Returns a dictionary of Zone names and Zone objects, as well as the width and height of Zone objects from the configuration. This dictionary can be used with the set_zones() when creating a new ZoneList instance.

Parameters

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

Returns

A dictionary with Zone name as the key and the Zone as the value; the width; the height.

create_zone_from_bounding_box(bounding_box, name='', description='', img_width=None, img_height=None)

Returns a Zone generated from the input BoundingBox.

Parameters
  • bounding_box (BoundingBox) – The box to generate a Zone for.

  • name (string) – The name for the new Zone.

  • description (string) – The description for the new Zone.

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

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

Returns

a Zone with ZoneType.BOX.

create_bounding_box_from_zone(zone)

Returns a BoundingBox generated from the input Zone. If the zone_type is ZoneType.POLYGON, a box that fits the outermost edges will be generated.

Parameters

zone (Zone) – The Zone being used to generate the box.

Returns

the generated BoundingBox.

convert_polygon_to_box_zone(zone)

Creates a new Zone with all of the attributes of the input Zone, with ZoneType.BOX, and which fits the outermost edges of the input Zone.

Parameters

zone (Zone) – the input Zone with zone_type value of ZoneType.POLYGON

Returns

a Zone with ZoneType.BOX.

class ZoneType(value)

Supported Zone types.

BOX = 'Box'
POLYGON = 'Polygon'
class Zone(name='', points=None, type=None, description='', width=None, height=None)

Class representing a region of interest in a static view.

property width

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

Type

int

property height

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

Type

int

property zone_mask

The binary mask representing the Zone within the associated width and height of the static image view it was generated with.

Type

numpy array

property name

The Zone name.

Type

string

property points

The Zone coordinates.

Type

list of [x, y] coordinates

property description

The description for the Zone.

Type

string

property zone_type

The type for the Zone.

Type

ZoneType

generate_scaled_zone_points(width, height)

Generates a new set of Zone points scaled to the input dimensions.

Parameters
  • width (int) – The new width to scale to.

  • height (int) – The new height to scale to.

Returns

a list of [x, y] coordinates representing the scaled Zone coordinates.

create_zone_mask(width=None, height=None)

Generates and returns a binary mask of the Zone in the context of the provided dimensions, where areas corresponding to the Zone are marked 1, and all other points are marked 0.

If width or height is not provided, the original value will be used.

If the width or height has not been properly set, None will be returned.

Parameters
  • width (int) – The width to use for the mask.

  • height (int) – The height to use for the mask.

Returns

numpy array.

check_object_detection_prediction_within_zone(prediction)

Checks whether a prediction is inside the Zone.

Parameters

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

Returns

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

get_results_for_zone(results)

Returns only the results that are inside the Zone.

Parameters

results (Results returned from update() or detect_objects()) – The results to check for Zone affiliation.

Returns

The results that are in the Zone.

class ZoneList(filepath=None)

Work with regions of interest in your applications. Configuration files can be generated with the Zone Creator tool, available through the alwaysAI CLI.

Typical usage:

my_zones = edgeiq.ZoneList("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)

zone_detect_results = my_zones.filter_results_by_zone(results)

obj_detect.publish_analytics(zone_detect_results)

image = my_zones.markup_image_with_zones(
        image,
        fill_zones=True,
        color=(255, 0, 0))
Parameters

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

set_zones(zones_dict)

Updates the zones property by passing in a dictionary as is returned by the load_zones_from_config() function.

Parameters

zones_dict (dictionary) – A dictionary with Zone name as the key and the Zone as the value.

property zones

A list of Zone objects.

Type

list of ZoneType

property width

The width of the image the Zone objects were created with.

Type

int

property height

The height of the image the Zone objects were created with.

Type

int

update_dimensions(width=None, height=None)

Updates the dimensions of the ZoneList and all included Zone objects. Re-scales points and updates the masks of each Zone in response to dimension updates.

Parameters
  • height (int) – The height to update to. Defaults to None.

  • height – The height to update to. Defaults to None.

add_zone(zone)

Add a new Zone to the ZoneList, which is a deep copy of the input Zone. If the name of the input Zone already exists in the ZoneList, the new Zone will replace the original.

Parameters

zone (a Zone object) – A new Zone to add to the ZoneList.

remove_zone(zone)

Removes an existing Zone from the ZoneList.

Parameters

zone (a Zone object) – The Zone to remove from the ZoneList.

get_zone(zone_name)

Returns the Zone that corresponds to the input name.

Parameters

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

Returns

A Zone that corresponds to the input name.

get_zones_for_prediction(prediction)

Returns a ZoneList containing Zone objects that this prediction is currently inside.

Parameters

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

Returns

a ZoneList containing Zone objects that this prediction is currently inside.

get_results_for_zone(results, zone_name)

Returns only the results that are inside the Zone.

Parameters
  • results (Results returned from update() or detect_objects()) – The results to check for Zone affiliation.

  • zone_name (string) – The name of the Zone to get results for.

Returns

The results that are in the selected Zone.

get_zone_names()

Returns a list of all of the names of the Zone objects.

Returns

A list of names.

create_sub_zone_list(name_list)

Creates a new ZoneList object containing Zone objects that correspond to the names in the input list.

Parameters

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

Returns

A new ZoneList containing the smaller list of Zone objects.

markup_image_with_zones(frame, zone_names=None, show_labels=True, show_boundaries=True, fill_zones=False, color=(255, 0, 0), line_thickness=2, font_size=0.5, font_thickness=2, alpha=0.5)

Marks up the input image with all of the Zone objects in the input list; defaults to all Zone objects in the ZoneList.

Parameters
  • frame (numpy array) – The image to mark up.

  • zone_names (a list of strings) – The list of names that correspond to Zone objects whose boundaries should be drawn.

  • show_lables (boolean) – True if Zone names should be drawn.

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

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

  • color (tuple of (B, G, R)) – 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.

  • alpha (float in range [0.0, 1.0]) – 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.

Returns

numpy array – The marked-up image.

filter_results_by_zone(results)

Filters results returned by update() or by detect_objects() by all of the Zones in the ZoneList.

Parameters

results (Results returned by detect_objects() or update()) – a dictionary as is returned by update()

Returns

A dictionary where all tracking results are listed under an ‘zones’ key. Each list item contains the ‘id’ of the zone, and the associated results for that zone.