Skip to content

Commit

Permalink
reformatted docstring in epytext
Browse files Browse the repository at this point in the history
  • Loading branch information
spbui00 committed Oct 2, 2024
1 parent 2a312f8 commit a27d09d
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 90 deletions.
69 changes: 44 additions & 25 deletions depthai_nodes/ml/helpers/tiles_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ class TilesPatcher(dai.node.HostNode):
detections from tiles back into the global frame, and sends out the combined
detections for further processing.
Attributes:
conf_thresh (float): Confidence threshold for filtering detections.
iou_thresh (float): IOU threshold for non-max suppression.
tile_manager (Tiling): Manager responsible for handling tiling configurations.
tile_buffer (list): Buffer to store tile detections temporarily.
current_timestamp (float): Timestamp for the current frame being processed.
expected_tiles_count (int): Number of tiles expected per frame.
@ivar conf_thresh: Confidence threshold for filtering detections.
@type conf_thresh: float
@ivar iou_thresh: IOU threshold for non-max suppression.
@type iou_thresh: float
@ivar tile_manager: Manager responsible for handling tiling configurations.
@type tile_manager: Tiling
@ivar tile_buffer: Buffer to store tile detections temporarily.
@type tile_buffer: list
@ivar current_timestamp: Timestamp for the current frame being processed.
@type current_timestamp: float
@ivar expected_tiles_count: Number of tiles expected per frame.
@type expected_tiles_count: int
"""

def __init__(self) -> None:
Expand All @@ -37,14 +42,19 @@ def build(
"""Configures the TilesPatcher node with the tile manager and links the neural
network's output.
Args:
tile_manager (Tiling): The tiling manager responsible for tile positions and dimensions.
nn (dai.Node.Output): The output of the neural network node from which detections are received.
conf_thresh (float, optional): Confidence threshold for filtering detections (default: 0.3).
iou_thresh (float, optional): IOU threshold for non-max suppression (default: 0.4).
Returns:
TilesPatcher: Returns self for method chaining.
@param tile_manager: The tiling manager responsible for tile positions and
dimensions.
@type tile_manager: Tiling
@param nn: The output of the neural network node from which detections are
received.
@type nn: dai.Node.Output
@param conf_thresh: Confidence threshold for filtering detections (default:
0.3).
@type conf_thresh: float
@param iou_thresh: IOU threshold for non-max suppression (default: 0.4).
@type iou_thresh: float
@return: Returns self for method chaining.
@rtype: TilesPatcher
"""
self.conf_thresh = conf_thresh
self.iou_thresh = iou_thresh
Expand All @@ -65,8 +75,8 @@ def process(self, nn_output: dai.ImgDetections) -> None:
patches back into the global frame and buffering them until all tiles for the
current frame are processed.
Args:
nn_output (dai.ImgDetections): The detections from the neural network's output.
@param nn_output: The detections from the neural network's output.
@type nn_output: dai.ImgDetections
"""
timestamp = nn_output.getTimestamp()
device_timestamp = nn_output.getTimestampDevice()
Expand Down Expand Up @@ -96,12 +106,12 @@ def _map_bboxes_to_global_frame(
"""Maps bounding boxes from their local tile coordinates back to the global
frame of the full image.
Args:
bboxes (list[dai.ImgDetection]): The bounding boxes to be mapped.
tile_index (int): The index of the tile being proccessed.
Returns:
list[dai.ImgDetection]: Mapped bounding boxes in the global image frame.
@param bboxes: The bounding boxes to be mapped.
@type bboxes: list[dai.ImgDetection]
@param tile_index: The index of the tile being processed.
@type tile_index: int
@return: Mapped bounding boxes in the global image frame.
@rtype: list[dai.ImgDetection]
"""
tile_info = self._get_tile_info(tile_index)
if (
Expand Down Expand Up @@ -178,7 +188,12 @@ def _map_bboxes_to_global_frame(

def _get_tile_info(self, tile_index: int):
"""Retrieves the tile's coordinates and scaled dimensions based on the tile
index."""
index.
@param tile_index: The index of the tile.
@type tile_index: int
@return: Tile information for the given index.
"""
if self.tile_manager is None or self.tile_manager.tile_positions is None:
raise ValueError("Tile manager or tile positions not initialized.")
if tile_index >= len(self.tile_manager.tile_positions):
Expand All @@ -187,7 +202,11 @@ def _get_tile_info(self, tile_index: int):

def _send_output(self, timestamp, device_timestamp):
"""Send the final combined bounding boxes as output when all tiles for a frame
are processed."""
are processed.
@param timestamp: The timestamp of the frame.
@param device_timestamp: The timestamp of the frame on the device.
"""
combined_bboxes: list[dai.ImgDetection] = []
for bboxes in self.tile_buffer:
combined_bboxes.extend(bboxes)
Expand Down
108 changes: 63 additions & 45 deletions depthai_nodes/ml/helpers/tiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ class Tiling(dai.node.HostNode):
overlapping tiles based on configuration parameters, and creates ImgFrames for each
tile to be sent to a neural network node.
Attributes:
overlap (float): Overlap between adjacent tiles, valid in [0,1).
grid_size (tuple): Grid size (number of tiles horizontally and vertically).
grid_matrix (list): The matrix representing the grid of tiles.
nn_shape (tuple): Shape of the neural network input.
x (list): Vector representing the tile's dimensions.
tile_positions (list): Coordinates and scaled sizes of the tiles.
img_shape (tuple): Shape of the original input image.
global_detection (bool): Whether to use global detection.
@ivar overlap: Overlap between adjacent tiles, valid in [0,1).
@type overlap: float
@ivar grid_size: Grid size (number of tiles horizontally and vertically).
@type grid_size: tuple
@ivar grid_matrix: The matrix representing the grid of tiles.
@type grid_matrix: list
@ivar nn_shape: Shape of the neural network input.
@type nn_shape: tuple
@ivar x: Vector representing the tile's dimensions.
@type x: list
@ivar tile_positions: Coordinates and scaled sizes of the tiles.
@type tile_positions: list
@ivar img_shape: Shape of the original input image.
@type img_shape: tuple
@ivar global_detection: Whether to use global detection.
@type global_detection: bool
"""

def __init__(self) -> None:
Expand Down Expand Up @@ -48,17 +55,22 @@ def build(
"""Configures the Tiling node with grid size, overlap, image and neural network
shapes, and other necessary parameters.
Args:
overlap (float): Overlap between adjacent tiles, valid in [0,1).
img_output (dai.Node.Output): The node from which the frames are sent.
grid_size (tuple): Number of tiles horizontally and vertically.
img_shape (tuple): Shape of the original image.
nn_shape (tuple): Shape of the neural network input.
global_detection (bool, optional): Whether to perform global detection.
grid_matrix (list, optional): Predefined matrix for tiling.
Returns:
Tiling: Returns self for method chaining.
@param overlap: Overlap between adjacent tiles, valid in [0,1).
@type overlap: float
@param img_output: The node from which the frames are sent.
@type img_output: dai.Node.Output
@param grid_size: Number of tiles horizontally and vertically.
@type grid_size: tuple
@param img_shape: Shape of the original image.
@type img_shape: tuple
@param nn_shape: Shape of the neural network input.
@type nn_shape: tuple
@param global_detection: Whether to perform global detection. Defaults to False.
@type global_detection: bool
@param grid_matrix: Predefined matrix for tiling. Defaults to None.
@type grid_matrix: list or None
@return: Returns self for method chaining.
@rtype: Tiling
"""
self.sendProcessingToPipeline(True)
self.link_args(img_output)
Expand All @@ -77,8 +89,8 @@ def process(self, img_frame) -> None:
"""Processes the input frame by cropping and tiling it, and sending each tile to
the neural network input.
Args:
img_frame (dai.ImgFrame): The frame to be sent to a neural network.
@param img_frame: The frame to be sent to a neural network.
@type img_frame: dai.ImgFrame
"""
frame: np.ndarray = img_frame.getCvFrame()

Expand All @@ -102,13 +114,14 @@ def _crop_to_nn_shape(
"""Crops and resizes the input tile to fit the neural network's input shape.
Adds padding if necessary to preserve aspect ratio.
Args:
frame (np.ndarray): The tile to be cropped and resized.
scaled_width (int): The width of the scaled tile.
scaled_height (int): The height of the scaled tile.
Returns:
np.ndarray: The padded and resized tile.
@param frame: The tile to be cropped and resized.
@type frame: np.ndarray
@param scaled_width: The width of the scaled tile.
@type scaled_width: int
@param scaled_height: The height of the scaled tile.
@type scaled_height: int
@return: The padded and resized tile.
@rtype: np.ndarray
"""
if self.nn_shape is None:
raise ValueError("NN shape is not initialized.")
Expand All @@ -132,15 +145,19 @@ def _create_img_frame(
neural network. This ImgFrame contains the tiled image in a planar format ready
for inference.
Args:
tile (np.ndarray): The cropped tile.
frame: The original ImgFrame object, providing metadata such as timestamps.
tile_index (int): Index of the current tile.
scaled_width (int): Width of the scaled tile.
scaled_height (int): Height of the scaled tile.
Returns:
dai.ImgFrame: The ImgFrame object with the tile data, ready for neural network inference.
@param tile: The cropped tile.
@type tile: np.ndarray
@param frame: The original ImgFrame object, providing metadata such as
timestamps.
@param tile_index: Index of the current tile.
@type tile_index: int
@param scaled_width: Width of the scaled tile.
@type scaled_width: int
@param scaled_height: Height of the scaled tile.
@type scaled_height: int
@return: The ImgFrame object with the tile data, ready for neural network
inference.
@rtype: dai.ImgFrame
"""
if self.nn_shape is None:
raise ValueError("NN shape is not initialized.")
Expand All @@ -164,13 +181,14 @@ def _calculate_tiles(self, grid_size, img_shape, overlap):
"""Calculates the dimensions (x, y) of each tile given the grid size, image
shape, and overlap.
Args:
grid_size (tuple): The number of tiles in width and height.
img_shape (tuple): The dimensions (width, height) of the input image.
overlap (float): The overlap between adjacent tiles, valid in the range [0,1).
Returns:
np.ndarray: The dimensions (width, height) of each tile.
@param grid_size: The number of tiles in width and height.
@type grid_size: tuple
@param img_shape: The dimensions (width, height) of the input image.
@type img_shape: tuple
@param overlap: The overlap between adjacent tiles, valid in the range [0,1).
@type overlap: float
@return: The dimensions (width, height) of each tile.
@rtype: np.ndarray
"""
if not 0 <= overlap < 1:
raise ValueError("Overlap must be in the range [0,1).")
Expand Down
32 changes: 18 additions & 14 deletions depthai_nodes/ml/helpers/utils/nms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
def nms_detections(detections: list[dai.ImgDetection], conf_thresh=0.3, iou_thresh=0.4):
"""Applies Non-Maximum Suppression (NMS) on a list of dai.ImgDetection objects.
Parameters:
- detections: List of dai.ImgDetection objects.
- conf_thresh: Confidence threshold for filtering boxes.
- iou_thresh: IoU threshold for NMS.
Returns:
- A list of dai.ImgDetection objects after NMS.
@param detections: List of dai.ImgDetection objects. @type
detections: list[dai.ImgDetection] @param conf_thresh: Confidence
threshold for filtering boxes. @type conf_thresh: float @param
iou_thresh: IoU threshold for Non-Maximum Suppression (NMS). @type
iou_thresh: float
@return: A list of dai.ImgDetection objects after applying NMS.
@rtype: list[dai.ImgDetection]
"""
if len(detections) == 0:
return []
Expand Down Expand Up @@ -54,13 +55,16 @@ def nms_detections(detections: list[dai.ImgDetection], conf_thresh=0.3, iou_thre
def nms(boxes, scores, iou_thresh):
"""Perform Non-Maximum Suppression (NMS).
Parameters:
- boxes: ndarray of shape (N, 4), where each row is [xmin, ymin, xmax, ymax].
- scores: ndarray of shape (N,), scores for each box.
- iou_thresh: float, IoU threshold for NMS.
Returns:
- List of indices of boxes to keep.
@param boxes: An ndarray of shape (N, 4), where each row is [xmin, ymin, xmax,
ymax].
@type boxes: np.ndarray
@param scores: An ndarray of shape (N,), containing the confidence scores for each
box.
@type scores: np.ndarray
@param iou_thresh: The IoU threshold for Non-Maximum Suppression (NMS).
@type iou_thresh: float
@return: A list of indices of the boxes to keep after applying NMS.
@rtype: list[int]
"""
x1 = boxes[:, 0]
y1 = boxes[:, 1]
Expand Down
12 changes: 6 additions & 6 deletions depthai_nodes/ml/helpers/utils/to_planar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ def to_planar(arr: np.ndarray, shape: tuple) -> np.ndarray:
"""Converts the input image `arr` (NumPy array) to the planar format expected by
depthai. The image is resized to the dimensions specified in `shape`.
Parameters:
- arr: Input NumPy array (image).
- shape: Target dimensions (width, height).
Returns:
- A 1D NumPy array with the planar image data.
@param arr: Input NumPy array (image).
@type arr: np.ndarray
@param shape: Target dimensions (width, height).
@type shape: tuple
@return: A 1D NumPy array with the planar image data.
@rtype: np.ndarray
"""
if arr.shape[:2] == shape:
resized = arr
Expand Down

0 comments on commit a27d09d

Please sign in to comment.