cv2ext.detection package¶
Module contents¶
Submodule for performing simple types of detection.
Classes¶
AbstractFramePackerAbstract class for frame packers.
AnnealingFramePackerA frame packer that uses simulated annealing.
BlobDetectorA simple blob detector class.
RandomFramePackerA frame packer that randomly samples.
Functions¶
detect_blobs()Detect blobs in an image.
draw_detections()Draw detections on an image.
- class cv2ext.detection.AbstractFramePacker[source]¶
Bases:
ABCPack regions of a frame together based on detection activity.
Detections are represented as a list of bounding boxes with scores and class id labels optional.
- abstract pack(image: np.ndarray, exclude: tuple[int, int, int, int] | list[tuple[int, int, int, int]]) tuple[np.ndarray, np.ndarray][source]¶
Pack regions of a frame together.
- abstract unpack(detections: list[tuple[int, int, int, int]] | list[tuple[tuple[int, int, int, int], float, int]], transform: np.ndarray) list[tuple[int, int, int, int]] | list[tuple[tuple[int, int, int, int], float, int]][source]¶
Unpack regions of a frame.
- class cv2ext.detection.AbstractGridFramePacker(image_shape: tuple[int, int], gridsize: int = 128, detection_buffer: int = 30, method: str = 'shelf')[source]¶
Bases:
AbstractFramePackerPack regions of a frame together based on a grid.
- reset(image_shape: tuple[int, int] | None = None, gridsize: int | None = None) None[source]¶
Reset the packer.
- pack(image: ndarray, exclude: tuple[int, int, int, int] | list[tuple[int, int, int, int]] | None = None, method: str | None = None) tuple[ndarray, ndarray][source]¶
Pack regions of a frame together.
- Parameters:
image (np.ndarray) – The image to pack.
exclude (tuple[int, int, int, int] | list[tuple[int, int, int, int]], optional) – Regions of the image to exclude from the packing. By default None.
method (str, optional) – The method to pack the bounding boxes with. By default, None Options are: [‘simple’, ‘shelf’]
- Returns:
The packed image and the transform information.
- Return type:
tuple[np.ndarray, np.ndarray]
- Raises:
ValueError – If the image shape does not match the packer shape.
- unpack(detections: list[tuple[int, int, int, int]] | list[tuple[tuple[int, int, int, int], float, int]], transform: np.ndarray) list[tuple[int, int, int, int]] | list[tuple[tuple[int, int, int, int], float, int]][source]¶
Unpack regions of a frame.
- class cv2ext.detection.AnnealingFramePacker(image_shape: tuple[int, int], gridsize: int = 128, alpha: float = 0.01, min_prob: float = 0.1, detection_buffer: int = 30, method: str = 'shelf')[source]¶
Bases:
AbstractGridFramePackerPack regions of a frame together based on detection activity.
Detections are represented as a list of bounding boxes with scores and class id labels optional.
- class cv2ext.detection.BlobDetector(kernel_size: int = 9, sigma: float = 2.0, max_area: float = 0.9, min_area: float = 0.05, *, use_blur: bool | None = None, is_rgb: bool | None = None, filter_size: bool | None = None)[source]¶
Bases:
objectClass for detecting blobs in images.
- class cv2ext.detection.RandomFramePacker(image_shape: tuple[int, int], gridsize: int = 128, threshold: float = 0.1, detection_buffer: int = 30, method: str = 'shelf')[source]¶
Bases:
AbstractGridFramePackerPack regions of a frame together randomly.
- cv2ext.detection.detect_blobs(image: np.ndarray, kernel_size: int = 9, sigma: float = 2.0, max_area: float = 0.9, min_area: float = 0.05, *, use_blur: bool | None = None, is_rgb: bool | None = None, filter_size: bool | None = None) list[tuple[int, int, int, int]][source]¶
Detect blobs in an image.
Blob detection is performed using OpenCVs, findContours function. Each contour is identified and then a bounding box is created for each one.
- Parameters:
image (np.ndarray) – The image to detect blobs in.
kernel_size (int) – The size of the kernel for the Gaussian blur.
sigma (float) – The standard deviation for the Gaussian blur.
max_area (float) – The maximum area a bounding box can be relative to the image. Default is 0.9, so bounding boxes larger than 90% of the image will be removed from final result if filtering enabled.
min_area (float) – The minimum area a bounding box can be relative to the image. Default is 0.01, so bounding boxes smaller than 5% of the image will be removed from final result if filtering enabled.
use_blur (bool, optional) – Whether or not to use a Gaussian blur on the image. By default, blur will be used.
is_rgb (bool, optional) – Whether or not the image is in RGB format. Pass True if input image is RGB instead of BGR (OpenCV default).
filter_size (bool, optional) – Whether or not to filter the final bounding boxes by the area of the bounding box.
- Returns:
A list of bounding boxes for each blob.
- Return type:
- cv2ext.detection.draw_detections(image: np.ndarray, dets: Sequence[tuple[tuple[int, int, int, int], float, int]], class_map: dict[int, str] | None = None, color: Color | tuple[int, int, int] = Color.RED, thickness: int = 2, opacity: float | None = None, *, copy: bool | None = None) np.ndarray[source]¶
Draw bounding boxes on an image.
- Parameters:
image (np.ndarray) – The image to draw the bounding boxes on.
dets (Sequence[tuple[tuple[int, int, int, int], float, int]]) – The detections to draw. The detections should be in form: (bbox, confidence, classid)
class_map (dict[int, str], optional) – The class map to use for converting class indices to labels.
color (Color, tuple[int, int, int], optional) – The color to draw the bounding boxes. In BGR format and the default is Color.RED.
thickness (int, optional) – The thickness of the bounding box lines. Default is 2.
opacity (float, optional) – The opacity to draw the bounding boxes with. By default None or 100%. Can have a high performance impact.
copy (bool, optional) – Whether or not to copy the image before drawing. Default is False.
- Returns:
The image with the bounding boxes drawn.
- Return type:
np.ndarray