Package pyvision :: Package surveillance :: Module MotionDetector :: Class MotionDetector
[hide private]
[frames] | no frames]

Class MotionDetector

source code

object --+
         |
        MotionDetector

Uses background subtraction from an image buffer to detect areas of motion in a video.

The general process is to update the image buffer and then call the MotionDetector's detect() method.

Instance Methods [hide private]
 
__init__(self, imageBuff=None, thresh=20, method='BG_SUBTRACT_AMF', minArea=400, rectFilter=None, buffSize=5, soft_thresh=False, rect_type='BOUNDING_RECTS', rect_sigma=2.0, smooth=False)
Constructor
source code
 
_initBGSubtract(self) source code
 
_computeContours(self) source code
 
_computeConvexHulls(self) source code
 
__call__(self, img, **kwargs) source code
 
detect(self, img, ConvexHulls=False)
You call this method to update detection results, given the new image in the stream.
source code
 
getKeyFrame(self)
Returns: The "key frame" of the motion detector's buffer.
source code
 
getForegroundMask(self)
Returns: a binary pv.Image representing the foreground pixels as determined by the selected background subtraction method.
source code
 
getWatershedMask(self)
Uses the watershed algorithm to refine the foreground mask.
source code
 
getForegroundPixels(self, bgcolor=None)
Returns: The full color foreground pixels on either a blank (black) background, or on a background color specified by the user.
source code
 
getRects(self)
Returns: the bounding boxes of the external contours of the foreground mask.
source code
 
getBoundingRects(self)
Returns: the bounding boxes of the external contours of the foreground mask.
source code
 
getStandardizedRects(self)
Returns: the boxes centered on the target center of mass +- n_sigma*std
source code
 
getPolygons(self, return_all=False)
Returns: the polygon contours of the foreground mask.
source code
 
getConvexHulls(self)
Returns: the convex hulls of the contours of the foreground mask.
source code
 
getAnnotatedImage(self, showRects=True, showContours=False, showConvexHulls=False, showFlow=False)
Returns: the annotation image with selected objects drawn upon it.
source code
 
annotateFrame(self, key_frame, rect_color='yellow', contour_color='#00FF00', flow_color='white')
Draws detection results on an image (key_frame) specified by the user.
source code
 
getForegroundTiles(self, bgcolor=None)
Returns: a list of "tiles", where each tile is a small pv.Image representing the clipped area of the annotationImg based on the motion detection.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, imageBuff=None, thresh=20, method='BG_SUBTRACT_AMF', minArea=400, rectFilter=None, buffSize=5, soft_thresh=False, rect_type='BOUNDING_RECTS', rect_sigma=2.0, smooth=False)
(Constructor)

source code 

Constructor

Parameters:
  • imageBuff - a pv.ImageBuffer object to be used in the background subtraction step of the motion detection. If None, then this object will create an empty 5-frame buffer, and until the buffer is full, the results of the motion detection will be nothing.
  • thresh - Used by the background subtraction to eliminate noise.
  • method - Select background subtraction method. See constants defined in BackgroundSubtraction module
  • minArea - minimum foreground contour area required for detection
  • rectFilter - a function reference that takes a list of rectangles and returns a list filtered in some way. This allows the user to arbitrarily define rules to further limit motion detection results based on the geometry of the bounding boxes.
  • buffSize - Only used if imageBuff==None. This controls the size of the internal image buffer.
  • soft_thresh - Specify if the background subtraction method should use a soft threshold, in which case the returned mask is no longer a binary image, but represents weighted values. NOTE: NOT CURRENTLY IMPLEMENTED. SOFT THRESHOLD WILL BE IGNORED, HARD THRESHOLD ONLY IN THIS VERSION.
  • smooth - applies smothing to the image before detection which can reduce false detections.
Overrides: object.__init__

Note: Until the image buffer is full, the result of the motion detection will be nothing. See documentation on the detect(img) method of this class.

detect(self, img, ConvexHulls=False)

source code 

You call this method to update detection results, given the new image in the stream. After updating detection results, use one of the get*() methods, such as getRects() to see the results in the appropriate format.

