yolo-helpers
Version:
Helper functions to use models converted from YOLO in browser and Node.js
102 lines (101 loc) • 3.19 kB
TypeScript
import type * as tf_type from '@tensorflow/tfjs';
import { BoundingBox } from '../yolo-box/common';
/** [height, width, num_channels] -> 0 for background, 1 for object */
export type Mask = number[][];
export type BoundingBoxWithMaskCoefficients = BoundingBox & {
/** 32 coefficients of mask */
mask_coefficients: number[];
};
/**
* output shape: [batch, box]
*
* Array of batches, each containing array of detected bounding boxes with masks coefficients and masks
* */
export type SegmentResult = {
bounding_boxes: BoundingBoxWithMaskCoefficients[];
/** e.g. [mask_height, mask_width, 32] for 32 channels of masks */
masks: Mask[];
}[];
export type ImageSize = {
width: number;
height: number;
};
export type DecodeSegmentArgs = {
/**
* tensorflow runtime:
* - browser: `import * as tf from '@tensorflow/tfjs'`
* - nodejs: `import * as tf from '@tensorflow/tfjs-node'`
*/
tf: typeof tf_type;
/** e.g. `1` for single class */
num_classes: number;
/**
* Number of channels in segmentation mask
* default: `32`
*/
num_channels?: number;
/** batched predict result, e.g. 1x116x8400 */
output_boxes: number[][][];
/** batched predict result, e.g. 1x160x160x32 */
output_masks: number[][][][];
/**
* Number of boxes to return using non-max suppression.
* If not provided, all boxes will be returned
*
* e.g. `1` for only selecting the bounding box with highest confidence.
*/
maxOutputSize?: number;
/**
* the threshold for deciding whether boxes overlap too much with respect to IOU.
*
* default: `0.5`
*/
iouThreshold?: number;
/**
* the threshold for deciding whether a box is a valid detection.
*
* default: `-Infinity`
*/
scoreThreshold?: number;
} & ({
/** default: `{ width: 640, height: 640 }` */
input_shape: ImageSize;
} | {
/** default: input_shape / 4 */
mask_shape: ImageSize;
});
/**
* tensorflow output: boxes [batch, features, channel] and masks [batch, height, width, channel]
*
* box features:
* - 4: x, y, width, height
* - num_classes: class confidence
* - 32: channel coefficients
*
* segmentation mask:
* - 0 for background, 1 for object
* - 32 channels, correspond to the 32 channel coefficients in the bounding box
*
* The x, y, width, height are in pixel unit, NOT normalized in the range of [0, 1].
* The the pixel units are scaled to the input_shape.
*
* The confidence are already normalized between 0 to 1.
*/
export declare function decodeSegment(args: DecodeSegmentArgs): Promise<SegmentResult>;
/**
* Sync version of `decodeSegment`.
*/
export declare function decodeSegmentSync(args: DecodeSegmentArgs): SegmentResult;
/**
* @description final mask = mask coefficients * mask channels
*/
export declare function combineMask(bounding_box: BoundingBoxWithMaskCoefficients,
/** e.g. [mask_height, mask_width, 32] for 32 channels of masks */
masks: Mask[]): number[][];
export type Rect = {
left: number;
top: number;
right: number;
bottom: number;
};
export declare function hasOverlap(a: Rect, b: Rect): boolean;