@mediapipe/tasks-vision 0.1.0-alpha-1668420867

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/vision.d.ts ADDED
@@ -0,0 +1,907 @@
1
+ /// <reference types="@types/offscreencanvas" />
2
+
3
+ import { BaseOptions } from '../../../../tasks/web/core/base_options';
4
+ import { Category } from '../../../../tasks/web/components/containers/category';
5
+ import { Classifications } from '../../../../tasks/web/components/containers/classification_result';
6
+ import { ClassifierOptions as ImageClassifierOptions } from '../../../../tasks/web/core/classifier_options';
7
+ import { ClassificationResult as ImageClassifierResult } from '../../../../tasks/web/components/containers/classification_result';
8
+ import { Landmark } from '../../../../tasks/web/components/containers/landmark';
9
+ import { WasmLoaderOptions } from '../../../../tasks/web/core/wasm_loader_options';
10
+
11
+ /** Listener to be passed in by user for handling output audio data. */
12
+ declare type AudioOutputListener = (output: Float32Array) => void;
13
+
14
+ /** An integer bounding box, axis aligned. */
15
+ export declare interface BoundingBox {
16
+ /** The X coordinate of the top-left corner, in pixels. */
17
+ originX: number;
18
+ /** The Y coordinate of the top-left corner, in pixels. */
19
+ originY: number;
20
+ /** The width of the bounding box, in pixels. */
21
+ width: number;
22
+ /** The height of the bounding box, in pixels. */
23
+ height: number;
24
+ }
25
+
26
+ export { Category }
27
+
28
+ export { Classifications }
29
+
30
+ /** Represents one object detected by the `ObjectDetector`. */
31
+ export declare interface Detection {
32
+ /** A list of `Category` objects. */
33
+ categories: Category[];
34
+ /** The bounding box of the detected objects. */
35
+ boundingBox?: BoundingBox;
36
+ }
37
+
38
+ /** A listener that will be invoked with an absl::StatusCode and message. */
39
+ declare type ErrorListener = (code: number, message: string) => void;
40
+
41
+ /** Performs hand gesture recognition on images. */
42
+ export declare class GestureRecognizer extends TaskRunner {
43
+ private gestures;
44
+ private landmarks;
45
+ private worldLandmarks;
46
+ private handednesses;
47
+ private readonly options;
48
+ private readonly handLandmarkerGraphOptions;
49
+ private readonly handLandmarksDetectorGraphOptions;
50
+ private readonly handDetectorGraphOptions;
51
+ private readonly handGestureRecognizerGraphOptions;
52
+ /**
53
+ * Initializes the Wasm runtime and creates a new gesture recognizer from the
54
+ * provided options.
55
+ * @param wasmLoaderOptions A configuration object that provides the location
56
+ * of the Wasm binary and its loader.
57
+ * @param gestureRecognizerOptions The options for the gesture recognizer.
58
+ * Note that either a path to the model asset or a model buffer needs to
59
+ * be provided (via `baseOptions`).
60
+ */
61
+ static createFromOptions(wasmLoaderOptions: WasmLoaderOptions, gestureRecognizerOptions: GestureRecognizerOptions): Promise<GestureRecognizer>;
62
+ /**
63
+ * Initializes the Wasm runtime and creates a new gesture recognizer based on
64
+ * the provided model asset buffer.
65
+ * @param wasmLoaderOptions A configuration object that provides the location
66
+ * of the Wasm binary and its loader.
67
+ * @param modelAssetBuffer A binary representation of the model.
68
+ */
69
+ static createFromModelBuffer(wasmLoaderOptions: WasmLoaderOptions, modelAssetBuffer: Uint8Array): Promise<GestureRecognizer>;
70
+ /**
71
+ * Initializes the Wasm runtime and creates a new gesture recognizer based on
72
+ * the path to the model asset.
73
+ * @param wasmLoaderOptions A configuration object that provides the location
74
+ * of the Wasm binary and its loader.
75
+ * @param modelAssetPath The path to the model asset.
76
+ */
77
+ static createFromModelPath(wasmLoaderOptions: WasmLoaderOptions, modelAssetPath: string): Promise<GestureRecognizer>;
78
+ constructor(wasmModule: WasmModule);
79
+ /**
80
+ * Sets new options for the gesture recognizer.
81
+ *
82
+ * Calling `setOptions()` with a subset of options only affects those options.
83
+ * You can reset an option back to its default value by explicitly setting it
84
+ * to `undefined`.
85
+ *
86
+ * @param options The options for the gesture recognizer.
87
+ */
88
+ setOptions(options: GestureRecognizerOptions): Promise<void>;
89
+ /**
90
+ * Performs gesture recognition on the provided single image and waits
91
+ * synchronously for the response.
92
+ * @param imageSource An image source to process.
93
+ * @param timestamp The timestamp of the current frame, in ms. If not
94
+ * provided, defaults to `performance.now()`.
95
+ * @return The detected gestures.
96
+ */
97
+ recognize(imageSource: ImageSource, timestamp?: number): GestureRecognizerResult;
98
+ /** Sets the default values for the graph. */
99
+ private initDefaults;
100
+ /** Converts the proto data to a Category[][] structure. */
101
+ private toJsCategories;
102
+ /** Converts raw data into a landmark, and adds it to our landmarks list. */
103
+ private addJsLandmarks;
104
+ /**
105
+ * Converts raw data into a landmark, and adds it to our worldLandmarks
106
+ * list.
107
+ */
108
+ private adddJsWorldLandmarks;
109
+ /** Updates the MediaPipe graph configuration. */
110
+ private refreshGraph;
111
+ }
112
+
113
+ /** Options to configure the MediaPipe Gesture Recognizer Task */
114
+ export declare interface GestureRecognizerOptions {
115
+ /** Options to configure the loading of the model assets. */
116
+ baseOptions?: BaseOptions;
117
+ /**
118
+ * The maximum number of hands can be detected by the GestureRecognizer.
119
+ * Defaults to 1.
120
+ */
121
+ numHands?: number | undefined;
122
+ /**
123
+ * The minimum confidence score for the hand detection to be considered
124
+ * successful. Defaults to 0.5.
125
+ */
126
+ minHandDetectionConfidence?: number | undefined;
127
+ /**
128
+ * The minimum confidence score of hand presence score in the hand landmark
129
+ * detection. Defaults to 0.5.
130
+ */
131
+ minHandPresenceConfidence?: number | undefined;
132
+ /**
133
+ * The minimum confidence score for the hand tracking to be considered
134
+ * successful. Defaults to 0.5.
135
+ */
136
+ minTrackingConfidence?: number | undefined;
137
+ /**
138
+ * Sets the optional `ClassifierOptions` controling the canned gestures
139
+ * classifier, such as score threshold, allow list and deny list of gestures.
140
+ * The categories for canned gesture
141
+ * classifiers are: ["None", "Closed_Fist", "Open_Palm", "Pointing_Up",
142
+ * "Thumb_Down", "Thumb_Up", "Victory", "ILoveYou"]
143
+ */
144
+ cannedGesturesClassifierOptions?: ImageClassifierOptions | undefined;
145
+ /**
146
+ * Options for configuring the custom gestures classifier, such as score
147
+ * threshold, allow list and deny list of gestures.
148
+ */
149
+ customGesturesClassifierOptions?: ImageClassifierOptions | undefined;
150
+ }
151
+
152
+ /**
153
+ * Represents the gesture recognition results generated by `GestureRecognizer`.
154
+ */
155
+ export declare interface GestureRecognizerResult {
156
+ /** Hand landmarks of detected hands. */
157
+ landmarks: Landmark[][];
158
+ /** Hand landmarks in world coordniates of detected hands. */
159
+ worldLandmarks: Landmark[][];
160
+ /** Handedness of detected hands. */
161
+ handednesses: Category[][];
162
+ /** Recognized hand gestures of detected hands */
163
+ gestures: Category[][];
164
+ }
165
+
166
+ /** Performs classification on images. */
167
+ export declare class ImageClassifier extends TaskRunner {
168
+ private classificationResult;
169
+ private readonly options;
170
+ /**
171
+ * Initializes the Wasm runtime and creates a new image classifier from the
172
+ * provided options.
173
+ * @param wasmLoaderOptions A configuration object that provides the location
174
+ * of the Wasm binary and its loader.
175
+ * @param imageClassifierOptions The options for the image classifier. Note
176
+ * that either a path to the model asset or a model buffer needs to be
177
+ * provided (via `baseOptions`).
178
+ */
179
+ static createFromOptions(wasmLoaderOptions: WasmLoaderOptions, imageClassifierOptions: ImageClassifierOptions): Promise<ImageClassifier>;
180
+ /**
181
+ * Initializes the Wasm runtime and creates a new image classifier based on
182
+ * the provided model asset buffer.
183
+ * @param wasmLoaderOptions A configuration object that provides the location
184
+ * of the Wasm binary and its loader.
185
+ * @param modelAssetBuffer A binary representation of the model.
186
+ */
187
+ static createFromModelBuffer(wasmLoaderOptions: WasmLoaderOptions, modelAssetBuffer: Uint8Array): Promise<ImageClassifier>;
188
+ /**
189
+ * Initializes the Wasm runtime and creates a new image classifier based on
190
+ * the path to the model asset.
191
+ * @param wasmLoaderOptions A configuration object that provides the location
192
+ * of the Wasm binary and its loader.
193
+ * @param modelAssetPath The path to the model asset.
194
+ */
195
+ static createFromModelPath(wasmLoaderOptions: WasmLoaderOptions, modelAssetPath: string): Promise<ImageClassifier>;
196
+ /**
197
+ * Sets new options for the image classifier.
198
+ *
199
+ * Calling `setOptions()` with a subset of options only affects those options.
200
+ * You can reset an option back to its default value by explicitly setting it
201
+ * to `undefined`.
202
+ *
203
+ * @param options The options for the image classifier.
204
+ */
205
+ setOptions(options: ImageClassifierOptions): Promise<void>;
206
+ /**
207
+ * Performs image classification on the provided image and waits synchronously
208
+ * for the response.
209
+ *
210
+ * @param imageSource An image source to process.
211
+ * @param timestamp The timestamp of the current frame, in ms. If not
212
+ * provided, defaults to `performance.now()`.
213
+ * @return The classification result of the image
214
+ */
215
+ classify(imageSource: ImageSource, timestamp?: number): ImageClassifierResult;
216
+ /** Updates the MediaPipe graph configuration. */
217
+ private refreshGraph;
218
+ }
219
+
220
+ export { ImageClassifierOptions }
221
+
222
+ export { ImageClassifierResult }
223
+
224
+ /**
225
+ * Valid types of image sources which we can run our WasmMediaPipeLib over.
226
+ */
227
+ export declare type ImageSource = HTMLCanvasElement | HTMLVideoElement | HTMLImageElement | ImageData | ImageBitmap;
228
+
229
+ /** Performs object detection on images. */
230
+ export declare class ObjectDetector extends TaskRunner {
231
+ private detections;
232
+ private readonly options;
233
+ /**
234
+ * Initializes the Wasm runtime and creates a new object detector from the
235
+ * provided options.
236
+ * @param wasmLoaderOptions A configuration object that provides the location
237
+ * of the Wasm binary and its loader.
238
+ * @param objectDetectorOptions The options for the Object Detector. Note that
239
+ * either a path to the model asset or a model buffer needs to be
240
+ * provided (via `baseOptions`).
241
+ */
242
+ static createFromOptions(wasmLoaderOptions: WasmLoaderOptions, objectDetectorOptions: ObjectDetectorOptions): Promise<ObjectDetector>;
243
+ /**
244
+ * Initializes the Wasm runtime and creates a new object detector based on the
245
+ * provided model asset buffer.
246
+ * @param wasmLoaderOptions A configuration object that provides the location
247
+ * of the Wasm binary and its loader.
248
+ * @param modelAssetBuffer A binary representation of the model.
249
+ */
250
+ static createFromModelBuffer(wasmLoaderOptions: WasmLoaderOptions, modelAssetBuffer: Uint8Array): Promise<ObjectDetector>;
251
+ /**
252
+ * Initializes the Wasm runtime and creates a new object detector based on the
253
+ * path to the model asset.
254
+ * @param wasmLoaderOptions A configuration object that provides the location
255
+ * of the Wasm binary and its loader.
256
+ * @param modelAssetPath The path to the model asset.
257
+ */
258
+ static createFromModelPath(wasmLoaderOptions: WasmLoaderOptions, modelAssetPath: string): Promise<ObjectDetector>;
259
+ /**
260
+ * Sets new options for the object detector.
261
+ *
262
+ * Calling `setOptions()` with a subset of options only affects those options.
263
+ * You can reset an option back to its default value by explicitly setting it
264
+ * to `undefined`.
265
+ *
266
+ * @param options The options for the object detector.
267
+ */
268
+ setOptions(options: ObjectDetectorOptions): Promise<void>;
269
+ /**
270
+ * Performs object detection on the provided single image and waits
271
+ * synchronously for the response.
272
+ * @param imageSource An image source to process.
273
+ * @param timestamp The timestamp of the current frame, in ms. If not
274
+ * provided, defaults to `performance.now()`.
275
+ * @return The list of detected objects
276
+ */
277
+ detect(imageSource: ImageSource, timestamp?: number): Detection[];
278
+ /** Converts raw data into a Detection, and adds it to our detection list. */
279
+ private addJsObjectDetections;
280
+ /** Updates the MediaPipe graph configuration. */
281
+ private refreshGraph;
282
+ }
283
+
284
+ /** Options to configure the MediaPipe Object Detector Task */
285
+ export declare interface ObjectDetectorOptions {
286
+ /** Options to configure the loading of the model assets. */
287
+ baseOptions?: BaseOptions;
288
+ /**
289
+ * The locale to use for display names specified through the TFLite Model
290
+ * Metadata, if any. Defaults to English.
291
+ */
292
+ displayNamesLocale?: string | undefined;
293
+ /** The maximum number of top-scored detection results to return. */
294
+ maxResults?: number | undefined;
295
+ /**
296
+ * Overrides the value provided in the model metadata. Results below this
297
+ * value are rejected.
298
+ */
299
+ scoreThreshold?: number | undefined;
300
+ /**
301
+ * Allowlist of category names. If non-empty, detection results whose category
302
+ * name is not in this set will be filtered out. Duplicate or unknown category
303
+ * names are ignored. Mutually exclusive with `categoryDenylist`.
304
+ */
305
+ categoryAllowlist?: string[] | undefined;
306
+ /**
307
+ * Denylist of category names. If non-empty, detection results whose category
308
+ * name is in this set will be filtered out. Duplicate or unknown category
309
+ * names are ignored. Mutually exclusive with `categoryAllowlist`.
310
+ */
311
+ categoryDenylist?: string[] | undefined;
312
+ }
313
+
314
+ /** Base class for all MediaPipe Tasks. */
315
+ declare abstract class TaskRunner extends WasmMediaPipeImageLib {
316
+ private processingErrors;
317
+ constructor(wasmModule: WasmModule);
318
+ /**
319
+ * Takes the raw data from a MediaPipe graph, and passes it to C++ to be run
320
+ * over the video stream. Will replace the previously running MediaPipe graph,
321
+ * if there is one.
322
+ * @param graphData The raw MediaPipe graph data, either in binary
323
+ * protobuffer format (.binarypb), or else in raw text format (.pbtxt or
324
+ * .textproto).
325
+ * @param isBinary This should be set to true if the graph is in
326
+ * binary format, and false if it is in human-readable text format.
327
+ */
328
+ setGraph(graphData: Uint8Array, isBinary: boolean): void;
329
+ /**
330
+ * Forces all queued-up packets to be pushed through the MediaPipe graph as
331
+ * far as possible, performing all processing until no more processing can be
332
+ * done.
333
+ */
334
+ finishProcessing(): void;
335
+ /** Throws the error from the error listener if an error was raised. */
336
+ private handleErrors;
337
+ }
338
+
339
+ declare const WasmMediaPipeImageLib: (new (...args: any[]) => {
340
+ registerModelResourcesGraphService(): void;
341
+ readonly wasmModule: WasmModule;
342
+ readonly hasMultiStreamSupport: boolean;
343
+ autoResizeCanvas: boolean;
344
+ audioPtr: number | null;
345
+ audioSize: number;
346
+ initializeGraph(graphFile: string): Promise<void>;
347
+ setGraphFromString(graphConfig: string): void;
348
+ setGraph(graphData: Uint8Array, isBinary: boolean): void;
349
+ configureAudio(numChannels: number, numSamples: number, sampleRate: number): void;
350
+ setAutoResizeCanvas(resize: boolean): void;
351
+ setAutoRenderToScreen(enabled: boolean): void;
352
+ bindTextureToStream(imageSource: ImageSource, streamNamePtr?: number | undefined): [number, number];
353
+ processGl(imageSource: ImageSource, timestamp: number): WebGLTexture | undefined;
354
+ wrapStringPtr(stringData: string, stringPtrFunc: (ptr: number) => void): void;
355
+ wrapStringPtrPtr(stringData: string[], ptrFunc: (ptr: number) => void): void;
356
+ setListener<T>(outputStreamName: string, callbackFcn: (data: T) => void): void;
357
+ setVectorListener<T_1>(outputStreamName: string, callbackFcn: (data: T_1[]) => void): void;
358
+ attachErrorListener(callbackFcn: (code: number, message: string) => void): void;
359
+ addAudioToStream(audioData: Float32Array, timestamp: number): void;
360
+ addGpuBufferToStream(imageSource: ImageSource, streamName: string, timestamp: number): void;
361
+ addBoolToStream(data: boolean, streamName: string, timestamp: number): void;
362
+ addDoubleToStream(data: number, streamName: string, timestamp: number): void;
363
+ addFloatToStream(data: number, streamName: string, timestamp: number): void;
364
+ addIntToStream(data: number, streamName: string, timestamp: number): void;
365
+ addStringToStream(data: string, streamName: string, timestamp: number): void;
366
+ addStringRecordToStream(data: Record<string, string>, streamName: string, timestamp: number): void;
367
+ addProtoToStream(data: Uint8Array, protoType: string, streamName: string, timestamp: number): void;
368
+ addBoolToInputSidePacket(data: boolean, sidePacketName: string): void;
369
+ addDoubleToInputSidePacket(data: number, sidePacketName: string): void;
370
+ addFloatToInputSidePacket(data: number, sidePacketName: string): void;
371
+ addIntToInputSidePacket(data: number, sidePacketName: string): void;
372
+ addStringToInputSidePacket(data: string, sidePacketName: string): void;
373
+ addProtoToInputSidePacket(data: Uint8Array, protoType: string, sidePacketName: string): void;
374
+ attachBoolListener(outputStreamName: string, callbackFcn: (data: boolean) => void): void;
375
+ attachBoolVectorListener(outputStreamName: string, callbackFcn: (data: boolean[]) => void): void;
376
+ attachIntListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
377
+ attachIntVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
378
+ attachDoubleListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
379
+ attachDoubleVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
380
+ attachFloatListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
381
+ attachFloatVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
382
+ attachStringListener(outputStreamName: string, callbackFcn: (data: string) => void): void;
383
+ attachStringVectorListener(outputStreamName: string, callbackFcn: (data: string[]) => void): void;
384
+ attachProtoListener(outputStreamName: string, callbackFcn: (data: Uint8Array) => void, makeDeepCopy?: boolean | undefined): void;
385
+ attachProtoVectorListener(outputStreamName: string, callbackFcn: (data: Uint8Array[]) => void, makeDeepCopy?: boolean | undefined): void;
386
+ setOnAudioOutput(audioOutputListener: AudioOutputListener): void;
387
+ finishProcessing(): void;
388
+ }) & (new (...args: any[]) => {
389
+ addGpuBufferAsImageToStream(imageSource: ImageSource, streamName: string, timestamp: number): void;
390
+ readonly wasmModule: WasmModule;
391
+ readonly hasMultiStreamSupport: boolean;
392
+ autoResizeCanvas: boolean;
393
+ audioPtr: number | null;
394
+ audioSize: number;
395
+ initializeGraph(graphFile: string): Promise<void>;
396
+ setGraphFromString(graphConfig: string): void;
397
+ setGraph(graphData: Uint8Array, isBinary: boolean): void;
398
+ configureAudio(numChannels: number, numSamples: number, sampleRate: number): void;
399
+ setAutoResizeCanvas(resize: boolean): void; /**
400
+ * Forces all queued-up packets to be pushed through the MediaPipe graph as
401
+ * far as possible, performing all processing until no more processing can be
402
+ * done.
403
+ */
404
+ setAutoRenderToScreen(enabled: boolean): void;
405
+ bindTextureToStream(imageSource: ImageSource, streamNamePtr?: number | undefined): [number, number];
406
+ processGl(imageSource: ImageSource, timestamp: number): WebGLTexture | undefined;
407
+ wrapStringPtr(stringData: string, stringPtrFunc: (ptr: number) => void): void;
408
+ wrapStringPtrPtr(stringData: string[], ptrFunc: (ptr: number) => void): void;
409
+ setListener<T_2>(outputStreamName: string, callbackFcn: (data: T_2) => void): void;
410
+ setVectorListener<T_1_1>(outputStreamName: string, callbackFcn: (data: T_1_1[]) => void): void;
411
+ attachErrorListener(callbackFcn: (code: number, message: string) => void): void;
412
+ addAudioToStream(audioData: Float32Array, timestamp: number): void;
413
+ addGpuBufferToStream(imageSource: ImageSource, streamName: string, timestamp: number): void;
414
+ addBoolToStream(data: boolean, streamName: string, timestamp: number): void;
415
+ addDoubleToStream(data: number, streamName: string, timestamp: number): void;
416
+ addFloatToStream(data: number, streamName: string, timestamp: number): void;
417
+ addIntToStream(data: number, streamName: string, timestamp: number): void;
418
+ addStringToStream(data: string, streamName: string, timestamp: number): void;
419
+ addStringRecordToStream(data: Record<string, string>, streamName: string, timestamp: number): void;
420
+ addProtoToStream(data: Uint8Array, protoType: string, streamName: string, timestamp: number): void;
421
+ addBoolToInputSidePacket(data: boolean, sidePacketName: string): void;
422
+ addDoubleToInputSidePacket(data: number, sidePacketName: string): void;
423
+ addFloatToInputSidePacket(data: number, sidePacketName: string): void;
424
+ addIntToInputSidePacket(data: number, sidePacketName: string): void;
425
+ addStringToInputSidePacket(data: string, sidePacketName: string): void;
426
+ addProtoToInputSidePacket(data: Uint8Array, protoType: string, sidePacketName: string): void;
427
+ attachBoolListener(outputStreamName: string, callbackFcn: (data: boolean) => void): void;
428
+ attachBoolVectorListener(outputStreamName: string, callbackFcn: (data: boolean[]) => void): void;
429
+ attachIntListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
430
+ attachIntVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
431
+ attachDoubleListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
432
+ attachDoubleVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
433
+ attachFloatListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
434
+ attachFloatVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
435
+ attachStringListener(outputStreamName: string, callbackFcn: (data: string) => void): void;
436
+ attachStringVectorListener(outputStreamName: string, callbackFcn: (data: string[]) => void): void;
437
+ attachProtoListener(outputStreamName: string, callbackFcn: (data: Uint8Array) => void, makeDeepCopy?: boolean | undefined): void;
438
+ attachProtoVectorListener(outputStreamName: string, callbackFcn: (data: Uint8Array[]) => void, makeDeepCopy?: boolean | undefined): void;
439
+ setOnAudioOutput(audioOutputListener: AudioOutputListener): void;
440
+ finishProcessing(): void;
441
+ }) & typeof WasmMediaPipeLib;
442
+
443
+ /**
444
+ * Simple class to run an arbitrary image-in/image-out MediaPipe graph (i.e.
445
+ * as created by wasm_mediapipe_demo BUILD macro), and either render results
446
+ * into canvas, or else return the output WebGLTexture. Takes a WebAssembly
447
+ * Module (must be instantiated to self.Module).
448
+ */
449
+ declare class WasmMediaPipeLib {
450
+ readonly wasmModule: WasmModule;
451
+ readonly hasMultiStreamSupport: boolean;
452
+ autoResizeCanvas: boolean;
453
+ audioPtr: number | null;
454
+ audioSize: number;
455
+ /**
456
+ * Creates a new MediaPipe WASM module. Must be called *after* wasm Module has
457
+ * initialized. Note that we take control of the GL canvas from here on out,
458
+ * and will resize it to fit input.
459
+ *
460
+ * @param module The underlying Wasm Module to use.
461
+ * @param glCanvas The type of the GL canvas to use, or `null` if no GL
462
+ * canvas should be initialzed. Initializes an offscreen canvas if not
463
+ * provided.
464
+ */
465
+ constructor(module: WasmModule, glCanvas?: HTMLCanvasElement | OffscreenCanvas | null);
466
+ /**
467
+ * Convenience helper to load a MediaPipe graph from a file and pass it to
468
+ * setGraph.
469
+ * @param graphFile The url of the MediaPipe graph file to load.
470
+ */
471
+ initializeGraph(graphFile: string): Promise<void>;
472
+ /**
473
+ * Convenience helper for calling setGraph with a string representing a text
474
+ * proto config.
475
+ * @param graphConfig The text proto graph config, expected to be a string in
476
+ * default JavaScript UTF-16 format.
477
+ */
478
+ setGraphFromString(graphConfig: string): void;
479
+ /**
480
+ * Takes the raw data from a MediaPipe graph, and passes it to C++ to be run
481
+ * over the video stream. Will replace the previously running MediaPipe graph,
482
+ * if there is one.
483
+ * @param graphData The raw MediaPipe graph data, either in binary
484
+ * protobuffer format (.binarypb), or else in raw text format (.pbtxt or
485
+ * .textproto).
486
+ * @param isBinary This should be set to true if the graph is in
487
+ * binary format, and false if it is in human-readable text format.
488
+ */
489
+ setGraph(graphData: Uint8Array, isBinary: boolean): void;
490
+ /**
491
+ * Configures the current graph to handle audio in a certain way. Must be
492
+ * called before the graph is set/started in order to use processAudio.
493
+ * @param numChannels The number of channels of audio input. Only 1
494
+ * is supported for now.
495
+ * @param numSamples The number of samples that are taken in each
496
+ * audio capture.
497
+ * @param sampleRate The rate, in Hz, of the sampling.
498
+ */
499
+ configureAudio(numChannels: number, numSamples: number, sampleRate: number): void;
500
+ /**
501
+ * Allows disabling automatic canvas resizing, in case clients want to control
502
+ * control this.
503
+ * @param resize True will re-enable automatic canvas resizing, while false
504
+ * will disable the feature.
505
+ */
506
+ setAutoResizeCanvas(resize: boolean): void;
507
+ /**
508
+ * Allows disabling the automatic render-to-screen code, in case clients don't
509
+ * need/want this. In particular, this removes the requirement for pipelines
510
+ * to have access to GPU resources, as well as the requirement for graphs to
511
+ * have "input_frames_gpu" and "output_frames_gpu" streams defined, so pure
512
+ * CPU pipelines and non-video pipelines can be created.
513
+ * NOTE: This only affects future graph initializations (via setGraph or
514
+ * initializeGraph), and does NOT affect the currently running graph, so
515
+ * calls to this should be made *before* setGraph/initializeGraph for the
516
+ * graph file being targeted.
517
+ * @param enabled True will re-enable automatic render-to-screen code and
518
+ * cause GPU resources to once again be requested, while false will
519
+ * disable the feature.
520
+ */
521
+ setAutoRenderToScreen(enabled: boolean): void;
522
+ /**
523
+ * Bind texture to our internal canvas, and upload image source to GPU.
524
+ * Returns tuple [width, height] of texture. Intended for internal usage.
525
+ */
526
+ bindTextureToStream(imageSource: ImageSource, streamNamePtr?: number): [
527
+ number,
528
+ number
529
+ ];
530
+ /**
531
+ * Takes the raw data from a JS image source, and sends it to C++ to be
532
+ * processed, waiting synchronously for the response. Note that we will resize
533
+ * our GL canvas to fit the input, so input size should only change
534
+ * infrequently.
535
+ * @param imageSource An image source to process.
536
+ * @param timestamp The timestamp of the current frame, in ms.
537
+ * @return texture? The WebGL texture reference, if one was produced.
538
+ */
539
+ processGl(imageSource: ImageSource, timestamp: number): WebGLTexture | undefined;
540
+ /**
541
+ * Converts JavaScript string input parameters into C++ c-string pointers.
542
+ * See b/204830158 for more details. Intended for internal usage.
543
+ */
544
+ wrapStringPtr(stringData: string, stringPtrFunc: (ptr: number) => void): void;
545
+ /**
546
+ * Converts JavaScript string input parameters into C++ c-string pointers.
547
+ * See b/204830158 for more details.
548
+ */
549
+ wrapStringPtrPtr(stringData: string[], ptrFunc: (ptr: number) => void): void;
550
+ /**
551
+ * Ensures existence of the simple listeners table and registers the callback.
552
+ * Intended for internal usage.
553
+ */
554
+ setListener<T>(outputStreamName: string, callbackFcn: (data: T) => void): void;
555
+ /**
556
+ * Ensures existence of the vector listeners table and registers the callback.
557
+ * Intended for internal usage.
558
+ */
559
+ setVectorListener<T>(outputStreamName: string, callbackFcn: (data: T[]) => void): void;
560
+ /**
561
+ * Attaches a listener that will be invoked when the MediaPipe framework
562
+ * returns an error.
563
+ */
564
+ attachErrorListener(callbackFcn: (code: number, message: string) => void): void;
565
+ /**
566
+ * Takes the raw data from a JS audio capture array, and sends it to C++ to be
567
+ * processed.
568
+ * @param audioData An array of raw audio capture data, like
569
+ * from a call to getChannelData on an AudioBuffer.
570
+ * @param timestamp The timestamp of the current frame, in ms.
571
+ */
572
+ addAudioToStream(audioData: Float32Array, timestamp: number): void;
573
+ /**
574
+ * Takes the relevant information from the HTML video or image element, and
575
+ * passes it into the WebGL-based graph for processing on the given stream at
576
+ * the given timestamp. Can be used for additional auxiliary GpuBuffer input
577
+ * streams. Processing will not occur until a blocking call (like
578
+ * processVideoGl or finishProcessing) is made. For use with
579
+ * 'gl_graph_runner_internal_multi_input'.
580
+ * @param imageSource Reference to the video frame we wish to add into our
581
+ * graph.
582
+ * @param streamName The name of the MediaPipe graph stream to add the frame
583
+ * to.
584
+ * @param timestamp The timestamp of the input frame, in ms.
585
+ */
586
+ addGpuBufferToStream(imageSource: ImageSource, streamName: string, timestamp: number): void;
587
+ /**
588
+ * Sends a boolean packet into the specified stream at the given timestamp.
589
+ * @param data The boolean data to send.
590
+ * @param streamName The name of the graph input stream to send data into.
591
+ * @param timestamp The timestamp of the input data, in ms.
592
+ */
593
+ addBoolToStream(data: boolean, streamName: string, timestamp: number): void;
594
+ /**
595
+ * Sends a double packet into the specified stream at the given timestamp.
596
+ * @param data The double data to send.
597
+ * @param streamName The name of the graph input stream to send data into.
598
+ * @param timestamp The timestamp of the input data, in ms.
599
+ */
600
+ addDoubleToStream(data: number, streamName: string, timestamp: number): void;
601
+ /**
602
+ * Sends a float packet into the specified stream at the given timestamp.
603
+ * @param data The float data to send.
604
+ * @param streamName The name of the graph input stream to send data into.
605
+ * @param timestamp The timestamp of the input data, in ms.
606
+ */
607
+ addFloatToStream(data: number, streamName: string, timestamp: number): void;
608
+ /**
609
+ * Sends an integer packet into the specified stream at the given timestamp.
610
+ * @param data The integer data to send.
611
+ * @param streamName The name of the graph input stream to send data into.
612
+ * @param timestamp The timestamp of the input data, in ms.
613
+ */
614
+ addIntToStream(data: number, streamName: string, timestamp: number): void;
615
+ /**
616
+ * Sends a string packet into the specified stream at the given timestamp.
617
+ * @param data The string data to send.
618
+ * @param streamName The name of the graph input stream to send data into.
619
+ * @param timestamp The timestamp of the input data, in ms.
620
+ */
621
+ addStringToStream(data: string, streamName: string, timestamp: number): void;
622
+ /**
623
+ * Sends a Record<string, string> packet into the specified stream at the
624
+ * given timestamp.
625
+ * @param data The records to send (will become a
626
+ * std::flat_hash_map<std::string, std::string).
627
+ * @param streamName The name of the graph input stream to send data into.
628
+ * @param timestamp The timestamp of the input data, in ms.
629
+ */
630
+ addStringRecordToStream(data: Record<string, string>, streamName: string, timestamp: number): void;
631
+ /**
632
+ * Sends a serialized protobuffer packet into the specified stream at the
633
+ * given timestamp, to be parsed into the specified protobuffer type.
634
+ * @param data The binary (serialized) raw protobuffer data.
635
+ * @param protoType The C++ namespaced type this protobuffer data corresponds
636
+ * to. It will be converted to this type when output as a packet into the
637
+ * graph.
638
+ * @param streamName The name of the graph input stream to send data into.
639
+ * @param timestamp The timestamp of the input data, in ms.
640
+ */
641
+ addProtoToStream(data: Uint8Array, protoType: string, streamName: string, timestamp: number): void;
642
+ /**
643
+ * Attaches a boolean packet to the specified input_side_packet.
644
+ * @param data The boolean data to send.
645
+ * @param sidePacketName The name of the graph input side packet to send data
646
+ * into.
647
+ */
648
+ addBoolToInputSidePacket(data: boolean, sidePacketName: string): void;
649
+ /**
650
+ * Attaches a double packet to the specified input_side_packet.
651
+ * @param data The double data to send.
652
+ * @param sidePacketName The name of the graph input side packet to send data
653
+ * into.
654
+ */
655
+ addDoubleToInputSidePacket(data: number, sidePacketName: string): void;
656
+ /**
657
+ * Attaches a float packet to the specified input_side_packet.
658
+ * @param data The float data to send.
659
+ * @param sidePacketName The name of the graph input side packet to send data
660
+ * into.
661
+ */
662
+ addFloatToInputSidePacket(data: number, sidePacketName: string): void;
663
+ /**
664
+ * Attaches a integer packet to the specified input_side_packet.
665
+ * @param data The integer data to send.
666
+ * @param sidePacketName The name of the graph input side packet to send data
667
+ * into.
668
+ */
669
+ addIntToInputSidePacket(data: number, sidePacketName: string): void;
670
+ /**
671
+ * Attaches a string packet to the specified input_side_packet.
672
+ * @param data The string data to send.
673
+ * @param sidePacketName The name of the graph input side packet to send data
674
+ * into.
675
+ */
676
+ addStringToInputSidePacket(data: string, sidePacketName: string): void;
677
+ /**
678
+ * Attaches a serialized proto packet to the specified input_side_packet.
679
+ * @param data The binary (serialized) raw protobuffer data.
680
+ * @param protoType The C++ namespaced type this protobuffer data corresponds
681
+ * to. It will be converted to this type for use in the graph.
682
+ * @param sidePacketName The name of the graph input side packet to send data
683
+ * into.
684
+ */
685
+ addProtoToInputSidePacket(data: Uint8Array, protoType: string, sidePacketName: string): void;
686
+ /**
687
+ * Attaches a boolean packet listener to the specified output_stream.
688
+ * @param outputStreamName The name of the graph output stream to grab boolean
689
+ * data from.
690
+ * @param callbackFcn The function that will be called back with the data, as
691
+ * it is received. Note that the data is only guaranteed to exist for the
692
+ * duration of the callback, and the callback will be called inline, so it
693
+ * should not perform overly complicated (or any async) behavior.
694
+ */
695
+ attachBoolListener(outputStreamName: string, callbackFcn: (data: boolean) => void): void;
696
+ /**
697
+ * Attaches a bool[] packet listener to the specified output_stream.
698
+ * @param outputStreamName The name of the graph output stream to grab
699
+ * std::vector<bool> data from.
700
+ * @param callbackFcn The function that will be called back with the data, as
701
+ * it is received. Note that the data is only guaranteed to exist for the
702
+ * duration of the callback, and the callback will be called inline, so it
703
+ * should not perform overly complicated (or any async) behavior.
704
+ */
705
+ attachBoolVectorListener(outputStreamName: string, callbackFcn: (data: boolean[]) => void): void;
706
+ /**
707
+ * Attaches an int packet listener to the specified output_stream.
708
+ * @param outputStreamName The name of the graph output stream to grab int
709
+ * data from.
710
+ * @param callbackFcn The function that will be called back with the data, as
711
+ * it is received. Note that the data is only guaranteed to exist for the
712
+ * duration of the callback, and the callback will be called inline, so it
713
+ * should not perform overly complicated (or any async) behavior.
714
+ */
715
+ attachIntListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
716
+ /**
717
+ * Attaches an int[] packet listener to the specified output_stream.
718
+ * @param outputStreamName The name of the graph output stream to grab
719
+ * std::vector<int> data from.
720
+ * @param callbackFcn The function that will be called back with the data, as
721
+ * it is received. Note that the data is only guaranteed to exist for the
722
+ * duration of the callback, and the callback will be called inline, so it
723
+ * should not perform overly complicated (or any async) behavior.
724
+ */
725
+ attachIntVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
726
+ /**
727
+ * Attaches a double packet listener to the specified output_stream.
728
+ * @param outputStreamName The name of the graph output stream to grab double
729
+ * data from.
730
+ * @param callbackFcn The function that will be called back with the data, as
731
+ * it is received. Note that the data is only guaranteed to exist for the
732
+ * duration of the callback, and the callback will be called inline, so it
733
+ * should not perform overly complicated (or any async) behavior.
734
+ */
735
+ attachDoubleListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
736
+ /**
737
+ * Attaches a double[] packet listener to the specified output_stream.
738
+ * @param outputStreamName The name of the graph output stream to grab
739
+ * std::vector<double> data from.
740
+ * @param callbackFcn The function that will be called back with the data, as
741
+ * it is received. Note that the data is only guaranteed to exist for the
742
+ * duration of the callback, and the callback will be called inline, so it
743
+ * should not perform overly complicated (or any async) behavior.
744
+ */
745
+ attachDoubleVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
746
+ /**
747
+ * Attaches a float packet listener to the specified output_stream.
748
+ * @param outputStreamName The name of the graph output stream to grab float
749
+ * data from.
750
+ * @param callbackFcn The function that will be called back with the data, as
751
+ * it is received. Note that the data is only guaranteed to exist for the
752
+ * duration of the callback, and the callback will be called inline, so it
753
+ * should not perform overly complicated (or any async) behavior.
754
+ */
755
+ attachFloatListener(outputStreamName: string, callbackFcn: (data: number) => void): void;
756
+ /**
757
+ * Attaches a float[] packet listener to the specified output_stream.
758
+ * @param outputStreamName The name of the graph output stream to grab
759
+ * std::vector<float> data from.
760
+ * @param callbackFcn The function that will be called back with the data, as
761
+ * it is received. Note that the data is only guaranteed to exist for the
762
+ * duration of the callback, and the callback will be called inline, so it
763
+ * should not perform overly complicated (or any async) behavior.
764
+ */
765
+ attachFloatVectorListener(outputStreamName: string, callbackFcn: (data: number[]) => void): void;
766
+ /**
767
+ * Attaches a string packet listener to the specified output_stream.
768
+ * @param outputStreamName The name of the graph output stream to grab string
769
+ * data from.
770
+ * @param callbackFcn The function that will be called back with the data, as
771
+ * it is received. Note that the data is only guaranteed to exist for the
772
+ * duration of the callback, and the callback will be called inline, so it
773
+ * should not perform overly complicated (or any async) behavior.
774
+ */
775
+ attachStringListener(outputStreamName: string, callbackFcn: (data: string) => void): void;
776
+ /**
777
+ * Attaches a string[] packet listener to the specified output_stream.
778
+ * @param outputStreamName The name of the graph output stream to grab
779
+ * std::vector<std::string> data from.
780
+ * @param callbackFcn The function that will be called back with the data, as
781
+ * it is received. Note that the data is only guaranteed to exist for the
782
+ * duration of the callback, and the callback will be called inline, so it
783
+ * should not perform overly complicated (or any async) behavior.
784
+ */
785
+ attachStringVectorListener(outputStreamName: string, callbackFcn: (data: string[]) => void): void;
786
+ /**
787
+ * Attaches a serialized proto packet listener to the specified output_stream.
788
+ * @param outputStreamName The name of the graph output stream to grab binary
789
+ * serialized proto data from (in Uint8Array format).
790
+ * @param callbackFcn The function that will be called back with the data, as
791
+ * it is received. Note that by default the data is only guaranteed to
792
+ * exist for the duration of the callback, and the callback will be called
793
+ * inline, so it should not perform overly complicated (or any async)
794
+ * behavior. If the proto data needs to be able to outlive the call, you
795
+ * may set the optional makeDeepCopy parameter to true, or can manually
796
+ * deep-copy the data yourself.
797
+ * @param makeDeepCopy Optional convenience parameter which, if set to true,
798
+ * will override the default memory management behavior and make a deep
799
+ * copy of the underlying data, rather than just returning a view into the
800
+ * C++-managed memory. At the cost of a data copy, this allows the
801
+ * returned data to outlive the callback lifetime (and it will be cleaned
802
+ * up automatically by JS garbage collection whenever the user is finished
803
+ * with it).
804
+ */
805
+ attachProtoListener(outputStreamName: string, callbackFcn: (data: Uint8Array) => void, makeDeepCopy?: boolean): void;
806
+ /**
807
+ * Attaches a listener for an array of serialized proto packets to the
808
+ * specified output_stream.
809
+ * @param outputStreamName The name of the graph output stream to grab a
810
+ * vector of binary serialized proto data from (in Uint8Array[] format).
811
+ * @param callbackFcn The function that will be called back with the data, as
812
+ * it is received. Note that by default the data is only guaranteed to
813
+ * exist for the duration of the callback, and the callback will be called
814
+ * inline, so it should not perform overly complicated (or any async)
815
+ * behavior. If the proto data needs to be able to outlive the call, you
816
+ * may set the optional makeDeepCopy parameter to true, or can manually
817
+ * deep-copy the data yourself.
818
+ * @param makeDeepCopy Optional convenience parameter which, if set to true,
819
+ * will override the default memory management behavior and make a deep
820
+ * copy of the underlying data, rather than just returning a view into the
821
+ * C++-managed memory. At the cost of a data copy, this allows the
822
+ * returned data to outlive the callback lifetime (and it will be cleaned
823
+ * up automatically by JS garbage collection whenever the user is finished
824
+ * with it).
825
+ */
826
+ attachProtoVectorListener(outputStreamName: string, callbackFcn: (data: Uint8Array[]) => void, makeDeepCopy?: boolean): void;
827
+ /**
828
+ * Sets a listener to be called back with audio output packet data, as a
829
+ * Float32Array, when graph has finished processing it.
830
+ * @param audioOutputListener The caller's listener function.
831
+ */
832
+ setOnAudioOutput(audioOutputListener: AudioOutputListener): void;
833
+ /**
834
+ * Forces all queued-up packets to be pushed through the MediaPipe graph as
835
+ * far as possible, performing all processing until no more processing can be
836
+ * done.
837
+ */
838
+ finishProcessing(): void;
839
+ }
840
+
841
+ /**
842
+ * Declarations for Emscripten's WebAssembly Module behavior, so TS compiler
843
+ * doesn't break our JS/C++ bridge.
844
+ */
845
+ declare interface WasmModule {
846
+ canvas: HTMLCanvasElement | OffscreenCanvas | null;
847
+ HEAPU8: Uint8Array;
848
+ HEAPU32: Uint32Array;
849
+ HEAPF32: Float32Array;
850
+ HEAPF64: Float64Array;
851
+ errorListener?: ErrorListener;
852
+ _bindTextureToCanvas: () => boolean;
853
+ _changeBinaryGraph: (size: number, dataPtr: number) => void;
854
+ _changeTextGraph: (size: number, dataPtr: number) => void;
855
+ _configureAudio: (channels: number, samples: number, sampleRate: number) => void;
856
+ _free: (ptr: number) => void;
857
+ _malloc: (size: number) => number;
858
+ _processAudio: (dataPtr: number, timestamp: number) => void;
859
+ _processFrame: (width: number, height: number, timestamp: number) => void;
860
+ _setAutoRenderToScreen: (enabled: boolean) => void;
861
+ _waitUntilIdle: () => void;
862
+ dataFileDownloads?: {
863
+ [url: string]: {
864
+ loaded: number;
865
+ total: number;
866
+ };
867
+ };
868
+ onAudioOutput?: AudioOutputListener;
869
+ stringToNewUTF8: (data: string) => number;
870
+ _bindTextureToStream: (streamNamePtr: number) => void;
871
+ _addBoundTextureToStream: (streamNamePtr: number, width: number, height: number, timestamp: number) => void;
872
+ _addBoolToInputStream: (data: boolean, streamNamePtr: number, timestamp: number) => void;
873
+ _addDoubleToInputStream: (data: number, streamNamePtr: number, timestamp: number) => void;
874
+ _addFloatToInputStream: (data: number, streamNamePtr: number, timestamp: number) => void;
875
+ _addIntToInputStream: (data: number, streamNamePtr: number, timestamp: number) => void;
876
+ _addStringToInputStream: (dataPtr: number, streamNamePtr: number, timestamp: number) => void;
877
+ _addFlatHashMapToInputStream: (keysPtr: number, valuesPtr: number, count: number, streamNamePtr: number, timestamp: number) => void;
878
+ _addProtoToInputStream: (dataPtr: number, dataSize: number, protoNamePtr: number, streamNamePtr: number, timestamp: number) => void;
879
+ _addBoolToInputSidePacket: (data: boolean, streamNamePtr: number) => void;
880
+ _addDoubleToInputSidePacket: (data: number, streamNamePtr: number) => void;
881
+ _addFloatToInputSidePacket: (data: number, streamNamePtr: number) => void;
882
+ _addIntToInputSidePacket: (data: number, streamNamePtr: number) => void;
883
+ _addStringToInputSidePacket: (dataPtr: number, streamNamePtr: number) => void;
884
+ _addProtoToInputSidePacket: (dataPtr: number, dataSize: number, protoNamePtr: number, streamNamePtr: number) => void;
885
+ simpleListeners?: {
886
+ [outputStreamName: string]: (data: unknown) => void;
887
+ };
888
+ vectorListeners?: {
889
+ [outputStreamName: string]: (data: unknown, index: number, length: number) => void;
890
+ };
891
+ _attachBoolListener: (streamNamePtr: number) => void;
892
+ _attachBoolVectorListener: (streamNamePtr: number) => void;
893
+ _attachDoubleListener: (streamNamePtr: number) => void;
894
+ _attachDoubleVectorListener: (streamNamePtr: number) => void;
895
+ _attachFloatListener: (streamNamePtr: number) => void;
896
+ _attachFloatVectorListener: (streamNamePtr: number) => void;
897
+ _attachIntListener: (streamNamePtr: number) => void;
898
+ _attachIntVectorListener: (streamNamePtr: number) => void;
899
+ _attachStringListener: (streamNamePtr: number) => void;
900
+ _attachStringVectorListener: (streamNamePtr: number) => void;
901
+ _attachProtoListener: (streamNamePtr: number, makeDeepCopy?: boolean) => void;
902
+ _attachProtoVectorListener: (streamNamePtr: number, makeDeepCopy?: boolean) => void;
903
+ _attachAudioOutputListener: () => void;
904
+ _processGl: (frameDataPtr: number) => number;
905
+ }
906
+
907
+ export { }