@100mslive/hms-virtual-background 1.3.4 → 1.3.6
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/dist/HMSVirtualBackgroundPlugin.d.ts +17 -5
- package/dist/hms-virtual-background.cjs.development.js +233 -79
- package/dist/hms-virtual-background.cjs.development.js.map +1 -1
- package/dist/hms-virtual-background.cjs.production.min.js +1 -1
- package/dist/hms-virtual-background.cjs.production.min.js.map +1 -1
- package/dist/hms-virtual-background.esm.js +233 -79
- package/dist/hms-virtual-background.esm.js.map +1 -1
- package/package.json +13 -8
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import '@tensorflow/tfjs-backend-webgl';
|
|
2
|
-
import { HMSVideoPlugin, HMSVideoPluginType } from
|
|
2
|
+
import { HMSVideoPlugin, HMSVideoPluginType } from '@100mslive/hms-video';
|
|
3
3
|
export declare class HMSVirtualBackgroundPlugin implements HMSVideoPlugin {
|
|
4
4
|
background: string | HTMLImageElement;
|
|
5
5
|
personMaskWidth: number;
|
|
6
6
|
personMaskHeight: number;
|
|
7
7
|
isVirtualBackground: boolean;
|
|
8
|
-
backgroundImage: HTMLImageElement |
|
|
8
|
+
backgroundImage: HTMLImageElement | null;
|
|
9
|
+
backgroundVideo: HTMLVideoElement | null;
|
|
10
|
+
backgroundType: string;
|
|
9
11
|
loadModelCalled: boolean;
|
|
10
12
|
blurValue: any;
|
|
11
13
|
tfLite: any;
|
|
@@ -22,24 +24,34 @@ export declare class HMSVirtualBackgroundPlugin implements HMSVideoPlugin {
|
|
|
22
24
|
personMaskCtx: any;
|
|
23
25
|
filters: any;
|
|
24
26
|
enableSharpening?: boolean | false;
|
|
27
|
+
gifFrames: any;
|
|
28
|
+
gifFramesIndex: number;
|
|
29
|
+
gifFrameImageData: any;
|
|
30
|
+
tempGifCanvas: HTMLCanvasElement;
|
|
31
|
+
tempGifContext: any;
|
|
32
|
+
giflocalCount: number;
|
|
25
33
|
constructor(background: string, enableSharpening?: boolean);
|
|
26
34
|
init(): Promise<void>;
|
|
27
35
|
isSupported(): boolean;
|
|
28
36
|
getName(): string;
|
|
29
37
|
getPluginType(): HMSVideoPluginType;
|
|
30
|
-
setBackground(path?: string | HTMLImageElement): Promise<void>;
|
|
38
|
+
setBackground(path?: string | HTMLImageElement | HTMLVideoElement): Promise<void>;
|
|
31
39
|
stop(): void;
|
|
32
40
|
processVideoFrame(input: HTMLCanvasElement, output: HTMLCanvasElement, skipProcessing?: boolean): Promise<void> | void;
|
|
33
41
|
private setImage;
|
|
42
|
+
private setGiF;
|
|
34
43
|
private log;
|
|
35
44
|
private resizeInputData;
|
|
36
45
|
private infer;
|
|
37
46
|
private postProcessing;
|
|
38
|
-
sharpenFilter
|
|
47
|
+
private sharpenFilter;
|
|
39
48
|
private drawPersonMask;
|
|
40
49
|
private drawSegmentedBackground;
|
|
41
50
|
private runSegmentation;
|
|
51
|
+
private fitVideoToBackground;
|
|
42
52
|
private fitImageToBackground;
|
|
53
|
+
private fitGifToBackground;
|
|
54
|
+
private fitData;
|
|
43
55
|
private addBlurToBackground;
|
|
44
|
-
initSharpenFilter
|
|
56
|
+
private initSharpenFilter;
|
|
45
57
|
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
require('@tensorflow/tfjs-backend-webgl');
|
|
6
|
+
var gifuctJs = require('gifuct-js');
|
|
6
7
|
var hmsVideo = require('@100mslive/hms-video');
|
|
7
8
|
|
|
8
9
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
@@ -917,8 +918,9 @@ var loadTFLite = /*#__PURE__*/function () {
|
|
|
917
918
|
}();
|
|
918
919
|
|
|
919
920
|
var TAG$1 = 'VBProcessor';
|
|
921
|
+
var DEFAULT_DELAY = 33;
|
|
920
922
|
|
|
921
|
-
var pkg$1 = /*#__PURE__*/require(
|
|
923
|
+
var pkg$1 = /*#__PURE__*/require('../package.json');
|
|
922
924
|
|
|
923
925
|
var minVideoWidthForSharpening = 214;
|
|
924
926
|
var maxVideoWidthForSharpening = 855;
|
|
@@ -930,7 +932,11 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
930
932
|
enableSharpening = false;
|
|
931
933
|
}
|
|
932
934
|
|
|
935
|
+
this.backgroundType = 'none';
|
|
933
936
|
this.background = background;
|
|
937
|
+
this.enableSharpening = enableSharpening;
|
|
938
|
+
this.backgroundImage = null;
|
|
939
|
+
this.backgroundVideo = null;
|
|
934
940
|
this.personMaskWidth = 256;
|
|
935
941
|
this.personMaskHeight = 144;
|
|
936
942
|
this.isVirtualBackground = false;
|
|
@@ -950,8 +956,14 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
950
956
|
this.personMaskCanvas.height = this.personMaskHeight;
|
|
951
957
|
this.personMaskCtx = this.personMaskCanvas.getContext('2d');
|
|
952
958
|
this.filters = {};
|
|
959
|
+
this.gifFrames = null;
|
|
960
|
+
this.gifFramesIndex = 0;
|
|
961
|
+
this.gifFrameImageData = null;
|
|
962
|
+
this.tempGifCanvas = document.createElement('canvas');
|
|
963
|
+
this.tempGifContext = this.tempGifCanvas.getContext('2d');
|
|
964
|
+
this.giflocalCount = 0;
|
|
953
965
|
this.enableSharpening = enableSharpening;
|
|
954
|
-
this.log(TAG$1,
|
|
966
|
+
this.log(TAG$1, 'Virtual Background plugin created');
|
|
955
967
|
this.setBackground(this.background);
|
|
956
968
|
}
|
|
957
969
|
|
|
@@ -968,7 +980,7 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
968
980
|
break;
|
|
969
981
|
}
|
|
970
982
|
|
|
971
|
-
this.log(TAG$1,
|
|
983
|
+
this.log(TAG$1, 'PREVIOUS LOADED MODEL IS ', this.tfLite);
|
|
972
984
|
this.loadModelCalled = true;
|
|
973
985
|
this.tfLitePromise = loadTFLite();
|
|
974
986
|
_context.next = 6;
|
|
@@ -1005,7 +1017,7 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1005
1017
|
|
|
1006
1018
|
_proto.isSupported = function isSupported() {
|
|
1007
1019
|
//support chrome, firefox, edge TODO: check this
|
|
1008
|
-
return navigator.userAgent.indexOf(
|
|
1020
|
+
return navigator.userAgent.indexOf('Chrome') !== -1 || navigator.userAgent.indexOf('Firefox') !== -1 || navigator.userAgent.indexOf('Edg') !== -1;
|
|
1009
1021
|
};
|
|
1010
1022
|
|
|
1011
1023
|
_proto.getName = function getName() {
|
|
@@ -1017,71 +1029,145 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1017
1029
|
};
|
|
1018
1030
|
|
|
1019
1031
|
_proto.setBackground = /*#__PURE__*/function () {
|
|
1020
|
-
var _setBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1032
|
+
var _setBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(path) {
|
|
1033
|
+
var _this = this;
|
|
1034
|
+
|
|
1021
1035
|
var img;
|
|
1022
|
-
return runtime_1.wrap(function
|
|
1036
|
+
return runtime_1.wrap(function _callee3$(_context3) {
|
|
1023
1037
|
while (1) {
|
|
1024
|
-
switch (
|
|
1038
|
+
switch (_context3.prev = _context3.next) {
|
|
1025
1039
|
case 0:
|
|
1026
|
-
if (!(path !==
|
|
1027
|
-
|
|
1040
|
+
if (!(path !== '')) {
|
|
1041
|
+
_context3.next = 50;
|
|
1028
1042
|
break;
|
|
1029
1043
|
}
|
|
1030
1044
|
|
|
1031
|
-
if (!(path ===
|
|
1032
|
-
|
|
1045
|
+
if (!(path === 'none')) {
|
|
1046
|
+
_context3.next = 8;
|
|
1033
1047
|
break;
|
|
1034
1048
|
}
|
|
1035
1049
|
|
|
1036
1050
|
this.log(TAG$1, 'setting background to :', path);
|
|
1037
|
-
this.background =
|
|
1051
|
+
this.background = 'none';
|
|
1052
|
+
this.backgroundType = 'none';
|
|
1038
1053
|
this.isVirtualBackground = false;
|
|
1039
|
-
|
|
1054
|
+
_context3.next = 48;
|
|
1040
1055
|
break;
|
|
1041
1056
|
|
|
1042
|
-
case
|
|
1043
|
-
if (!(path ===
|
|
1044
|
-
|
|
1057
|
+
case 8:
|
|
1058
|
+
if (!(path === 'blur')) {
|
|
1059
|
+
_context3.next = 15;
|
|
1045
1060
|
break;
|
|
1046
1061
|
}
|
|
1047
1062
|
|
|
1048
1063
|
this.log(TAG$1, 'setting background to :', path);
|
|
1049
|
-
this.background =
|
|
1064
|
+
this.background = 'blur';
|
|
1065
|
+
this.backgroundType = 'blur';
|
|
1050
1066
|
this.isVirtualBackground = false;
|
|
1051
|
-
|
|
1067
|
+
_context3.next = 48;
|
|
1052
1068
|
break;
|
|
1053
1069
|
|
|
1054
|
-
case
|
|
1055
|
-
|
|
1070
|
+
case 15:
|
|
1071
|
+
if (!(path instanceof HTMLImageElement)) {
|
|
1072
|
+
_context3.next = 29;
|
|
1073
|
+
break;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
this.log('setting background to image', path);
|
|
1077
|
+
_context3.next = 19;
|
|
1056
1078
|
return this.setImage(path);
|
|
1057
1079
|
|
|
1058
|
-
case
|
|
1059
|
-
img =
|
|
1080
|
+
case 19:
|
|
1081
|
+
img = _context3.sent;
|
|
1060
1082
|
|
|
1061
1083
|
if (!(!img || !img.complete || !img.naturalHeight)) {
|
|
1062
|
-
|
|
1084
|
+
_context3.next = 24;
|
|
1063
1085
|
break;
|
|
1064
1086
|
}
|
|
1065
1087
|
|
|
1066
1088
|
throw new Error('Invalid image. Provide a valid and successfully loaded HTMLImageElement');
|
|
1067
1089
|
|
|
1068
|
-
case
|
|
1090
|
+
case 24:
|
|
1069
1091
|
this.isVirtualBackground = true;
|
|
1070
1092
|
this.backgroundImage = img;
|
|
1093
|
+
this.backgroundType = 'image';
|
|
1071
1094
|
|
|
1072
|
-
case
|
|
1073
|
-
|
|
1095
|
+
case 27:
|
|
1096
|
+
_context3.next = 48;
|
|
1074
1097
|
break;
|
|
1075
1098
|
|
|
1076
|
-
case
|
|
1077
|
-
|
|
1099
|
+
case 29:
|
|
1100
|
+
if (!(path instanceof HTMLVideoElement)) {
|
|
1101
|
+
_context3.next = 38;
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
this.log('setting background to video', path);
|
|
1106
|
+
this.backgroundVideo = path;
|
|
1107
|
+
this.backgroundVideo.crossOrigin = 'anonymous';
|
|
1108
|
+
this.backgroundVideo.muted = true;
|
|
1109
|
+
this.backgroundVideo.loop = true;
|
|
1110
|
+
this.backgroundVideo.oncanplaythrough = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
|
|
1111
|
+
return runtime_1.wrap(function _callee2$(_context2) {
|
|
1112
|
+
while (1) {
|
|
1113
|
+
switch (_context2.prev = _context2.next) {
|
|
1114
|
+
case 0:
|
|
1115
|
+
if (!(_this.backgroundVideo != null)) {
|
|
1116
|
+
_context2.next = 5;
|
|
1117
|
+
break;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
_context2.next = 3;
|
|
1121
|
+
return _this.backgroundVideo.play();
|
|
1122
|
+
|
|
1123
|
+
case 3:
|
|
1124
|
+
_this.isVirtualBackground = true;
|
|
1125
|
+
_this.backgroundType = 'video';
|
|
1126
|
+
|
|
1127
|
+
case 5:
|
|
1128
|
+
case "end":
|
|
1129
|
+
return _context2.stop();
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
}, _callee2);
|
|
1133
|
+
}));
|
|
1134
|
+
_context3.next = 48;
|
|
1135
|
+
break;
|
|
1136
|
+
|
|
1137
|
+
case 38:
|
|
1138
|
+
console.log('setting gif to background');
|
|
1139
|
+
_context3.next = 41;
|
|
1140
|
+
return this.setGiF(path);
|
|
1078
1141
|
|
|
1079
|
-
case
|
|
1142
|
+
case 41:
|
|
1143
|
+
this.gifFrames = _context3.sent;
|
|
1144
|
+
|
|
1145
|
+
if (!(this.gifFrames != null && this.gifFrames.length > 0)) {
|
|
1146
|
+
_context3.next = 47;
|
|
1147
|
+
break;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
this.backgroundType = 'gif';
|
|
1151
|
+
this.isVirtualBackground = true;
|
|
1152
|
+
_context3.next = 48;
|
|
1153
|
+
break;
|
|
1154
|
+
|
|
1155
|
+
case 47:
|
|
1156
|
+
throw new Error('Invalid background supplied, see the docs to check supported background type');
|
|
1157
|
+
|
|
1158
|
+
case 48:
|
|
1159
|
+
_context3.next = 51;
|
|
1160
|
+
break;
|
|
1161
|
+
|
|
1162
|
+
case 50:
|
|
1163
|
+
throw new Error('Invalid background supplied, see the docs to check supported background type');
|
|
1164
|
+
|
|
1165
|
+
case 51:
|
|
1080
1166
|
case "end":
|
|
1081
|
-
return
|
|
1167
|
+
return _context3.stop();
|
|
1082
1168
|
}
|
|
1083
1169
|
}
|
|
1084
|
-
},
|
|
1170
|
+
}, _callee3, this);
|
|
1085
1171
|
}));
|
|
1086
1172
|
|
|
1087
1173
|
function setBackground(_x) {
|
|
@@ -1093,19 +1179,31 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1093
1179
|
|
|
1094
1180
|
_proto.stop = function stop() {
|
|
1095
1181
|
if (this.isVirtualBackground) {
|
|
1096
|
-
var _this$backgroundImage;
|
|
1182
|
+
var _this$backgroundImage, _this$backgroundVideo;
|
|
1097
1183
|
|
|
1098
1184
|
(_this$backgroundImage = this.backgroundImage) == null ? void 0 : _this$backgroundImage.removeAttribute('src');
|
|
1185
|
+
(_this$backgroundVideo = this.backgroundVideo) == null ? void 0 : _this$backgroundVideo.removeAttribute('src');
|
|
1186
|
+
|
|
1187
|
+
if (this.backgroundType === 'video') {
|
|
1188
|
+
this.backgroundVideo.loop = false;
|
|
1189
|
+
this.backgroundVideo = null;
|
|
1190
|
+
}
|
|
1099
1191
|
}
|
|
1100
1192
|
|
|
1101
1193
|
if (this.outputCtx) {
|
|
1102
1194
|
this.outputCtx.fillStyle = "rgb(0, 0, 0)";
|
|
1103
1195
|
this.outputCtx.fillRect(0, 0, this.output.width, this.output.height);
|
|
1104
|
-
}
|
|
1196
|
+
} //gif related
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
this.gifFrameImageData = null;
|
|
1200
|
+
this.gifFrames = null;
|
|
1201
|
+
this.giflocalCount = 0;
|
|
1202
|
+
this.gifFramesIndex = 0;
|
|
1105
1203
|
};
|
|
1106
1204
|
|
|
1107
1205
|
_proto.processVideoFrame = function processVideoFrame(input, output, skipProcessing) {
|
|
1108
|
-
var
|
|
1206
|
+
var _this2 = this;
|
|
1109
1207
|
|
|
1110
1208
|
if (!input || !output) {
|
|
1111
1209
|
throw new Error('Plugin invalid input/output');
|
|
@@ -1123,36 +1221,41 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1123
1221
|
ctx.canvas.height = input.height;
|
|
1124
1222
|
}
|
|
1125
1223
|
|
|
1224
|
+
if (this.backgroundType === 'video') {
|
|
1225
|
+
this.backgroundVideo.width = input.width;
|
|
1226
|
+
this.backgroundVideo.height = input.height;
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1126
1229
|
this.outputCtx = ctx;
|
|
1127
1230
|
this.imageAspectRatio = input.width / input.height;
|
|
1128
1231
|
|
|
1129
1232
|
if (this.imageAspectRatio <= 0) {
|
|
1130
|
-
throw new Error(
|
|
1233
|
+
throw new Error('Invalid input width/height');
|
|
1131
1234
|
}
|
|
1132
1235
|
|
|
1133
1236
|
var process = /*#__PURE__*/function () {
|
|
1134
|
-
var
|
|
1135
|
-
return runtime_1.wrap(function
|
|
1237
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4() {
|
|
1238
|
+
return runtime_1.wrap(function _callee4$(_context4) {
|
|
1136
1239
|
while (1) {
|
|
1137
|
-
switch (
|
|
1240
|
+
switch (_context4.prev = _context4.next) {
|
|
1138
1241
|
case 0:
|
|
1139
|
-
|
|
1140
|
-
return
|
|
1242
|
+
_context4.next = 2;
|
|
1243
|
+
return _this2.runSegmentation(skipProcessing);
|
|
1141
1244
|
|
|
1142
1245
|
case 2:
|
|
1143
1246
|
case "end":
|
|
1144
|
-
return
|
|
1247
|
+
return _context4.stop();
|
|
1145
1248
|
}
|
|
1146
1249
|
}
|
|
1147
|
-
},
|
|
1250
|
+
}, _callee4);
|
|
1148
1251
|
}));
|
|
1149
1252
|
|
|
1150
1253
|
return function process() {
|
|
1151
|
-
return
|
|
1254
|
+
return _ref2.apply(this, arguments);
|
|
1152
1255
|
};
|
|
1153
1256
|
}();
|
|
1154
1257
|
|
|
1155
|
-
if (this.background ===
|
|
1258
|
+
if (this.background === 'none' && !this.isVirtualBackground) {
|
|
1156
1259
|
this.outputCtx.globalCompositeOperation = 'copy';
|
|
1157
1260
|
this.outputCtx.filter = 'none';
|
|
1158
1261
|
this.outputCtx.drawImage(input, 0, 0, input.width, input.height);
|
|
@@ -1162,13 +1265,13 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1162
1265
|
};
|
|
1163
1266
|
|
|
1164
1267
|
_proto.setImage = /*#__PURE__*/function () {
|
|
1165
|
-
var _setImage = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1166
|
-
return runtime_1.wrap(function
|
|
1268
|
+
var _setImage = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(image) {
|
|
1269
|
+
return runtime_1.wrap(function _callee5$(_context5) {
|
|
1167
1270
|
while (1) {
|
|
1168
|
-
switch (
|
|
1271
|
+
switch (_context5.prev = _context5.next) {
|
|
1169
1272
|
case 0:
|
|
1170
1273
|
image.crossOrigin = 'anonymous';
|
|
1171
|
-
return
|
|
1274
|
+
return _context5.abrupt("return", new Promise(function (resolve, reject) {
|
|
1172
1275
|
image.onload = function () {
|
|
1173
1276
|
return resolve(image);
|
|
1174
1277
|
};
|
|
@@ -1178,10 +1281,10 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1178
1281
|
|
|
1179
1282
|
case 2:
|
|
1180
1283
|
case "end":
|
|
1181
|
-
return
|
|
1284
|
+
return _context5.stop();
|
|
1182
1285
|
}
|
|
1183
1286
|
}
|
|
1184
|
-
},
|
|
1287
|
+
}, _callee5);
|
|
1185
1288
|
}));
|
|
1186
1289
|
|
|
1187
1290
|
function setImage(_x2) {
|
|
@@ -1191,6 +1294,16 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1191
1294
|
return setImage;
|
|
1192
1295
|
}();
|
|
1193
1296
|
|
|
1297
|
+
_proto.setGiF = function setGiF(url) {
|
|
1298
|
+
return fetch(url).then(function (resp) {
|
|
1299
|
+
return resp.arrayBuffer();
|
|
1300
|
+
}).then(function (buff) {
|
|
1301
|
+
return gifuctJs.parseGIF(buff);
|
|
1302
|
+
}).then(function (gif) {
|
|
1303
|
+
return gifuctJs.decompressFrames(gif, true);
|
|
1304
|
+
});
|
|
1305
|
+
};
|
|
1306
|
+
|
|
1194
1307
|
_proto.log = function log(tag) {
|
|
1195
1308
|
var _console;
|
|
1196
1309
|
|
|
@@ -1203,7 +1316,8 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1203
1316
|
|
|
1204
1317
|
_proto.resizeInputData = function resizeInputData() {
|
|
1205
1318
|
this.personMaskCtx.drawImage(this.input, 0, 0, this.input.width, this.input.height, 0, 0, this.personMaskWidth, this.personMaskHeight);
|
|
1206
|
-
var imageData = this.personMaskCtx.getImageData(0, 0, this.personMaskWidth, this.personMaskHeight);
|
|
1319
|
+
var imageData = this.personMaskCtx.getImageData(0, 0, this.personMaskWidth, this.personMaskHeight); //
|
|
1320
|
+
|
|
1207
1321
|
var inputMemoryOffset = this.tfLite._getInputMemoryOffset() / 4;
|
|
1208
1322
|
|
|
1209
1323
|
for (var i = 0; i < this.personMaskPixelCount; i++) {
|
|
@@ -1289,26 +1403,44 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1289
1403
|
this.outputCtx.imageSmoothingQuality = 'high';
|
|
1290
1404
|
|
|
1291
1405
|
if (this.isVirtualBackground) {
|
|
1292
|
-
this.
|
|
1406
|
+
if (this.backgroundType === 'video' && this.backgroundVideo != null && this.backgroundVideo.readyState >= 4) {
|
|
1407
|
+
this.fitVideoToBackground();
|
|
1408
|
+
} else if (this.backgroundType === 'image') {
|
|
1409
|
+
this.fitImageToBackground();
|
|
1410
|
+
} else if (this.backgroundType === 'gif') {
|
|
1411
|
+
if (this.giflocalCount > this.gifFrames[this.gifFramesIndex].delay / DEFAULT_DELAY) {
|
|
1412
|
+
this.gifFramesIndex++;
|
|
1413
|
+
|
|
1414
|
+
if (this.gifFramesIndex >= this.gifFrames.length) {
|
|
1415
|
+
this.gifFramesIndex = 0;
|
|
1416
|
+
}
|
|
1417
|
+
|
|
1418
|
+
this.giflocalCount = 0;
|
|
1419
|
+
} else {
|
|
1420
|
+
this.giflocalCount++;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
this.fitGifToBackground();
|
|
1424
|
+
}
|
|
1293
1425
|
} else {
|
|
1294
1426
|
this.addBlurToBackground();
|
|
1295
1427
|
}
|
|
1296
1428
|
};
|
|
1297
1429
|
|
|
1298
1430
|
_proto.runSegmentation = /*#__PURE__*/function () {
|
|
1299
|
-
var _runSegmentation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1300
|
-
return runtime_1.wrap(function
|
|
1431
|
+
var _runSegmentation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee6(skipProcessing) {
|
|
1432
|
+
return runtime_1.wrap(function _callee6$(_context6) {
|
|
1301
1433
|
while (1) {
|
|
1302
|
-
switch (
|
|
1434
|
+
switch (_context6.prev = _context6.next) {
|
|
1303
1435
|
case 0:
|
|
1304
1436
|
if (!this.tfLite) {
|
|
1305
|
-
|
|
1437
|
+
_context6.next = 5;
|
|
1306
1438
|
break;
|
|
1307
1439
|
}
|
|
1308
1440
|
|
|
1309
1441
|
// const start = performance.now();
|
|
1310
1442
|
this.resizeInputData();
|
|
1311
|
-
|
|
1443
|
+
_context6.next = 4;
|
|
1312
1444
|
return this.infer(skipProcessing);
|
|
1313
1445
|
|
|
1314
1446
|
case 4:
|
|
@@ -1317,10 +1449,10 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1317
1449
|
|
|
1318
1450
|
case 5:
|
|
1319
1451
|
case "end":
|
|
1320
|
-
return
|
|
1452
|
+
return _context6.stop();
|
|
1321
1453
|
}
|
|
1322
1454
|
}
|
|
1323
|
-
},
|
|
1455
|
+
}, _callee6, this);
|
|
1324
1456
|
}));
|
|
1325
1457
|
|
|
1326
1458
|
function runSegmentation(_x3) {
|
|
@@ -1330,30 +1462,52 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1330
1462
|
return runSegmentation;
|
|
1331
1463
|
}();
|
|
1332
1464
|
|
|
1465
|
+
_proto.fitVideoToBackground = function fitVideoToBackground() {
|
|
1466
|
+
this.fitData(this.backgroundVideo, this.backgroundVideo.width, this.backgroundVideo.height);
|
|
1467
|
+
};
|
|
1468
|
+
|
|
1333
1469
|
_proto.fitImageToBackground = function fitImageToBackground() {
|
|
1334
|
-
|
|
1470
|
+
this.fitData(this.backgroundImage, this.backgroundImage.width, this.backgroundImage.height);
|
|
1471
|
+
};
|
|
1472
|
+
|
|
1473
|
+
_proto.fitGifToBackground = function fitGifToBackground() {
|
|
1474
|
+
if (this.gifFrameImageData == null) {
|
|
1475
|
+
var dims = this.gifFrames[this.gifFramesIndex].dims;
|
|
1476
|
+
this.tempGifCanvas.width = dims.width;
|
|
1477
|
+
this.tempGifCanvas.height = dims.height;
|
|
1478
|
+
this.gifFrameImageData = this.tempGifContext.createImageData(dims.width, dims.height);
|
|
1479
|
+
} // set the patch data as an override
|
|
1480
|
+
|
|
1335
1481
|
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1482
|
+
this.gifFrameImageData.data.set(this.gifFrames[this.gifFramesIndex].patch);
|
|
1483
|
+
this.tempGifContext.putImageData(this.gifFrameImageData, 0, 0);
|
|
1484
|
+
this.fitData(this.tempGifCanvas, this.gifFrameImageData.width, this.gifFrameImageData.height);
|
|
1485
|
+
};
|
|
1486
|
+
|
|
1487
|
+
_proto.fitData = function fitData(data, dataWidth, dataHeight) {
|
|
1488
|
+
var inputWidth, inputHeight, xoffset, yoffset;
|
|
1489
|
+
|
|
1490
|
+
if (dataWidth / dataHeight < this.imageAspectRatio) {
|
|
1491
|
+
inputWidth = dataWidth;
|
|
1492
|
+
inputHeight = dataWidth / this.imageAspectRatio;
|
|
1493
|
+
xoffset = 0;
|
|
1494
|
+
yoffset = (dataHeight - inputHeight) / 2;
|
|
1341
1495
|
} else {
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1496
|
+
inputHeight = dataHeight;
|
|
1497
|
+
inputWidth = dataHeight * this.imageAspectRatio;
|
|
1498
|
+
yoffset = 0;
|
|
1499
|
+
xoffset = (dataWidth - inputWidth) / 2;
|
|
1346
1500
|
}
|
|
1347
1501
|
|
|
1348
|
-
this.outputCtx.drawImage(
|
|
1502
|
+
this.outputCtx.drawImage(data, xoffset, yoffset, inputWidth, inputHeight, 0, 0, this.output.width, this.output.height);
|
|
1349
1503
|
};
|
|
1350
1504
|
|
|
1351
1505
|
_proto.addBlurToBackground = /*#__PURE__*/function () {
|
|
1352
|
-
var _addBlurToBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1506
|
+
var _addBlurToBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7() {
|
|
1353
1507
|
var blurValue;
|
|
1354
|
-
return runtime_1.wrap(function
|
|
1508
|
+
return runtime_1.wrap(function _callee7$(_context7) {
|
|
1355
1509
|
while (1) {
|
|
1356
|
-
switch (
|
|
1510
|
+
switch (_context7.prev = _context7.next) {
|
|
1357
1511
|
case 0:
|
|
1358
1512
|
blurValue = '15px';
|
|
1359
1513
|
if (this.input.width <= 160) blurValue = '5px';else if (this.input.width <= 320) blurValue = '10px';else if (this.input.width <= 640) blurValue = '15px';else if (this.input.width <= 960) blurValue = '20px';else if (this.input.width <= 1280) blurValue = '25px';else if (this.input.width <= 1920) blurValue = '30px';
|
|
@@ -1362,10 +1516,10 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1362
1516
|
|
|
1363
1517
|
case 4:
|
|
1364
1518
|
case "end":
|
|
1365
|
-
return
|
|
1519
|
+
return _context7.stop();
|
|
1366
1520
|
}
|
|
1367
1521
|
}
|
|
1368
|
-
},
|
|
1522
|
+
}, _callee7, this);
|
|
1369
1523
|
}));
|
|
1370
1524
|
|
|
1371
1525
|
function addBlurToBackground() {
|
|
@@ -1376,13 +1530,13 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1376
1530
|
}();
|
|
1377
1531
|
|
|
1378
1532
|
_proto.initSharpenFilter = function initSharpenFilter() {
|
|
1379
|
-
var
|
|
1533
|
+
var _this3 = this;
|
|
1380
1534
|
|
|
1381
1535
|
this.filters.tmpCanvas = document.createElement('canvas');
|
|
1382
1536
|
this.filters.tmpCtx = this.filters.tmpCanvas.getContext('2d');
|
|
1383
1537
|
|
|
1384
1538
|
this.filters.createImageData = function (w, h) {
|
|
1385
|
-
return
|
|
1539
|
+
return _this3.filters.tmpCtx.createImageData(w, h);
|
|
1386
1540
|
};
|
|
1387
1541
|
|
|
1388
1542
|
this.filters.convolute = function (pixels, weights, opaque) {
|
|
@@ -1399,7 +1553,7 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1399
1553
|
var w = sw;
|
|
1400
1554
|
var h = sh;
|
|
1401
1555
|
|
|
1402
|
-
var output =
|
|
1556
|
+
var output = _this3.filters.createImageData(w, h);
|
|
1403
1557
|
|
|
1404
1558
|
var dst = output.data; // go through the destination image pixels
|
|
1405
1559
|
|
|
@@ -1409,7 +1563,7 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1409
1563
|
for (var x = 0; x < w; x = x + 1) {
|
|
1410
1564
|
var dstOff = (y * w + x) * 4;
|
|
1411
1565
|
|
|
1412
|
-
if (src[dstOff + 3]
|
|
1566
|
+
if (src[dstOff + 3] === 0) {
|
|
1413
1567
|
continue;
|
|
1414
1568
|
} else if (x < w && y < h) {
|
|
1415
1569
|
var sy = y;
|