Video Frame Utils
Video-frame decoding and resizing helpers for analysis.
abs_diff(first, second)
Compute the elementwise absolute difference of two uint8 arrays.
This is the NumPy equivalent of cv2.absdiff and avoids unsigned
subtraction overflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
first
|
ndarray
|
First |
required |
second
|
ndarray
|
Second array with the same shape. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: |
decode_bgr(data)
Decode encoded image bytes to a contiguous BGR uint8 array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Encoded image bytes in any format Pillow can open. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A three-channel BGR |
limit_size(frame, max_width, max_height)
Downscale an image to fit within a bounding box while preserving its aspect ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
Non-empty grayscale |
required |
max_width
|
int
|
Maximum output width in pixels. |
required |
max_height
|
int
|
Maximum output height in pixels. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The original frame when it already fits, otherwise an anti-aliased bilinear resize. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
resize_box(frame, size)
Resize a two- or three-dimensional uint8 image with Pillow bilinear resampling.
Pillow widens the bilinear filter support when downscaling, so reduced images are
anti-aliased. Every channel is resampled independently, so the channel order is preserved.
A no-op whenever frame is already at size -- the common case when a caller has
already resized the frame to a detector's preferred input size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
Source grayscale or BGR |
required |
size
|
tuple[int, int]
|
Target |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The resized, writable |
to_grayscale(frame)
Convert a BGR analysis frame to a single-channel grayscale image.
Uses fixed-point BT.601 coefficients consistent with the analysis pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
ndarray
|
BGR |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A two-dimensional grayscale |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is not a three-channel BGR image. |