Video analysis

These classes provide basic motion tracking features via foreground-background subtraction - for more details see the OpenCV docs 2 and Tutorial 6.

Motion tracker

class phenopype.tracking.motion_tracker(video_path, at_frame=1)

Bases: object

Initialize motion tracker class; extract information (length, fps, codec, etc.) from input video and pass to other tracking methods.

Parameters:
  • video_path (str) – path to video

  • at_frame (int, optional) – frame index to be used to extract the video information

video_output(video_format=None, save_suffix='out', dirpath=None, fps=None, save_colour=None, dimensions=None, resize=1)

Set properties of output video file. Most settings can be left blank, so that settings from the input video will be applied

Parameters:
  • video_format (str, optional) – format of the output video file. needs to be a fourcc-string https://www.fourcc.org/codecs.php

  • save_suffix (str, optional) – name for the output video file (defaut: “input-filename” + _out.avi)

  • dirpath (str, optional) – save directory for the output video file. will be created if not existing

  • fps (int, optional) – frames per second of the output video file

  • dimensions (tuple, optional) – dimensions (width, length) of the output video file

  • resize (int, optional) – factor by which to resize the output video dimensions

  • save_colour (bool, optional) – should the saved video frames be in colour

detection_settings(skip=5, warmup=0, start_after=0, finish_after=0, history=60, threshold=10, detect_shadows=True, mode='MOG', methods=None, masks=None, c_mask=False, c_mask_shape='rect', c_mask_size=50)

Set properties of output video file. Most settings can be left at their default value.

Parameters:
  • skip (int, optional) – how many frames to skip between each capture

  • warmup (int, optional) – warmup period in seconds for the background subtractor

  • start_after (int, optional) – start after X seconds

  • finish_after (int, optional) – finish after X seconds

  • history (int, optional) – how many frames to use for fg-bg subtraction algorithm

  • threshold (int, optional) – sensitivity-level for fg-bg subtraction algorithm (lower = more sensitive)

  • detect_shadows (bool, optional) – attempt to detect shadows - will be returned as gray pixels

  • mode ({"MOG", "KNN"} str, optional) – type of fg-bg subtraction algorithm

  • methods (method or list of methods, optional) – list with tracking_method objects

  • c_mask (bool, optional) – consecutive masking. if multiple methods are defined, the objects detected first will mask the objects detected in subsequent methods

  • c_mask_shape ({"rect", "ellipse", "contour"} str, optional) – which shape should the consecutive mask have

  • c_mask_size (int, optional) – area in pixels that is added around the mask

run_tracking(feedback=True, canvas='overlay', overlay_weight=0.5, **kwargs)

Start motion tracking procedure. Enable or disable video feedback, output and select canvas (overlay of detected objects, foreground mask, input video).

Parameters:
  • feedback (bool, optional) – show output of tracking

  • canvas ({"overlay","fgmask","input"} str, optional) – the background for the ouput video

  • overlay_weight (float (default: 0.5)) – if canvas=”overlay”, how transparent should the overlay should be

Tracking methods

class phenopype.tracking.tracking_method(label='m1', blur=5, threshold=127, remove_shadows=True, min_length=0, max_length=inf, min_area=0, max_area=inf, mode='multiple', overlay_colour='red', operations=[])

Bases: object

Constructs a tracking method that can be supplied to the motion_tracker class.

Parameters:
  • label (str, optional) – label for all objects detected by this method

  • blur (int, optional) – blurring of fgbg-mask (kernel size)

  • threshold (int, optional) – binarization of fgbg-mask after blurring (threshold value)

  • remove_shadows (bool, optional) – if motion_tracker has detect_shadows=True, they can be removed here

  • min_area (int, optional) – minimum contour area in pixels to be included

  • max_area (int, optional) – maximum contour area in pixels to be included

  • min_length (int, optional) – minimum diameter of boundary circle to be included

  • max_length (int, optional) – maximum diameter of boundary circle to be included

  • mode ({"single", "multiple"} str, optional) – track “multiple”, or “single” (biggest by diameter) objects

  • remove_shadows – remove shadows if shadow-detection is actived in MOG-algorithm

  • overlay_colour ({"red", "green", "blue", "black", "white"} str, optional) – which colour should tracked objects have

  • operations (list (default: ["length", "area"])) –

    determines the type of operations to be performed on the detected objects:
    • ”diameter” of the bounding circle of our object

    • ”area” within the contour of our object

    • ”grayscale” mean and standard deviation of grayscale pixel values inside the object contours

    • ”grayscale_background” background within boundingbox of contour

    • ”bgr” mean and standard deviation of blue, green and red pixel values inside the object contours