@depz/camera-sdk 0.0.0-stage → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +136 -2
- package/dist/calibration.d.ts +168 -0
- package/dist/calibration.js +251 -0
- package/dist/cameras.d.ts +91 -0
- package/dist/cameras.js +118 -0
- package/dist/errors.d.ts +25 -0
- package/dist/errors.js +71 -0
- package/dist/frames.d.ts +108 -0
- package/dist/frames.js +184 -0
- package/dist/generated/constants.g.d.ts +158 -0
- package/dist/generated/constants.g.js +171 -0
- package/dist/generated/error_code.g.d.ts +86 -0
- package/dist/generated/error_code.g.js +133 -0
- package/dist/generated/records.g.d.ts +708 -0
- package/dist/generated/records.g.js +20 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +81 -0
- package/dist/inference.d.ts +175 -0
- package/dist/inference.js +223 -0
- package/dist/native.d.ts +136 -0
- package/dist/native.js +38 -0
- package/dist/pipeline.d.ts +225 -0
- package/dist/pipeline.js +384 -0
- package/dist/settings.d.ts +64 -0
- package/dist/settings.js +154 -0
- package/dist/sources.d.ts +137 -0
- package/dist/sources.js +229 -0
- package/package.json +40 -4
package/dist/native.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.native = void 0;
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
// The addon (addon/addon.cc) and its loading. Internal: the public API is index.ts.
|
|
6
|
+
//
|
|
7
|
+
// The addon is found, in order: DEPZ_NODE_ADDON (a path to depz_node.node, for tests of another
|
|
8
|
+
// build); the platform package @depz/camera-sdk-<platform>-<arch> (a release: the addon and
|
|
9
|
+
// libdepz* side by side); this package's build/Release (a development build: npm run build).
|
|
10
|
+
const path = require("node:path");
|
|
11
|
+
function candidates() {
|
|
12
|
+
const out = [];
|
|
13
|
+
if (process.env.DEPZ_NODE_ADDON)
|
|
14
|
+
out.push(path.resolve(process.env.DEPZ_NODE_ADDON));
|
|
15
|
+
try {
|
|
16
|
+
const pkg = require.resolve(`@depz/camera-sdk-${process.platform}-${process.arch}/package.json`);
|
|
17
|
+
out.push(path.join(path.dirname(pkg), "depz_node.node"));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// no platform package: a development build
|
|
21
|
+
}
|
|
22
|
+
out.push(path.join(__dirname, "..", "build", "Release", "depz_node.node"));
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
function load() {
|
|
26
|
+
const tried = [];
|
|
27
|
+
for (const file of candidates()) {
|
|
28
|
+
try {
|
|
29
|
+
return require(file);
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
tried.push(`${file}: ${e.message.split("\n")[0]}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
throw new Error(`@depz/camera-sdk: no addon for ${process.platform}-${process.arch}:\n ${tried.join("\n ")}`);
|
|
36
|
+
}
|
|
37
|
+
/** The addon, loaded once. */
|
|
38
|
+
exports.native = load();
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { ErrorCode } from "./errors";
|
|
2
|
+
import { Calibration, type Control } from "./calibration";
|
|
3
|
+
import { FramePacket } from "./frames";
|
|
4
|
+
import { DepthStage } from "./inference";
|
|
5
|
+
import { type NativeHandle } from "./native";
|
|
6
|
+
import { type CorrectionTerm, type RecalibrationMode } from "./settings";
|
|
7
|
+
import { FrameSource, type SourceInfo } from "./sources";
|
|
8
|
+
/** A stream the packets can carry (enabling one enables what it needs). */
|
|
9
|
+
export type Stream = "raw" | "rectified" | "disparity" | "depth" | "cloud";
|
|
10
|
+
/** Every stream, in the order of their bits. */
|
|
11
|
+
export declare const STREAMS: Stream[];
|
|
12
|
+
/** What a full output queue does: drop its oldest packet, or hold the pipeline back until the caller takes one. */
|
|
13
|
+
export type OutputDropPolicy = "drop_oldest" | "block";
|
|
14
|
+
/** How much work recalibration does: `normal`, or `light` (a fifth of the matching cost, for weak devices). */
|
|
15
|
+
export type RecalibrationProfile = "normal" | "light";
|
|
16
|
+
/** The options of a `RecalibrationConfig` (each one optional: its default). */
|
|
17
|
+
export interface RecalibrationConfigOptions {
|
|
18
|
+
mode?: RecalibrationMode;
|
|
19
|
+
/** null: the board's (light on a Raspberry Pi 4), else normal. */
|
|
20
|
+
profile?: RecalibrationProfile | null;
|
|
21
|
+
persist?: boolean;
|
|
22
|
+
webCopy?: boolean;
|
|
23
|
+
autoPitch?: boolean;
|
|
24
|
+
autoRoll?: boolean;
|
|
25
|
+
autoYaw?: boolean;
|
|
26
|
+
autoTy?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Settings of recalibration (the reference's RecalibrationConfig). */
|
|
29
|
+
export declare class RecalibrationConfig {
|
|
30
|
+
readonly mode: RecalibrationMode;
|
|
31
|
+
readonly profile: RecalibrationProfile | null;
|
|
32
|
+
readonly persist: boolean;
|
|
33
|
+
readonly webCopy: boolean;
|
|
34
|
+
readonly autoPitch: boolean;
|
|
35
|
+
readonly autoRoll: boolean;
|
|
36
|
+
readonly autoYaw: boolean;
|
|
37
|
+
readonly autoTy: boolean;
|
|
38
|
+
/** The defaults: continuous, the board's profile, every correction stored in the camera with a copy on the web, all four terms. */
|
|
39
|
+
constructor(options?: RecalibrationConfigOptions);
|
|
40
|
+
/** The terms recalibration may not change (auto_<term> off). */
|
|
41
|
+
get heldTerms(): Set<CorrectionTerm>;
|
|
42
|
+
/** Internal: the C record. */
|
|
43
|
+
toRaw(): {
|
|
44
|
+
mode: number;
|
|
45
|
+
profile: number;
|
|
46
|
+
persist: number;
|
|
47
|
+
webCopy: number;
|
|
48
|
+
heldTerms: number;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** The options of a `PipelineConfig` (each one optional: its default). */
|
|
52
|
+
export interface PipelineConfigOptions {
|
|
53
|
+
/** null: depth for a pipeline with stages, rectified for one without. */
|
|
54
|
+
streams?: Iterable<Stream> | null;
|
|
55
|
+
maxOutputQueue?: number;
|
|
56
|
+
/** Frames in flight; null: the SDK's rule. */
|
|
57
|
+
pipelineDepth?: number | null;
|
|
58
|
+
/** null: by the source (block for an every-frame replay, else drop_oldest). */
|
|
59
|
+
outputDropPolicy?: OutputDropPolicy | null;
|
|
60
|
+
photoMode?: boolean;
|
|
61
|
+
/** null: the settings' recalibration. */
|
|
62
|
+
recalibration?: RecalibrationConfig | null;
|
|
63
|
+
/** null: never reported. */
|
|
64
|
+
noFramesAfterS?: number | null;
|
|
65
|
+
/** [height, width] of the canvas when no stage asks for one; null: the eye's capture size. */
|
|
66
|
+
canvasSize?: readonly [number, number] | null;
|
|
67
|
+
}
|
|
68
|
+
/** How a pipeline runs (the reference's PipelineConfig). */
|
|
69
|
+
export declare class PipelineConfig {
|
|
70
|
+
readonly streams: ReadonlySet<Stream> | null;
|
|
71
|
+
readonly maxOutputQueue: number;
|
|
72
|
+
readonly pipelineDepth: number | null;
|
|
73
|
+
readonly outputDropPolicy: OutputDropPolicy | null;
|
|
74
|
+
readonly photoMode: boolean;
|
|
75
|
+
readonly recalibration: RecalibrationConfig | null;
|
|
76
|
+
readonly noFramesAfterS: number | null;
|
|
77
|
+
readonly canvasSize: readonly [number, number] | null;
|
|
78
|
+
/** Refuses (invalid_argument) a stream, a size or a policy the pipeline does not know. */
|
|
79
|
+
constructor(options?: PipelineConfigOptions);
|
|
80
|
+
}
|
|
81
|
+
/** The controls of one scope of a pipeline (the reference's ControlSet): "pipeline", "inference"
|
|
82
|
+
* (the depth stage's model), "calibration", "camera", "filter:<slug>", "stage:<name>". */
|
|
83
|
+
export declare class ControlSet {
|
|
84
|
+
readonly scope: string;
|
|
85
|
+
private readonly p;
|
|
86
|
+
private readonly sc;
|
|
87
|
+
private readonly owner;
|
|
88
|
+
/** @internal */
|
|
89
|
+
constructor(p: NativeHandle, scope: string);
|
|
90
|
+
/** Every control of the scope with its value now. */
|
|
91
|
+
list(): Control[];
|
|
92
|
+
/** A control's value now (null: not known; a camera control that could not be read). */
|
|
93
|
+
get(name: string): number | string | null;
|
|
94
|
+
/** Sets a control (coerced: clamped, snapped); the value in force after. */
|
|
95
|
+
set(name: string, value: number | string | boolean): number | string | null;
|
|
96
|
+
/** Several controls; the names not applied. */
|
|
97
|
+
setMany(values: Record<string, number | string | boolean>): string[];
|
|
98
|
+
/** Every control back to its default; the names not applied. */
|
|
99
|
+
reset(): string[];
|
|
100
|
+
}
|
|
101
|
+
/** A finished recording (the reference's RecordingResult). */
|
|
102
|
+
export interface RecordingResult {
|
|
103
|
+
readonly path: string;
|
|
104
|
+
readonly frames: number;
|
|
105
|
+
/** The camera's frames' bytes (not the file's size). */
|
|
106
|
+
readonly bytes: number;
|
|
107
|
+
readonly durationS: number;
|
|
108
|
+
readonly dropped: number;
|
|
109
|
+
readonly width: number;
|
|
110
|
+
readonly height: number;
|
|
111
|
+
readonly captureMode: string;
|
|
112
|
+
/** The writer's failure; null when none. */
|
|
113
|
+
readonly error: string | null;
|
|
114
|
+
}
|
|
115
|
+
/** A recording under way (the reference's RecordingStatus). */
|
|
116
|
+
export interface RecordingStatus {
|
|
117
|
+
readonly isRecording: boolean;
|
|
118
|
+
readonly path: string;
|
|
119
|
+
readonly framesWritten: number;
|
|
120
|
+
readonly bytesWritten: number;
|
|
121
|
+
readonly dropped: number;
|
|
122
|
+
readonly durationS: number;
|
|
123
|
+
readonly isPaused: boolean;
|
|
124
|
+
readonly width: number;
|
|
125
|
+
readonly height: number;
|
|
126
|
+
readonly captureMode: string;
|
|
127
|
+
readonly error: string | null;
|
|
128
|
+
}
|
|
129
|
+
/** A pipeline's recording: the MKV of contracts/recording/FORMAT.md, written by the SDK. */
|
|
130
|
+
export declare class Recording {
|
|
131
|
+
private readonly p;
|
|
132
|
+
/** @internal */
|
|
133
|
+
constructor(p: NativeHandle);
|
|
134
|
+
/** Starts recording what the source delivers (its own bytes) on a running pipeline.
|
|
135
|
+
* cameraControls: the controls the file starts with; omitted: as the camera reports them. */
|
|
136
|
+
start(path: string, options?: {
|
|
137
|
+
cameraControls?: Record<string, number> | null;
|
|
138
|
+
}): void;
|
|
139
|
+
/** Finishes the file. */
|
|
140
|
+
stop(): Promise<RecordingResult>;
|
|
141
|
+
/** Stops writing frames, keeping the file open (nothing recorded: nothing done). */
|
|
142
|
+
pause(): void;
|
|
143
|
+
/** Writes frames again. */
|
|
144
|
+
resume(): void;
|
|
145
|
+
/** The recording under way; null when nothing is recorded. */
|
|
146
|
+
status(): RecordingStatus | null;
|
|
147
|
+
/** A recording of one frame: the next one the source delivers (frames 0: none came in time). */
|
|
148
|
+
captureStill(path: string, timeoutMs?: number): Promise<RecordingResult>;
|
|
149
|
+
}
|
|
150
|
+
/** A pipeline's replay of a recording (the reference's Replay). */
|
|
151
|
+
export declare class Replay {
|
|
152
|
+
private readonly p;
|
|
153
|
+
/** @internal */
|
|
154
|
+
constructor(p: NativeHandle);
|
|
155
|
+
/** No frame is handed out until resume. */
|
|
156
|
+
pause(): void;
|
|
157
|
+
/** Plays on from the position (no catching up on the time paused). */
|
|
158
|
+
resume(): void;
|
|
159
|
+
/** To the frame at or before ptsUs, or exactly that frame of the index (a running pipeline). */
|
|
160
|
+
seek(to: {
|
|
161
|
+
ptsUs: number;
|
|
162
|
+
} | {
|
|
163
|
+
frame: number;
|
|
164
|
+
}): void;
|
|
165
|
+
/** One frame forward (+1) or back (-1). */
|
|
166
|
+
step(direction: 1 | -1): void;
|
|
167
|
+
get isPaused(): boolean;
|
|
168
|
+
/** The recording played to its end and every frame of it taken. */
|
|
169
|
+
get atEof(): boolean;
|
|
170
|
+
}
|
|
171
|
+
/** What a pipeline runs (the reference's PipelineInfo, the parts the C ABI carries). */
|
|
172
|
+
export interface PipelineInfo {
|
|
173
|
+
readonly isRunning: boolean;
|
|
174
|
+
readonly model: string;
|
|
175
|
+
readonly variant: string;
|
|
176
|
+
readonly engine: string;
|
|
177
|
+
/** [height, width]; null without a model. */
|
|
178
|
+
readonly inputSize: readonly [number, number] | null;
|
|
179
|
+
readonly streams: ReadonlySet<Stream>;
|
|
180
|
+
}
|
|
181
|
+
/** A running pipeline's last problem (no_frames, capture_stopped, ...). */
|
|
182
|
+
export interface ErrorInfo {
|
|
183
|
+
readonly code: ErrorCode;
|
|
184
|
+
readonly message: string;
|
|
185
|
+
}
|
|
186
|
+
/** A source and its stages, run by the SDK's worker. Built without I/O; start() opens. */
|
|
187
|
+
export declare class Pipeline {
|
|
188
|
+
readonly source: FrameSource;
|
|
189
|
+
readonly stages: readonly DepthStage[];
|
|
190
|
+
readonly config: PipelineConfig;
|
|
191
|
+
/** @internal */
|
|
192
|
+
readonly _h: NativeHandle;
|
|
193
|
+
/** Set when a finite source gave its last frame and every frame was taken. */
|
|
194
|
+
atEof: boolean;
|
|
195
|
+
/** Its recording. */
|
|
196
|
+
readonly recording: Recording;
|
|
197
|
+
/** Its replay; null for a live source. */
|
|
198
|
+
readonly replay: Replay | null;
|
|
199
|
+
/** A pipeline on a source with its stages, run in this order on every frame; no I/O until start(). */
|
|
200
|
+
constructor(source: FrameSource, stages?: readonly DepthStage[], config?: PipelineConfig | null);
|
|
201
|
+
/** The controls of a scope ("pipeline", "inference", "calibration", "camera", "filter:<slug>", "stage:<name>"). */
|
|
202
|
+
controls(scope: string): ControlSet;
|
|
203
|
+
/** Opens the source and prepares the stages (a model's engine warms up). Once per pipeline. */
|
|
204
|
+
start(options?: {
|
|
205
|
+
progress?: (stage: string, done: number, total: number) => void;
|
|
206
|
+
signal?: AbortSignal;
|
|
207
|
+
}): Promise<void>;
|
|
208
|
+
/** The next frame, waiting up to timeoutMs (-1 or omitted: no limit); null when nothing came in
|
|
209
|
+
* time or at the end of a finite source (atEof set). */
|
|
210
|
+
get(timeoutMs?: number): Promise<FramePacket | null>;
|
|
211
|
+
/** The frames until the end of a finite source (a live source: until stop). */
|
|
212
|
+
frames(timeoutMs?: number): AsyncGenerator<FramePacket>;
|
|
213
|
+
/** Closes the source and stops the SDK's threads of this pipeline. */
|
|
214
|
+
stop(): Promise<void>;
|
|
215
|
+
/** What it runs now. */
|
|
216
|
+
get info(): PipelineInfo;
|
|
217
|
+
/** The opened source's info. */
|
|
218
|
+
get sourceInfo(): SourceInfo;
|
|
219
|
+
/** The last problem of the running pipeline; null when none. */
|
|
220
|
+
get lastError(): ErrorInfo | null;
|
|
221
|
+
/** The calibration in force now (with its correction): a snapshot. */
|
|
222
|
+
get activeCalibration(): Calibration;
|
|
223
|
+
/** Releases the C pipeline now (else the garbage collector does). */
|
|
224
|
+
release(): void;
|
|
225
|
+
}
|
package/dist/pipeline.js
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Pipeline = exports.Replay = exports.Recording = exports.ControlSet = exports.PipelineConfig = exports.RecalibrationConfig = exports.STREAMS = void 0;
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
// The pipeline (the reference's Pipeline, PipelineConfig, RecalibrationConfig): a source and its
|
|
6
|
+
// stages run by the SDK's worker; start, get and stop are Promises (they wait on a worker thread).
|
|
7
|
+
const C = require("./generated/constants.g");
|
|
8
|
+
const errors_1 = require("./errors");
|
|
9
|
+
const calibration_1 = require("./calibration");
|
|
10
|
+
const frames_1 = require("./frames");
|
|
11
|
+
const inference_1 = require("./inference");
|
|
12
|
+
const native_1 = require("./native");
|
|
13
|
+
const settings_1 = require("./settings");
|
|
14
|
+
const sources_1 = require("./sources");
|
|
15
|
+
const STREAM_BITS = {
|
|
16
|
+
raw: C.DEPZ_STREAM_RAW, rectified: C.DEPZ_STREAM_RECTIFIED, disparity: C.DEPZ_STREAM_DISPARITY,
|
|
17
|
+
depth: C.DEPZ_STREAM_DEPTH, cloud: C.DEPZ_STREAM_CLOUD,
|
|
18
|
+
};
|
|
19
|
+
/** Every stream, in the order of their bits. */
|
|
20
|
+
exports.STREAMS = Object.keys(STREAM_BITS);
|
|
21
|
+
/** Settings of recalibration (the reference's RecalibrationConfig). */
|
|
22
|
+
class RecalibrationConfig {
|
|
23
|
+
mode;
|
|
24
|
+
profile;
|
|
25
|
+
persist;
|
|
26
|
+
webCopy;
|
|
27
|
+
autoPitch;
|
|
28
|
+
autoRoll;
|
|
29
|
+
autoYaw;
|
|
30
|
+
autoTy;
|
|
31
|
+
/** The defaults: continuous, the board's profile, every correction stored in the camera with a copy on the web, all four terms. */
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
const known = ["mode", "profile", "persist", "webCopy", "autoPitch", "autoRoll", "autoYaw", "autoTy"];
|
|
34
|
+
for (const k of Object.keys(options))
|
|
35
|
+
if (!known.includes(k))
|
|
36
|
+
throw (0, errors_1.invalidArgument)(`unknown recalibration settings: ${k}`);
|
|
37
|
+
this.mode = (0, settings_1.checkMode)(options.mode ?? "continuous", "mode");
|
|
38
|
+
const profile = options.profile ?? null;
|
|
39
|
+
if (profile !== null && profile !== "normal" && profile !== "light") {
|
|
40
|
+
throw (0, errors_1.invalidArgument)(`profile must be one of normal, light, got ${JSON.stringify(profile)}`);
|
|
41
|
+
}
|
|
42
|
+
this.profile = profile;
|
|
43
|
+
const flags = { persist: true, webCopy: true, autoPitch: true, autoRoll: true, autoYaw: true, autoTy: true };
|
|
44
|
+
for (const name of Object.keys(flags)) {
|
|
45
|
+
const v = options[name] ?? flags[name];
|
|
46
|
+
if (typeof v !== "boolean")
|
|
47
|
+
throw (0, errors_1.invalidArgument)(`RecalibrationConfig.${name} must be a bool`);
|
|
48
|
+
flags[name] = v;
|
|
49
|
+
}
|
|
50
|
+
this.persist = flags.persist;
|
|
51
|
+
this.webCopy = flags.webCopy;
|
|
52
|
+
this.autoPitch = flags.autoPitch;
|
|
53
|
+
this.autoRoll = flags.autoRoll;
|
|
54
|
+
this.autoYaw = flags.autoYaw;
|
|
55
|
+
this.autoTy = flags.autoTy;
|
|
56
|
+
Object.freeze(this);
|
|
57
|
+
}
|
|
58
|
+
/** The terms recalibration may not change (auto_<term> off). */
|
|
59
|
+
get heldTerms() {
|
|
60
|
+
const auto = { pitch: this.autoPitch, roll: this.autoRoll, yaw: this.autoYaw, ty: this.autoTy };
|
|
61
|
+
return new Set(settings_1.CORRECTION_TERMS.filter((t) => !auto[t]));
|
|
62
|
+
}
|
|
63
|
+
/** Internal: the C record. */
|
|
64
|
+
toRaw() {
|
|
65
|
+
let held = 0;
|
|
66
|
+
if (!this.autoPitch)
|
|
67
|
+
held |= C.DEPZ_TERM_PITCH;
|
|
68
|
+
if (!this.autoRoll)
|
|
69
|
+
held |= C.DEPZ_TERM_ROLL;
|
|
70
|
+
if (!this.autoYaw)
|
|
71
|
+
held |= C.DEPZ_TERM_YAW;
|
|
72
|
+
if (!this.autoTy)
|
|
73
|
+
held |= C.DEPZ_TERM_TY;
|
|
74
|
+
return {
|
|
75
|
+
mode: (0, settings_1.recalibrationModeValue)(this.mode),
|
|
76
|
+
profile: this.profile === null ? C.DEPZ_PROFILE_DEFAULT : this.profile === "light" ? C.DEPZ_PROFILE_LIGHT : C.DEPZ_PROFILE_NORMAL,
|
|
77
|
+
persist: this.persist ? C.DEPZ_SWITCH_ON : C.DEPZ_SWITCH_OFF,
|
|
78
|
+
webCopy: this.webCopy ? C.DEPZ_SWITCH_ON : C.DEPZ_SWITCH_OFF,
|
|
79
|
+
heldTerms: held,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.RecalibrationConfig = RecalibrationConfig;
|
|
84
|
+
const wholeAtLeast1 = (v) => typeof v === "number" && Number.isInteger(v) && v >= 1;
|
|
85
|
+
/** How a pipeline runs (the reference's PipelineConfig). */
|
|
86
|
+
class PipelineConfig {
|
|
87
|
+
streams;
|
|
88
|
+
maxOutputQueue;
|
|
89
|
+
pipelineDepth;
|
|
90
|
+
outputDropPolicy;
|
|
91
|
+
photoMode;
|
|
92
|
+
recalibration;
|
|
93
|
+
noFramesAfterS;
|
|
94
|
+
canvasSize;
|
|
95
|
+
/** Refuses (invalid_argument) a stream, a size or a policy the pipeline does not know. */
|
|
96
|
+
constructor(options = {}) {
|
|
97
|
+
const known = ["streams", "maxOutputQueue", "pipelineDepth", "outputDropPolicy", "photoMode", "recalibration", "noFramesAfterS", "canvasSize"];
|
|
98
|
+
for (const k of Object.keys(options))
|
|
99
|
+
if (!known.includes(k))
|
|
100
|
+
throw (0, errors_1.invalidArgument)(`PipelineConfig has no field ${k}`, { field: k });
|
|
101
|
+
if (options.streams === undefined || options.streams === null) {
|
|
102
|
+
this.streams = null;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const s = typeof options.streams === "string" ? null : [...options.streams];
|
|
106
|
+
if (s === null || s.some((x) => !(x in STREAM_BITS))) {
|
|
107
|
+
throw (0, errors_1.invalidArgument)(`streams must be Stream values (${exports.STREAMS.join(", ")}), got ${JSON.stringify(options.streams)}`);
|
|
108
|
+
}
|
|
109
|
+
this.streams = new Set(s);
|
|
110
|
+
}
|
|
111
|
+
const queue = options.maxOutputQueue ?? 4;
|
|
112
|
+
if (!wholeAtLeast1(queue))
|
|
113
|
+
throw (0, errors_1.invalidArgument)(`max_output_queue must be a whole number >= 1, got ${queue}`);
|
|
114
|
+
this.maxOutputQueue = queue;
|
|
115
|
+
const depth = options.pipelineDepth ?? null;
|
|
116
|
+
if (depth !== null && !wholeAtLeast1(depth))
|
|
117
|
+
throw (0, errors_1.invalidArgument)(`pipeline_depth must be a whole number >= 1, got ${depth}`);
|
|
118
|
+
this.pipelineDepth = depth;
|
|
119
|
+
const policy = options.outputDropPolicy ?? null;
|
|
120
|
+
if (policy !== null && policy !== "drop_oldest" && policy !== "block") {
|
|
121
|
+
throw (0, errors_1.invalidArgument)(`output_drop_policy must be one of drop_oldest, block or null, got ${JSON.stringify(policy)}`);
|
|
122
|
+
}
|
|
123
|
+
this.outputDropPolicy = policy;
|
|
124
|
+
const photo = options.photoMode ?? false;
|
|
125
|
+
if (typeof photo !== "boolean")
|
|
126
|
+
throw (0, errors_1.invalidArgument)("photo_mode must be a bool");
|
|
127
|
+
this.photoMode = photo;
|
|
128
|
+
const recal = options.recalibration ?? null;
|
|
129
|
+
if (recal !== null && !(recal instanceof RecalibrationConfig))
|
|
130
|
+
throw (0, errors_1.invalidArgument)("recalibration must be a RecalibrationConfig or None");
|
|
131
|
+
this.recalibration = recal;
|
|
132
|
+
const nf = options.noFramesAfterS === undefined ? 10.0 : options.noFramesAfterS;
|
|
133
|
+
if (nf !== null && !(typeof nf === "number" && nf > 0))
|
|
134
|
+
throw (0, errors_1.invalidArgument)("no_frames_after_s must be a positive number of seconds or None");
|
|
135
|
+
this.noFramesAfterS = nf;
|
|
136
|
+
const cs = options.canvasSize ?? null;
|
|
137
|
+
if (cs !== null && (!Array.isArray(cs) || cs.length !== 2 || !cs.every(wholeAtLeast1))) {
|
|
138
|
+
const shown = Array.isArray(cs) ? `(${cs.join(", ")})` : JSON.stringify(cs); // Python's tuple repr
|
|
139
|
+
throw (0, errors_1.invalidArgument)(`canvas_size must be (height, width), whole numbers >= 1, or None; got ${shown}`);
|
|
140
|
+
}
|
|
141
|
+
this.canvasSize = cs === null ? null : [cs[0], cs[1]];
|
|
142
|
+
Object.freeze(this);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.PipelineConfig = PipelineConfig;
|
|
146
|
+
// ---- controls -----------------------------------------------------------------------------------------
|
|
147
|
+
const SCOPES = {
|
|
148
|
+
pipeline: C.DEPZ_SCOPE_PIPELINE, inference: C.DEPZ_SCOPE_INFERENCE, calibration: C.DEPZ_SCOPE_CALIBRATION,
|
|
149
|
+
camera: C.DEPZ_SCOPE_CAMERA, filter: C.DEPZ_SCOPE_FILTER, stage: C.DEPZ_SCOPE_STAGE,
|
|
150
|
+
};
|
|
151
|
+
/** The controls of one scope of a pipeline (the reference's ControlSet): "pipeline", "inference"
|
|
152
|
+
* (the depth stage's model), "calibration", "camera", "filter:<slug>", "stage:<name>". */
|
|
153
|
+
class ControlSet {
|
|
154
|
+
scope;
|
|
155
|
+
p;
|
|
156
|
+
sc;
|
|
157
|
+
owner;
|
|
158
|
+
/** @internal */
|
|
159
|
+
constructor(p, scope) {
|
|
160
|
+
if (typeof scope !== "string")
|
|
161
|
+
throw (0, errors_1.invalidArgument)("controls: scope must be a string");
|
|
162
|
+
const [kind, ...rest] = scope.split(":");
|
|
163
|
+
const owner = rest.length ? rest.join(":") : null;
|
|
164
|
+
const sc = SCOPES[kind];
|
|
165
|
+
if (sc === undefined || ((kind === "filter" || kind === "stage") !== (owner !== null))) {
|
|
166
|
+
throw (0, errors_1.invalidArgument)(`unknown control scope ${JSON.stringify(scope)} (pipeline, inference, calibration, camera, filter:<slug>, stage:<name>)`);
|
|
167
|
+
}
|
|
168
|
+
this.scope = scope;
|
|
169
|
+
this.p = p;
|
|
170
|
+
this.sc = sc;
|
|
171
|
+
this.owner = owner;
|
|
172
|
+
}
|
|
173
|
+
/** Every control of the scope with its value now. */
|
|
174
|
+
list() {
|
|
175
|
+
return native_1.native.depz_controls(this.p, this.sc, this.owner).map(calibration_1.controlOf);
|
|
176
|
+
}
|
|
177
|
+
/** A control's value now (null: not known; a camera control that could not be read). */
|
|
178
|
+
get(name) {
|
|
179
|
+
return (0, calibration_1.controlOf)(native_1.native.depz_control_get(this.p, this.sc, this.owner, name)).value;
|
|
180
|
+
}
|
|
181
|
+
/** Sets a control (coerced: clamped, snapped); the value in force after. */
|
|
182
|
+
set(name, value) {
|
|
183
|
+
native_1.native.depz_control_set(this.p, this.sc, this.owner, name, typeof value === "boolean" ? (value ? 1 : 0) : value);
|
|
184
|
+
return this.get(name);
|
|
185
|
+
}
|
|
186
|
+
/** Several controls; the names not applied. */
|
|
187
|
+
setMany(values) {
|
|
188
|
+
return JSON.parse(native_1.native.depz_controls_set_many_json(this.p, this.sc, this.owner, JSON.stringify(values)));
|
|
189
|
+
}
|
|
190
|
+
/** Every control back to its default; the names not applied. */
|
|
191
|
+
reset() {
|
|
192
|
+
return JSON.parse(native_1.native.depz_controls_reset(this.p, this.sc, this.owner));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.ControlSet = ControlSet;
|
|
196
|
+
const resultOf = (r) => Object.freeze({
|
|
197
|
+
path: r.path, frames: r.frames, bytes: r.bytes, durationS: r.durationS, dropped: r.dropped, width: r.width,
|
|
198
|
+
height: r.height, captureMode: r.captureMode, error: r.hasError ? r.error : null,
|
|
199
|
+
});
|
|
200
|
+
const statusOf = (r) => Object.freeze({
|
|
201
|
+
isRecording: r.isRecording !== 0, path: r.path, framesWritten: r.framesWritten, bytesWritten: r.bytesWritten,
|
|
202
|
+
dropped: r.dropped, durationS: r.durationS, isPaused: r.isPaused !== 0, width: r.width, height: r.height,
|
|
203
|
+
captureMode: r.captureMode, error: r.hasError ? r.error : null,
|
|
204
|
+
});
|
|
205
|
+
/** A pipeline's recording: the MKV of contracts/recording/FORMAT.md, written by the SDK. */
|
|
206
|
+
class Recording {
|
|
207
|
+
p;
|
|
208
|
+
/** @internal */
|
|
209
|
+
constructor(p) {
|
|
210
|
+
this.p = p;
|
|
211
|
+
}
|
|
212
|
+
/** Starts recording what the source delivers (its own bytes) on a running pipeline.
|
|
213
|
+
* cameraControls: the controls the file starts with; omitted: as the camera reports them. */
|
|
214
|
+
start(path, options = {}) {
|
|
215
|
+
const controls = options.cameraControls ?? null;
|
|
216
|
+
native_1.native.depz_recording_start(this.p, path, { cameraControlsJson: controls === null ? null : JSON.stringify(controls) });
|
|
217
|
+
}
|
|
218
|
+
/** Finishes the file. */
|
|
219
|
+
async stop() {
|
|
220
|
+
return resultOf(await native_1.native.depz_recording_stop(this.p));
|
|
221
|
+
}
|
|
222
|
+
/** Stops writing frames, keeping the file open (nothing recorded: nothing done). */
|
|
223
|
+
pause() {
|
|
224
|
+
native_1.native.depz_recording_pause_resume(this.p, true);
|
|
225
|
+
}
|
|
226
|
+
/** Writes frames again. */
|
|
227
|
+
resume() {
|
|
228
|
+
native_1.native.depz_recording_pause_resume(this.p, false);
|
|
229
|
+
}
|
|
230
|
+
/** The recording under way; null when nothing is recorded. */
|
|
231
|
+
status() {
|
|
232
|
+
const s = native_1.native.depz_recording_status(this.p);
|
|
233
|
+
return s.isRecording || s.path ? statusOf(s) : null;
|
|
234
|
+
}
|
|
235
|
+
/** A recording of one frame: the next one the source delivers (frames 0: none came in time). */
|
|
236
|
+
async captureStill(path, timeoutMs = -1) {
|
|
237
|
+
return resultOf(await native_1.native.depz_recording_capture_still(this.p, path, timeoutMs));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.Recording = Recording;
|
|
241
|
+
// ---- replay --------------------------------------------------------------------------------------------
|
|
242
|
+
/** A pipeline's replay of a recording (the reference's Replay). */
|
|
243
|
+
class Replay {
|
|
244
|
+
p;
|
|
245
|
+
/** @internal */
|
|
246
|
+
constructor(p) {
|
|
247
|
+
this.p = p;
|
|
248
|
+
}
|
|
249
|
+
/** No frame is handed out until resume. */
|
|
250
|
+
pause() {
|
|
251
|
+
native_1.native.depz_replay(this.p, "pause");
|
|
252
|
+
}
|
|
253
|
+
/** Plays on from the position (no catching up on the time paused). */
|
|
254
|
+
resume() {
|
|
255
|
+
native_1.native.depz_replay(this.p, "resume");
|
|
256
|
+
}
|
|
257
|
+
/** To the frame at or before ptsUs, or exactly that frame of the index (a running pipeline). */
|
|
258
|
+
seek(to) {
|
|
259
|
+
if ("frame" in to)
|
|
260
|
+
native_1.native.depz_replay(this.p, "seek_frame", to.frame);
|
|
261
|
+
else
|
|
262
|
+
native_1.native.depz_replay(this.p, "seek_us", to.ptsUs);
|
|
263
|
+
}
|
|
264
|
+
/** One frame forward (+1) or back (-1). */
|
|
265
|
+
step(direction) {
|
|
266
|
+
if (direction !== 1 && direction !== -1)
|
|
267
|
+
throw (0, errors_1.invalidArgument)("step: +1 or -1");
|
|
268
|
+
native_1.native.depz_replay(this.p, "step", direction);
|
|
269
|
+
}
|
|
270
|
+
get isPaused() {
|
|
271
|
+
return native_1.native.depz_replay_status(this.p).isPaused !== 0;
|
|
272
|
+
}
|
|
273
|
+
/** The recording played to its end and every frame of it taken. */
|
|
274
|
+
get atEof() {
|
|
275
|
+
return native_1.native.depz_replay_status(this.p).atEof !== 0;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
exports.Replay = Replay;
|
|
279
|
+
/** A source and its stages, run by the SDK's worker. Built without I/O; start() opens. */
|
|
280
|
+
class Pipeline {
|
|
281
|
+
source;
|
|
282
|
+
stages;
|
|
283
|
+
config;
|
|
284
|
+
/** @internal */
|
|
285
|
+
_h;
|
|
286
|
+
/** Set when a finite source gave its last frame and every frame was taken. */
|
|
287
|
+
atEof = false;
|
|
288
|
+
/** Its recording. */
|
|
289
|
+
recording;
|
|
290
|
+
/** Its replay; null for a live source. */
|
|
291
|
+
replay;
|
|
292
|
+
/** A pipeline on a source with its stages, run in this order on every frame; no I/O until start(). */
|
|
293
|
+
constructor(source, stages = [], config = null) {
|
|
294
|
+
if (!(source instanceof sources_1.FrameSource))
|
|
295
|
+
throw (0, errors_1.invalidArgument)("Pipeline: source must be a FrameSource");
|
|
296
|
+
if (!Array.isArray(stages) || stages.some((s) => !(s instanceof inference_1.DepthStage)))
|
|
297
|
+
throw (0, errors_1.invalidArgument)("Pipeline: stages must be DepthStage objects");
|
|
298
|
+
if (config !== null && !(config instanceof PipelineConfig))
|
|
299
|
+
throw (0, errors_1.invalidArgument)("Pipeline: config must be a PipelineConfig");
|
|
300
|
+
const cfg = config ?? new PipelineConfig();
|
|
301
|
+
const depth = stages[0];
|
|
302
|
+
let streams = 0;
|
|
303
|
+
for (const s of cfg.streams ?? [])
|
|
304
|
+
streams |= STREAM_BITS[s];
|
|
305
|
+
const policy = cfg.outputDropPolicy === null ? C.DEPZ_DROP_SOURCE_DEFAULT
|
|
306
|
+
: cfg.outputDropPolicy === "block" ? C.DEPZ_DROP_BLOCK : C.DEPZ_DROP_OLDEST;
|
|
307
|
+
this._h = native_1.native.depz_pipeline_create(source._h, stages.map((s) => s._h), {
|
|
308
|
+
streams, maxOutputQueue: cfg.maxOutputQueue, pipelineDepth: cfg.pipelineDepth ?? 0, outputDropPolicy: policy,
|
|
309
|
+
photoMode: cfg.photoMode ? C.DEPZ_SWITCH_ON : C.DEPZ_SWITCH_OFF,
|
|
310
|
+
disparityOffsetPx: depth?.disparityOffsetPx ?? 0, monoEye: (0, calibration_1.eyeValue)(depth?.monoEye),
|
|
311
|
+
noFramesAfterS: cfg.noFramesAfterS ?? -1,
|
|
312
|
+
canvasWidth: cfg.canvasSize ? cfg.canvasSize[1] : 0, canvasHeight: cfg.canvasSize ? cfg.canvasSize[0] : 0,
|
|
313
|
+
}, cfg.recalibration?.toRaw() ?? null, depth?.region ? depth.region.toRaw() : null);
|
|
314
|
+
this.source = source;
|
|
315
|
+
this.stages = Object.freeze([...stages]);
|
|
316
|
+
this.config = cfg;
|
|
317
|
+
this.recording = new Recording(this._h);
|
|
318
|
+
this.replay = source instanceof sources_1.MkvSource ? new Replay(this._h) : null;
|
|
319
|
+
}
|
|
320
|
+
/** The controls of a scope ("pipeline", "inference", "calibration", "camera", "filter:<slug>", "stage:<name>"). */
|
|
321
|
+
controls(scope) {
|
|
322
|
+
return new ControlSet(this._h, scope);
|
|
323
|
+
}
|
|
324
|
+
/** Opens the source and prepares the stages (a model's engine warms up). Once per pipeline. */
|
|
325
|
+
async start(options = {}) {
|
|
326
|
+
await native_1.native.depz_pipeline_start(this._h, options.progress ?? null, (0, inference_1.flagOf)(options.signal));
|
|
327
|
+
}
|
|
328
|
+
/** The next frame, waiting up to timeoutMs (-1 or omitted: no limit); null when nothing came in
|
|
329
|
+
* time or at the end of a finite source (atEof set). */
|
|
330
|
+
async get(timeoutMs = -1) {
|
|
331
|
+
const r = await native_1.native.depz_pipeline_get(this._h, timeoutMs);
|
|
332
|
+
if (typeof r === "number") {
|
|
333
|
+
if (r === C.DEPZ_E_END_OF_STREAM)
|
|
334
|
+
this.atEof = true;
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
this.atEof = false; // a frame again (a seek or a step)
|
|
338
|
+
return new frames_1.FramePacket(r);
|
|
339
|
+
}
|
|
340
|
+
/** The frames until the end of a finite source (a live source: until stop). */
|
|
341
|
+
async *frames(timeoutMs = -1) {
|
|
342
|
+
for (;;) {
|
|
343
|
+
const p = await this.get(timeoutMs);
|
|
344
|
+
if (p !== null)
|
|
345
|
+
yield p;
|
|
346
|
+
else if (this.atEof)
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/** Closes the source and stops the SDK's threads of this pipeline. */
|
|
351
|
+
async stop() {
|
|
352
|
+
await native_1.native.depz_pipeline_stop(this._h);
|
|
353
|
+
}
|
|
354
|
+
/** What it runs now. */
|
|
355
|
+
get info() {
|
|
356
|
+
const i = native_1.native.depz_pipeline_info(this._h);
|
|
357
|
+
return Object.freeze({
|
|
358
|
+
isRunning: i.isRunning !== 0, model: i.model, variant: i.variant, engine: i.engine,
|
|
359
|
+
inputSize: i.inputWidth > 0 ? [i.inputHeight, i.inputWidth] : null,
|
|
360
|
+
streams: new Set(exports.STREAMS.filter((s) => (i.streams & STREAM_BITS[s]) !== 0)),
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
/** The opened source's info. */
|
|
364
|
+
get sourceInfo() {
|
|
365
|
+
return (0, sources_1.sourceInfoOf)(native_1.native.depz_pipeline_source_info(this._h));
|
|
366
|
+
}
|
|
367
|
+
/** The last problem of the running pipeline; null when none. */
|
|
368
|
+
get lastError() {
|
|
369
|
+
const e = native_1.native.depz_pipeline_last_error(this._h);
|
|
370
|
+
if (e.status === 0)
|
|
371
|
+
return null;
|
|
372
|
+
const err = (0, errors_1.errorOfStatus)(e.status, e.message, "{}");
|
|
373
|
+
return Object.freeze({ code: err.code, message: err.message });
|
|
374
|
+
}
|
|
375
|
+
/** The calibration in force now (with its correction): a snapshot. */
|
|
376
|
+
get activeCalibration() {
|
|
377
|
+
return new calibration_1.Calibration(native_1.native.depz_pipeline_calibration(this._h));
|
|
378
|
+
}
|
|
379
|
+
/** Releases the C pipeline now (else the garbage collector does). */
|
|
380
|
+
release() {
|
|
381
|
+
this._h.release();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
exports.Pipeline = Pipeline;
|