Parameters:
  • img - A pv.Image() to be added to the buffer as the most recent image, and that triggers the new motion detection. Note that, depending on the background subtraction method, this may not be the "key frame" for the detection. The Frame Differencer returns a background model based on the middle image, but Median and Approx. Median Filters return a background model based on the most recent (last) image in the buffer.
  • ConvexHulls - If true, then the detected foreground pixels are grouped into convex hulls, which can have the effect of removing internal "holes" in the detection.
Returns:
The number of detected components in the current image. To get more details, use the various getX() methods, like getForegroundMask(), after calling detect().

Note: Until the image buffer is full, this method will make no detections. In which case, the return value will be -1, indicating this status. Also, the getKeyFrame() method should be used to retrieve the key frame from the buffer, which is not always the most recent image, depending on background subtraction method.

getKeyFrame(self)

source code 
Returns:
The "key frame" of the motion detector's buffer. This is the image upon which detected motion rectangles, for example, should be overlaid. This is not always the last image in the buffer because some background subtraction methods (notably N-Frame Differencer) use the middle frame of the buffer.

getForegroundMask(self)

source code 
Returns:
a binary pv.Image representing the foreground pixels as determined by the selected background subtraction method.

Note: You must call the detect() method before getForegroundMask() to get the updated mask.

getWatershedMask(self)

source code 

Uses the watershed algorithm to refine the foreground mask. Currently, this doesn't work well on real video...maybe grabcut would be better.

getForegroundPixels(self, bgcolor=None)

source code 
Parameters:
  • bgcolor - The background color to use. Specify as an (R,G,B) tuple. Specify None for a blank/black background.
Returns:
The full color foreground pixels on either a blank (black) background, or on a background color specified by the user.

Note: You must call detect() before getForegroundPixels() to get updated information.

getRects(self)

source code 
Returns:
the bounding boxes of the external contours of the foreground mask. The boxes will either be the bounding rectangles of the contours, or a box fitted to the contours based on the center of mass and n-sigma deviations in x and y. This preference is selected when initializing the MotionDetector object.

Note: You must call detect() before getRects() to see updated results.

getBoundingRects(self)

source code 
Returns:
the bounding boxes of the external contours of the foreground mask.

Note: You must call detect() before getBoundingRects() to see updated results.

getStandardizedRects(self)

source code 
Returns:
the boxes centered on the target center of mass +- n_sigma*std

Note: You must call detect() before getStandardizedRects() to see updated results.

getPolygons(self, return_all=False)

source code 
Parameters:
  • return_all - return all contours regardless of min area.
Returns:
the polygon contours of the foreground mask. The polygons are compatible with pv.Image annotatePolygon() method.

Note: You must call detect() before getPolygons() to see updated results.

getConvexHulls(self)

source code 
Returns:
the convex hulls of the contours of the foreground mask.

Note: You must call detect() before getConvexHulls() to see updated results.

getAnnotatedImage(self, showRects=True, showContours=False, showConvexHulls=False, showFlow=False)

source code 
Returns:
the annotation image with selected objects drawn upon it. showFlow will only work if the BG subtraction method was MCFD.

Note: You must call detect() prior to getAnnotatedImage() to see updated results.

annotateFrame(self, key_frame, rect_color='yellow', contour_color='#00FF00', flow_color='white')

source code 

Draws detection results on an image (key_frame) specified by the user. Specify None as the color for any aspect you wish not drawn.

Returns:
Renders annotations onto key frame that shows detection information.
Notes:
  • You must call detect() prior to annotateFrame() to see updated results.
  • Optical flow is only shown if method was MCFD

getForegroundTiles(self, bgcolor=None)

source code 
Parameters:
  • bgcolor - The background color to use. Specify as an (R,G,B) tuple. Specify None for a blank/black background.
Returns:
a list of "tiles", where each tile is a small pv.Image representing the clipped area of the annotationImg based on the motion detection. Only the foreground pixels are copied, so the result are tiles with full-color foreground pixels on the specified background color (black by default).

Note: You must call detect() prior to getForegroundTiles() to get updated information.