|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {Pose} from '../types'; |
| 19 | +import {validateTrackerConfig} from './tracker_utils'; |
| 20 | +import {Track} from './interfaces/common_interfaces'; |
| 21 | +import {TrackerConfig} from './interfaces/config_interfaces'; |
| 22 | + |
| 23 | +/** |
| 24 | + * A stateful tracker for associating detections between frames. This is an |
| 25 | + * abstract base class that performs generic mechanics. Implementations must |
| 26 | + * inherit from this class. |
| 27 | + */ |
| 28 | +export abstract class Tracker { |
| 29 | + private tracks: Track[]; |
| 30 | + private readonly maxTracks: number; |
| 31 | + private readonly maxAge: number; |
| 32 | + |
| 33 | + constructor(config: TrackerConfig) { |
| 34 | + validateTrackerConfig(config); |
| 35 | + this.maxTracks = config.maxTracks; |
| 36 | + this.maxAge = config.maxAge; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Tracks person instances across frames based on detections. |
| 41 | + * @param poses An array of detected `Pose`s. |
| 42 | + * @param timestamp The timestamp associated with the incoming poses. |
| 43 | + * @returns An updated array of `Pose`s with tracking id properties. |
| 44 | + */ |
| 45 | + apply( |
| 46 | + poses: Pose[], timestamp: number): Pose[] { |
| 47 | + this.filterOldTracks(timestamp); |
| 48 | + const simMatrix = this.computeSimilarity(poses); |
| 49 | + this.assignTracks(poses, simMatrix, timestamp); |
| 50 | + this.updateTracks(timestamp); |
| 51 | + return poses; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Computes pairwise similarity scores between detections and tracks, based |
| 56 | + * on detected features. |
| 57 | + * @param poses An array of detected `Pose`s. |
| 58 | + * @returns A 2D array of shape [num_det, num_tracks] with pairwise |
| 59 | + * similarity scores between detections and tracks. |
| 60 | + */ |
| 61 | + abstract computeSimilarity( |
| 62 | + poses: Pose[]): number[][]; |
| 63 | + |
| 64 | + /** |
| 65 | + * Filters tracks based on their age. |
| 66 | + * @param timestamp The current timestamp in milliseconds. |
| 67 | + */ |
| 68 | + filterOldTracks(timestamp: number): void { |
| 69 | + this.tracks = this.tracks.filter(track => { |
| 70 | + return timestamp - track.lastTimestamp <= this.maxAge; |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Performs an optimization to link detections with tracks. The `poses` |
| 76 | + * array is updated in place by providing an `id` property. If incoming |
| 77 | + * detections are not linked with existing tracks, new tracks will be created. |
| 78 | + * @param poses An array of detected `Pose's. |
| 79 | + * @param simMatrix A 2D array of shape [num_det, num_tracks] with pairwise |
| 80 | + * similarity scores between detections and tracks. |
| 81 | + * @param timestamp The current timestamp in milliseconds. |
| 82 | + */ |
| 83 | + assignTracks( |
| 84 | + poses: Pose[], simMatrix: number[][], timestamp: number): void { |
| 85 | + //TODO: Implement optimization and track store mechanics. |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Updates the stored tracks in the tracker. Specifically, the following |
| 90 | + * operations are applied in order: |
| 91 | + * 1. Tracks are sorted based on freshness (i.e. the most recently linked |
| 92 | + * tracks are placed at the beginning of the array and the most stale are |
| 93 | + * at the end). |
| 94 | + * 2. The tracks array is sliced to only contain `maxTracks` tracks (i.e. the |
| 95 | + * most fresh tracks). |
| 96 | + * @param timestamp The current timestamp in milliseconds. |
| 97 | + */ |
| 98 | + updateTracks(timestamp: number): void { |
| 99 | + // Sort tracks from most recent to most stale, and then only keep the top |
| 100 | + // `maxTracks` tracks. |
| 101 | + this.tracks.sort((ta, tb) => tb.lastTimestamp - ta.lastTimestamp); |
| 102 | + this.tracks = this.tracks.slice(0, this.maxTracks); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Removes specific tracks, based on their ids. |
| 107 | + */ |
| 108 | + remove(...ids: number[]): void { |
| 109 | + this.tracks = this.tracks.filter(track => !ids.includes(track.id)); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Resets tracks. |
| 114 | + */ |
| 115 | + reset(): void { |
| 116 | + this.tracks = []; |
| 117 | + } |
| 118 | +} |
0 commit comments