UNPKG

tflite-object-detection-tracking

Version:

A package to smooth object detection results, from tflite model output

96 lines (95 loc) 3.79 kB
export interface BoundingBox { top: number; left: number; bottom: number; right: number; } export interface RecognizedObject { boundingBoxes: BoundingBox[]; boundingBox: BoundingBox; label: number; scores: number[]; score: number; recognitionCount: number; missedCount: number; } /** * FrameRecognition arguments: * @param height: the height of the device * @param width: the width of the device * @param recognitionCount: optional, minimum number of items you want before being considered as a top object * @param score: optional, minimum threshold of box score to keep track of * @param checkCount: optional, how many items to track for each tracked object */ interface Props { height: number; width: number; recognitionCount?: number; score?: number; checkCount?: number; } /** * Class: FrameRecognition takes the moments "through time" to track each object as the data is fed from the frame processor. * @param trackingObjects: the record of all objects based on arbitrary numberical id as key. The id is not related to anything from the frame processor, it's just for our own tracking. * @param recognizedObjectsCount: just to figure out what the next trackingObject key should be. */ export declare class FrameRecognition { private static readonly MAX_HISTORY_SIZE; private static readonly MAX_MISSED_COUNT; private static readonly TRACKING_THRESHOLD; private trackingObjects; private recognizedObjectsCount; private readonly height; private readonly width; private readonly recognitionCount; private readonly scoreThreshold; private readonly checkCount; constructor({ height, width, recognitionCount, score, checkCount, }: Props); private _cachedConfidentObject; private _lastConfidentObjectCheck; /** * getConfidentObject are the most confident about. * It finds the object with the highest score & count. * @returns RecognizedObject * @todo allow more than 1 output item */ getConfidentObject(): RecognizedObject | null; /** * addFrameData() handles the data directly from the output of the frame processor. It looks for an existing & similar bounding box and either adds new or updates tracking object. * * Frame data from TFLite is comprised of: * @param boundingBoxes - from frame processor as `BoundingBox[]`. Every 4 indexes relates to the index of the label. * @param labels - from frame processor as `{[x: string]: number}`. Map the labels to the dictionary for human readible. * @param scores - from frame processor as `number[]`. */ addFrameData(boundingBoxes: number[], labels: { [x: string]: number; }, scores: number[]): void; /** * addNewObject() adds new record to trackingObjects */ private addNewObject; /** * updateTrackingObject() updates existing record in the trackingObjects. It updates the averages for score, bounding box position. */ private updateTrackingObject; /** * penalizeObject(): called when the object with the given key is not recognized in the latest frame data. It adds to the missedCount so that if the threshold is reached, the entire key of trackingObject will be removed. */ private penalizeObject; /** * findTrackedObjectKey() looks for existing bounding box within threshold. * @param boundingBox * @param label * @returns key from trackingObjects */ private findTrackedObjectKey; /** * updatingAveragedArray() adds a new element to the array, based on FIFO logic if over the threshold. */ private updateAveragedArray; private getAverage; private getAverageBoundingBox; resetAll(): void; } export {};