Tools

Performance

class FPS

Monitor the frames per second (FPS) of video streams for performance tracking.

Typical usage:

fps = edgeiq.FPS().start()
while True:
    <main processing loop>
    fps.update()

# Get the elapsed time and FPS
fps.stop()
print("Elapsed seconds: {}".format(fps.get_elapsed_seconds()))
print("FPS: {}".format(fps.compute_fps()))

compute_fps() may also be called in the main processing loop to compute an instantaneous estimate of the FPS.

start()

Start tracking FPS.

stop()

Stop tracking FPS.

update()

Increment the total number of frames examined during the start and end intervals.

Raises

RuntimeError

get_elapsed_seconds()

Return the total number of seconds between the start and end intervals.

Returns

float – The elapsed time in seconds between start and end, or since start if stop() has not been called.

compute_fps()

Compute the (approximate) frames per second.

Returns

float – the approximate frames per second.

Image Manipulation

translate(image, x, y)

Translate an image.

Parameters
  • image (numpy array of image) – The image to manipulate.

  • x (integer) – Translate image on X axis by this amount.

  • y (integer) – Translate image on Y axis by this amount.

Returns

numpy array – The translated image.

rotate(image, angle, center=None, scale=1.0)

Rotate an image by specified angle.

Parameters
  • image (numpy array of image) – The image to manipulate.

  • angle (integer) – The angle to rotate the image by (degrees).

  • center (tuple of two integers) – The center coordinates of the rotation.

  • scale (integer) – The scale factor.

Returns

numpy array – The rotated image.

resize(image, width=None, height=None, keep_scale=True, inter=3)

Resize an image to specified height and width.

When both a width and height are given and keep_scale is True, these are treated as the maximum width and height.

Parameters
  • image (numpy array of image) – The image to manipulate.

  • height (integer) – The new height of image.

  • width (integer) – The new width of image.

  • keep_scale (boolean) – Maintain the original scale of the image.

  • inter (integer) – The interpolation method (One of OpenCV InterpolationFlags).

Returns

numpy array – The resized image.

markup_image(image, predictions, show_labels=True, show_confidences=True, colors=None, line_thickness=2, font_size=0.5, font_thickness=2)

Draw boxes, labels, and confidences on the specified image.

Parameters
  • image (numpy array of image in BGR format) – The image to draw on.

  • predictions (list of ObjectDetectionPrediction) – The list of prediction results.

  • show_labels (boolean) – Indicates whether to show the label of the prediction.

  • show_confidences (boolean) – Indicates whether to show the confidence of the prediction.

  • colors (list of tuples in the format (B, G, R)) – A custom color list to use for the bounding boxes. The index of the color will be matched with a label index.

  • line_thickness (float) – The thickness of the lines that make up the bounding box.

  • font_size (float) – The scale factor for the text.

  • font_thickness (float) – The thickness of the lines used to draw the text.

Returns

numpy array – The marked-up image.

transparent_overlay_boxes(image, predictions, alpha=0.5, colors=None, show_labels=False, show_confidences=False)

Overlay area(s) of interest within an image. This utility is designed to work with object detection to display colored bounding boxes on the original image.

Parameters
  • image (numpy array of image in BGR format) – The image to manipulate.

  • predictions (list of ObjectDetectionPrediction) – The list of prediction results.

  • alpha (integer in range [0.0, 1.0]) – Transparency of the overlay. 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.

  • colors (list of tuples in the format (B, G, R)) – A custom color list to use for the bounding boxes or object classes pixel map

  • show_labels (boolean) – Indicates whether to show the label of the prediction.

  • show_confidences (boolean) – Indicates whether to show the confidence of the prediction.

Returns

numpy array – The overlayed image.

pad_to_aspect_ratio(image, a_ratio)

Pad an image to a certain aspect ration. Padding is added to the bottom and right of the image

cutout_image(image, box)

Cut out the portion of an image outlined by a bounding box.

Parameters
  • image (numpy array of image) – The image to cut out from.

  • box (BoundingBox) – The bounding box outlining the section of the image to cut out.

Returns

numpy array – The segment of the image outlined by the bounding box. Will be independent from the original image.

blur_objects(image, predictions)

Blur objects detected in an image.

Parameters
  • image (numpy array of image) – The image to draw on.

  • predictions (list of ObjectDetectionPrediction) – A list of predictions objects to blur.

Returns

numpy array – The image with objects blurred.

blend_images(foreground_image, background_image, alpha)

Blend a foreground image with a background image, foreground image and background image must have the same dimensions and same color format (RGB/BGR).

Parameters
  • foreground_image (numpy array of image) – The image to be scaled by alpha in the blend.

  • background_image (numpy array of image) – The image to be scaled by 1 - alpha in the blend.

  • alpha (float in range [0.0, 1.0]) – The ratio of foreground to background image in the blend.

Returns

numpy array – The blended image.

overlay_image(foreground_image, background_image, foreground_mask)

Overlay a foreground image with a background image according to the foreground mask.

This function will mask both the foreground and background images, then combine them into the output image.

Parameters
  • foreground_image (numpy array of image in BGR format) – The image to be overlaid on the background.

  • background_image (numpy array of image in BGR format) – The image for the foreground to be overlaid on.

  • foreground_mask (numpy array of image in BGR format) – A mask with white indicating foreground and black indicating background. Shades in between will blend the foreground and background accordingly.

Returns

numpy array – The overlaid image.

Filesystem

list_images(base_path, contains=None)

List all images in specified path.

Finds images with the following extensions:

  • .jpg

  • .jpeg

  • .png

  • .bmp

  • .tif

  • .tiff

Parameters
  • base_path (string) – The base path of folder where images are located.

  • contains (string) – Select only filenames that contain this string.

Returns

list of strings – The valid image file paths.

list_files(base_path, valid_exts='.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff', contains=None)

List all files in specified path.

Parameters
  • base_path (string) – The base path of folder where files are located.

  • valid_exts (list of strings) – The list of valid extentions to filter for.

  • contains (string) – Select only filenames that contain this string.

Returns

list of strings – The valid file paths.

Results Filtering

filter_predictions_by_label(predictions, label_list)

Filter a prediction list by label.

Typical usage:

people_and_apples = edgeiq.filter_predictions_by_label(predictions, ['person', 'apple'])
Parameters
  • predictions (list of ObjectDetectionPrediction) – A list of predictions to filter.

  • label_list (list of strings) – The list of labels to keep in the filtered output.

Returns

list of ObjectDetectionPrediction – The filtered predictions.

filter_predictions_by_area(predictions, min_area_thresh)

Filter a prediction list by bounding box area.

Typical usage:

larger_boxes = edgeiq.filter_predictions_by_area(predictions, 450)
Parameters
  • predictions (list of ObjectDetectionPrediction) – A list of predictions to filter.

  • min_area_thresh (integer) – The minimum bounding box area to keep in the filtered output.

Returns

list of ObjectDetectionPrediction – The filtered predictions.

HW Discovery

find_usb_device(id_vendor, id_product)

Check if a USB device is connected.

Parameters
  • id_vendor (integer) – The vendor ID.

  • id_product (integer) – The product ID.

Returns

True if device found, otherwise False

find_ncs1()

Check if an NCS1 is connected.

Note that once a connection to the NCS1 is opened, the device’s VID and PID will change and this function will no longer find it.

Returns

True if NCS1 found, otherwise False

find_ncs2()

Check if an NCS2 is connected.

Note that once a connection to the NCS2 is opened, the device’s VID and PID will change and this function will no longer find it.

Returns

True if NCS2 found, otherwise False