forked from moq-dev/moq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetection.ts
More file actions
90 lines (67 loc) · 2.54 KB
/
Copy pathdetection.ts
File metadata and controls
90 lines (67 loc) · 2.54 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import * as Moq from "@kixelated/moq";
import { Effect, type Getter, Signal } from "@kixelated/signals";
import * as Comlink from "comlink";
import * as Catalog from "../../catalog";
import type { Video } from ".";
import type { DetectionWorker } from "./detection-worker";
export type DetectionProps = {
enabled?: boolean;
interval?: number;
threshold?: number;
};
export class Detection {
video: Video;
enabled: Signal<boolean>;
objects = new Signal<Catalog.DetectionObjects | undefined>(undefined);
#interval: number;
#threshold: number;
#catalog = new Signal<Catalog.Detection | undefined>(undefined);
readonly catalog: Getter<Catalog.Detection | undefined> = this.#catalog;
#track: Moq.TrackProducer;
signals = new Effect();
constructor(video: Video, props?: DetectionProps) {
this.video = video;
this.enabled = new Signal(props?.enabled ?? false);
this.#interval = props?.interval ?? 1000;
this.#threshold = props?.threshold ?? 0.5;
this.#track = new Moq.TrackProducer(`detection.json`, 1);
this.signals.cleanup(() => this.#track.close());
this.signals.effect(this.#run.bind(this));
}
#run(effect: Effect): void {
if (!effect.get(this.enabled)) return;
if (!effect.get(this.video.enabled)) return;
this.video.broadcast.insertTrack(this.#track.consume());
effect.cleanup(() => this.video.broadcast.removeTrack(this.#track.name));
// Set the detection catalog
this.#catalog.set({
track: { name: this.#track.name, priority: Catalog.u8(this.#track.priority) },
});
const worker = new Worker(new URL("./detection-worker", import.meta.url), { type: "module" });
effect.cleanup(() => worker.terminate());
const api = Comlink.wrap<DetectionWorker>(worker);
let timeout: ReturnType<typeof setTimeout>;
effect.cleanup(() => clearTimeout(timeout));
effect.spawn(async (cancel) => {
const ready = await Promise.race([api.ready(), cancel]);
if (!ready) return;
process();
});
const process = async () => {
const frame = this.video.frame.peek();
if (!frame) return;
const cloned = frame.clone();
const result = await api.detect(Comlink.transfer(cloned, [cloned]), this.#threshold);
this.objects.set(result);
this.#track.appendFrame(new TextEncoder().encode(JSON.stringify(result)));
// Schedule the next detection only after this one is complete.
// Otherwise, we're in trouble if it takes >= interval to complete.
timeout = setTimeout(process, this.#interval);
};
effect.cleanup(() => this.objects.set(undefined));
}
close() {
this.signals.close();
this.#track.close();
}
}