-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameClassification.ts
More file actions
59 lines (49 loc) · 1.92 KB
/
Copy pathFrameClassification.ts
File metadata and controls
59 lines (49 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function hexToFloatArray(hexString: string): number[] {
const regex = /.{2}/g; // matches every 2 characters
const hexPairs = hexString.match(regex) || []; // split into pairs of hex characters
const array = hexPairs.map((hex) => parseInt(hex, 16) / 255); // decode and scale each pair
return array;
}
export class FrameClassification {
NO_CAT?: number;
CAT_TRANSIT?: number;
CAT_CLEAR?: number;
CAT_PREY?: number;
HUMAN_ACTIVITY?: number;
constructor(classificationOutputs: number[] | string) {
if (!classificationOutputs) {
return;
}
if (typeof classificationOutputs === "string") {
classificationOutputs = hexToFloatArray(classificationOutputs);
}
this.NO_CAT = classificationOutputs[0];
this.CAT_TRANSIT = classificationOutputs[1];
this.CAT_CLEAR = classificationOutputs[2];
this.CAT_PREY = classificationOutputs[3];
this.HUMAN_ACTIVITY = classificationOutputs[4];
}
get topK(): [string, number][] {
// Convert the classes object to an array of label/value pairs
const arrayOfPairs = Object.entries(this);
// Sort the array based on the value in descending order
const sortedPairs = arrayOfPairs.sort((a, b) => b[1] - a[1]);
return sortedPairs;
}
/**
* Highest-scoring class, or undefined when no score is populated. Unlike
* `topK` this tolerates a short/absent score vector, since it is what gets
* persisted as `snapshot.classification_label`.
*/
get topLabel(): string | undefined {
let topLabel: string | undefined;
let topScore = -Infinity;
for (const [label, score] of Object.entries(this)) {
if (typeof score === "number" && Number.isFinite(score) && score > topScore) {
topScore = score;
topLabel = label;
}
}
return topLabel;
}
}