@stream-io/video-filters-web 0.7.0 → 0.7.2
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/CHANGELOG.md +12 -0
- package/dist/index.cjs.js +18 -30
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +18 -30
- package/dist/index.es.js.map +1 -1
- package/dist/src/BaseVideoProcessor.d.ts +1 -1
- package/dist/src/VirtualBackground.d.ts +0 -1
- package/dist/src/types.d.ts +4 -4
- package/mediapipe/models/selfie_multiclass_256x256.tflite +0 -0
- package/mediapipe/models/selfie_segmenter_landscape.tflite +0 -0
- package/package.json +1 -1
- package/src/BaseVideoProcessor.ts +1 -4
- package/src/VirtualBackground.ts +13 -14
- package/src/mediapipe.ts +1 -1
- package/src/types.ts +7 -16
|
@@ -29,7 +29,7 @@ export declare abstract class BaseVideoProcessor {
|
|
|
29
29
|
protected constructor(track: MediaStreamVideoTrack, hooks?: VideoTrackProcessorHooks);
|
|
30
30
|
start(): Promise<MediaStreamTrack>;
|
|
31
31
|
stop(): void;
|
|
32
|
-
|
|
32
|
+
protected updateStats(delay: number): void;
|
|
33
33
|
protected abstract initialize(): Promise<void>;
|
|
34
34
|
protected abstract transform(frame: VideoFrame): Promise<VideoFrame>;
|
|
35
35
|
protected onFlush(): void;
|
|
@@ -14,7 +14,6 @@ export declare class VirtualBackground extends BaseVideoProcessor {
|
|
|
14
14
|
private latestCategoryMask;
|
|
15
15
|
private latestConfidenceMask;
|
|
16
16
|
private lastFrameTime;
|
|
17
|
-
private count;
|
|
18
17
|
constructor(track: MediaStreamVideoTrack, options?: BackgroundOptions, hooks?: VideoTrackProcessorHooks);
|
|
19
18
|
protected initialize(): Promise<void>;
|
|
20
19
|
private initializeSegmenter;
|
package/dist/src/types.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export interface VideoTrackProcessorHooks {
|
|
|
39
39
|
onError?: (error: unknown) => void;
|
|
40
40
|
onStats?: (stats: PerformanceStats) => void;
|
|
41
41
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Maps blur level to blur strength values.
|
|
44
|
+
*/
|
|
45
|
+
export declare const BACKGROUND_BLUR_MAP: Record<'low' | 'medium' | 'high', number>;
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -64,11 +64,8 @@ export abstract class BaseVideoProcessor {
|
|
|
64
64
|
this.canvas.height = frame.displayHeight;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
const start = performance.now();
|
|
68
67
|
const processed = await this.transform(frame);
|
|
69
|
-
const delay = performance.now() - start;
|
|
70
68
|
|
|
71
|
-
this.updateStats(delay);
|
|
72
69
|
controller.enqueue(processed);
|
|
73
70
|
} catch (e) {
|
|
74
71
|
this.hooks.onError?.(e);
|
|
@@ -98,7 +95,7 @@ export abstract class BaseVideoProcessor {
|
|
|
98
95
|
this.onStop();
|
|
99
96
|
}
|
|
100
97
|
|
|
101
|
-
|
|
98
|
+
protected updateStats(delay: number): void {
|
|
102
99
|
this.frames++;
|
|
103
100
|
this.delayTotal += delay;
|
|
104
101
|
|
package/src/VirtualBackground.ts
CHANGED
|
@@ -24,7 +24,6 @@ export class VirtualBackground extends BaseVideoProcessor {
|
|
|
24
24
|
private latestCategoryMask: WebGLTexture | undefined = undefined;
|
|
25
25
|
private latestConfidenceMask: WebGLTexture | undefined = undefined;
|
|
26
26
|
private lastFrameTime = -1;
|
|
27
|
-
private count = 0;
|
|
28
27
|
|
|
29
28
|
constructor(
|
|
30
29
|
track: MediaStreamVideoTrack,
|
|
@@ -49,7 +48,8 @@ export class VirtualBackground extends BaseVideoProcessor {
|
|
|
49
48
|
`https://unpkg.com/${packageName}@${version}/mediapipe`;
|
|
50
49
|
|
|
51
50
|
const model =
|
|
52
|
-
this.options.modelPath ||
|
|
51
|
+
this.options.modelPath ||
|
|
52
|
+
`${basePath}/models/selfie_segmenter_landscape.tflite`;
|
|
53
53
|
|
|
54
54
|
const fileset = await FilesetResolver.forVisionTasks(`${basePath}/wasm`);
|
|
55
55
|
|
|
@@ -70,10 +70,12 @@ export class VirtualBackground extends BaseVideoProcessor {
|
|
|
70
70
|
|
|
71
71
|
protected async transform(frame: VideoFrame): Promise<VideoFrame> {
|
|
72
72
|
const currentTime = frame.timestamp;
|
|
73
|
-
const hasNewFrame = currentTime
|
|
73
|
+
const hasNewFrame = currentTime - this.lastFrameTime > 1_000;
|
|
74
74
|
this.lastFrameTime = currentTime;
|
|
75
75
|
|
|
76
76
|
if (hasNewFrame && this.isSegmenterReady && this.segmenter) {
|
|
77
|
+
const start = performance.now();
|
|
78
|
+
|
|
77
79
|
await this.runSegmentation(frame);
|
|
78
80
|
|
|
79
81
|
this.webGlRenderer.render(
|
|
@@ -82,6 +84,8 @@ export class VirtualBackground extends BaseVideoProcessor {
|
|
|
82
84
|
this.latestCategoryMask,
|
|
83
85
|
this.latestConfidenceMask,
|
|
84
86
|
);
|
|
87
|
+
|
|
88
|
+
this.updateStats(performance.now() - start);
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
return new VideoFrame(this.canvas, { timestamp: frame.timestamp });
|
|
@@ -124,20 +128,15 @@ export class VirtualBackground extends BaseVideoProcessor {
|
|
|
124
128
|
}
|
|
125
129
|
|
|
126
130
|
const blurLevel = this.options.backgroundBlurLevel;
|
|
131
|
+
const strength =
|
|
132
|
+
typeof blurLevel === 'string'
|
|
133
|
+
? BACKGROUND_BLUR_MAP[blurLevel]
|
|
134
|
+
: Math.round(blurLevel ?? 5);
|
|
127
135
|
|
|
128
|
-
if (typeof blurLevel === 'string') {
|
|
129
|
-
return {
|
|
130
|
-
...BACKGROUND_BLUR_MAP[blurLevel],
|
|
131
|
-
backgroundSource: undefined,
|
|
132
|
-
isSelfieMode,
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const numeric = blurLevel ?? 5;
|
|
137
136
|
return {
|
|
138
137
|
backgroundSource: undefined,
|
|
139
|
-
bgBlur: Math.min(
|
|
140
|
-
bgBlurRadius: Math.min(
|
|
138
|
+
bgBlur: Math.min(strength * 1.5, 20),
|
|
139
|
+
bgBlurRadius: Math.min(strength, 10),
|
|
141
140
|
isSelfieMode,
|
|
142
141
|
};
|
|
143
142
|
}
|
package/src/mediapipe.ts
CHANGED
|
@@ -10,7 +10,7 @@ export const loadMediaPipe = async (
|
|
|
10
10
|
): Promise<ArrayBuffer> => {
|
|
11
11
|
const {
|
|
12
12
|
basePath = `https://unpkg.com/${packageName}@${version}/mediapipe`,
|
|
13
|
-
modelPath = `${basePath}/models/
|
|
13
|
+
modelPath = `${basePath}/models/selfie_segmenter_landscape.tflite`,
|
|
14
14
|
} = options;
|
|
15
15
|
|
|
16
16
|
const model =
|
package/src/types.ts
CHANGED
|
@@ -43,20 +43,11 @@ export interface VideoTrackProcessorHooks {
|
|
|
43
43
|
onStats?: (stats: PerformanceStats) => void;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
> = {
|
|
50
|
-
low:
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
},
|
|
54
|
-
medium: {
|
|
55
|
-
bgBlur: 15,
|
|
56
|
-
bgBlurRadius: 4,
|
|
57
|
-
},
|
|
58
|
-
high: {
|
|
59
|
-
bgBlur: 30,
|
|
60
|
-
bgBlurRadius: 5,
|
|
61
|
-
},
|
|
46
|
+
/**
|
|
47
|
+
* Maps blur level to blur strength values.
|
|
48
|
+
*/
|
|
49
|
+
export const BACKGROUND_BLUR_MAP: Record<'low' | 'medium' | 'high', number> = {
|
|
50
|
+
low: 3,
|
|
51
|
+
medium: 5,
|
|
52
|
+
high: 7,
|
|
62
53
|
};
|