@100mslive/hms-virtual-background 1.3.1-6e → 1.3.3-alpha
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 +11 -6
- package/dist/hms-virtual-background.cjs.development.js +272 -189
- 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 +270 -189
- package/dist/hms-virtual-background.esm.js.map +1 -1
- package/package.json +2 -4
|
@@ -914,11 +914,19 @@ var loadTFLite = /*#__PURE__*/function () {
|
|
|
914
914
|
|
|
915
915
|
var TAG$1 = 'VBProcessor';
|
|
916
916
|
|
|
917
|
-
var pkg$1 = /*#__PURE__*/require(
|
|
917
|
+
var pkg$1 = /*#__PURE__*/require("../package.json");
|
|
918
918
|
|
|
919
919
|
var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
920
|
-
function HMSVirtualBackgroundPlugin(background) {
|
|
920
|
+
function HMSVirtualBackgroundPlugin(background, enableSharpening) {
|
|
921
|
+
if (enableSharpening === void 0) {
|
|
922
|
+
enableSharpening = false;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
this.backgroundType = "none";
|
|
921
926
|
this.background = background;
|
|
927
|
+
this.enableSharpening = enableSharpening;
|
|
928
|
+
this.backgroundImage = null;
|
|
929
|
+
this.backgroundVideo = null;
|
|
922
930
|
this.personMaskWidth = 256;
|
|
923
931
|
this.personMaskHeight = 144;
|
|
924
932
|
this.isVirtualBackground = false;
|
|
@@ -937,8 +945,8 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
937
945
|
this.personMaskCanvas.width = this.personMaskWidth;
|
|
938
946
|
this.personMaskCanvas.height = this.personMaskHeight;
|
|
939
947
|
this.personMaskCtx = this.personMaskCanvas.getContext('2d');
|
|
940
|
-
this.
|
|
941
|
-
this.log(TAG$1,
|
|
948
|
+
this.filters = {};
|
|
949
|
+
this.log(TAG$1, "Virtual Background plugin created");
|
|
942
950
|
this.setBackground(this.background);
|
|
943
951
|
}
|
|
944
952
|
|
|
@@ -955,7 +963,7 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
955
963
|
break;
|
|
956
964
|
}
|
|
957
965
|
|
|
958
|
-
this.log(TAG$1,
|
|
966
|
+
this.log(TAG$1, "PREVIOUS LOADED MODEL IS ", this.tfLite);
|
|
959
967
|
this.loadModelCalled = true;
|
|
960
968
|
this.tfLitePromise = loadTFLite();
|
|
961
969
|
_context.next = 6;
|
|
@@ -971,7 +979,9 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
971
979
|
return this.tfLitePromise;
|
|
972
980
|
|
|
973
981
|
case 11:
|
|
974
|
-
this.
|
|
982
|
+
if (this.enableSharpening) {
|
|
983
|
+
this.initSharpenFilter();
|
|
984
|
+
}
|
|
975
985
|
|
|
976
986
|
case 12:
|
|
977
987
|
case "end":
|
|
@@ -990,7 +1000,7 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
990
1000
|
|
|
991
1001
|
_proto.isSupported = function isSupported() {
|
|
992
1002
|
//support chrome, firefox, edge TODO: check this
|
|
993
|
-
return navigator.userAgent.indexOf(
|
|
1003
|
+
return navigator.userAgent.indexOf("Chrome") != -1 || navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Edg") != -1;
|
|
994
1004
|
};
|
|
995
1005
|
|
|
996
1006
|
_proto.getName = function getName() {
|
|
@@ -1001,166 +1011,122 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1001
1011
|
return HMSVideoPluginType.TRANSFORM;
|
|
1002
1012
|
};
|
|
1003
1013
|
|
|
1004
|
-
_proto.initSharpenFilter = function initSharpenFilter() {
|
|
1005
|
-
var _arguments = arguments,
|
|
1006
|
-
_this = this;
|
|
1007
|
-
|
|
1008
|
-
this.Filters.filterImage = function (filter, image, var_args) {
|
|
1009
|
-
var args = [image];
|
|
1010
|
-
|
|
1011
|
-
for (var i = 2; i < _arguments.length; i++) {
|
|
1012
|
-
args.push(_arguments[i]);
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
console.log(args);
|
|
1016
|
-
return filter.apply(null, args);
|
|
1017
|
-
};
|
|
1018
|
-
|
|
1019
|
-
this.Filters.tmpCanvas = document.createElement('canvas');
|
|
1020
|
-
this.Filters.tmpCtx = this.Filters.tmpCanvas.getContext('2d');
|
|
1021
|
-
|
|
1022
|
-
this.Filters.createImageData = function (w, h) {
|
|
1023
|
-
return _this.Filters.tmpCtx.createImageData(w, h);
|
|
1024
|
-
};
|
|
1025
|
-
|
|
1026
|
-
this.Filters.convolute = function (pixels, weights, opaque) {
|
|
1027
|
-
if (weights === void 0) {
|
|
1028
|
-
weights = [0, -1, 0, -1, 5, -1, 0, -1, 0];
|
|
1029
|
-
}
|
|
1030
|
-
|
|
1031
|
-
var side = Math.round(Math.sqrt(weights.length));
|
|
1032
|
-
var halfSide = Math.floor(side / 2);
|
|
1033
|
-
var src = pixels.data;
|
|
1034
|
-
var sw = pixels.width;
|
|
1035
|
-
var sh = pixels.height; // pad output by the convolution matrix
|
|
1036
|
-
|
|
1037
|
-
var w = sw;
|
|
1038
|
-
var h = sh; // console.log(this.Filters);
|
|
1039
|
-
|
|
1040
|
-
var output = _this.Filters.createImageData(w, h);
|
|
1041
|
-
|
|
1042
|
-
var dst = output.data; // go through the destination image pixels
|
|
1043
|
-
|
|
1044
|
-
var alphaFac = opaque ? 1 : 0;
|
|
1045
|
-
|
|
1046
|
-
for (var y = 0; y < h; y = y + 1) {
|
|
1047
|
-
for (var x = 0; x < w; x = x + 1) {
|
|
1048
|
-
// if (src[y * w + x] == 0) {
|
|
1049
|
-
// var dstOff = (y * w + x) * 4;
|
|
1050
|
-
// dst[dstOff] = src[dstOff];
|
|
1051
|
-
// dst[dstOff + 1] = src[dstOff + 1];
|
|
1052
|
-
// dst[dstOff + 2] = src[dstOff + 2];
|
|
1053
|
-
// dst[dstOff + 3] = src[dstOff + 3]; //a + alphaFac * (255 - a);
|
|
1054
|
-
// continue;
|
|
1055
|
-
// } else
|
|
1056
|
-
if (x < w && y < h) {
|
|
1057
|
-
var sy = y;
|
|
1058
|
-
var sx = x;
|
|
1059
|
-
var dstOff = (y * w + x) * 4; // calculate the weighed sum of the source image pixels that
|
|
1060
|
-
// fall under the convolution matrix
|
|
1061
|
-
|
|
1062
|
-
var r = 0,
|
|
1063
|
-
g = 0,
|
|
1064
|
-
b = 0,
|
|
1065
|
-
a = 0;
|
|
1066
|
-
|
|
1067
|
-
for (var cy = 0; cy < side; cy++) {
|
|
1068
|
-
for (var cx = 0; cx < side; cx++) {
|
|
1069
|
-
var scy = sy + cy - halfSide;
|
|
1070
|
-
var scx = sx + cx - halfSide;
|
|
1071
|
-
|
|
1072
|
-
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
|
|
1073
|
-
var srcOff = (scy * sw + scx) * 4;
|
|
1074
|
-
var wt = weights[cy * side + cx];
|
|
1075
|
-
r += src[srcOff] * wt;
|
|
1076
|
-
g += src[srcOff + 1] * wt;
|
|
1077
|
-
b += src[srcOff + 2] * wt;
|
|
1078
|
-
a += src[srcOff + 3] * wt;
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
}
|
|
1082
|
-
|
|
1083
|
-
dst[dstOff] = r;
|
|
1084
|
-
dst[dstOff + 1] = g;
|
|
1085
|
-
dst[dstOff + 2] = b;
|
|
1086
|
-
dst[dstOff + 3] = a + alphaFac * (255 - a);
|
|
1087
|
-
}
|
|
1088
|
-
}
|
|
1089
|
-
} // var idata = this.Filters.createImageData(w, h);
|
|
1090
|
-
// // set our buffer as source
|
|
1091
|
-
// idata.data.set(dst);
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
return output;
|
|
1095
|
-
};
|
|
1096
|
-
};
|
|
1097
|
-
|
|
1098
1014
|
_proto.setBackground = /*#__PURE__*/function () {
|
|
1099
|
-
var _setBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1015
|
+
var _setBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee3(path) {
|
|
1016
|
+
var _this = this;
|
|
1017
|
+
|
|
1100
1018
|
var img;
|
|
1101
|
-
return runtime_1.wrap(function
|
|
1019
|
+
return runtime_1.wrap(function _callee3$(_context3) {
|
|
1102
1020
|
while (1) {
|
|
1103
|
-
switch (
|
|
1021
|
+
switch (_context3.prev = _context3.next) {
|
|
1104
1022
|
case 0:
|
|
1105
|
-
if (!(path !==
|
|
1106
|
-
|
|
1023
|
+
if (!(path !== "")) {
|
|
1024
|
+
_context3.next = 43;
|
|
1107
1025
|
break;
|
|
1108
1026
|
}
|
|
1109
1027
|
|
|
1110
|
-
if (!(path ===
|
|
1111
|
-
|
|
1028
|
+
if (!(path === "none")) {
|
|
1029
|
+
_context3.next = 8;
|
|
1112
1030
|
break;
|
|
1113
1031
|
}
|
|
1114
1032
|
|
|
1115
1033
|
this.log(TAG$1, 'setting background to :', path);
|
|
1116
|
-
this.background =
|
|
1034
|
+
this.background = "none";
|
|
1035
|
+
this.backgroundType = "none";
|
|
1117
1036
|
this.isVirtualBackground = false;
|
|
1118
|
-
|
|
1037
|
+
_context3.next = 41;
|
|
1119
1038
|
break;
|
|
1120
1039
|
|
|
1121
|
-
case
|
|
1122
|
-
if (!(path ===
|
|
1123
|
-
|
|
1040
|
+
case 8:
|
|
1041
|
+
if (!(path === "blur")) {
|
|
1042
|
+
_context3.next = 15;
|
|
1124
1043
|
break;
|
|
1125
1044
|
}
|
|
1126
1045
|
|
|
1127
1046
|
this.log(TAG$1, 'setting background to :', path);
|
|
1128
|
-
this.background =
|
|
1047
|
+
this.background = "blur";
|
|
1048
|
+
this.backgroundType = "blur"; //TODO: check this
|
|
1049
|
+
|
|
1129
1050
|
this.isVirtualBackground = false;
|
|
1130
|
-
|
|
1051
|
+
_context3.next = 41;
|
|
1131
1052
|
break;
|
|
1132
1053
|
|
|
1133
|
-
case
|
|
1134
|
-
|
|
1054
|
+
case 15:
|
|
1055
|
+
if (!(path instanceof HTMLImageElement)) {
|
|
1056
|
+
_context3.next = 29;
|
|
1057
|
+
break;
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
this.log("setting background to image", path);
|
|
1061
|
+
_context3.next = 19;
|
|
1135
1062
|
return this.setImage(path);
|
|
1136
1063
|
|
|
1137
|
-
case
|
|
1138
|
-
img =
|
|
1064
|
+
case 19:
|
|
1065
|
+
img = _context3.sent;
|
|
1139
1066
|
|
|
1140
1067
|
if (!(!img || !img.complete || !img.naturalHeight)) {
|
|
1141
|
-
|
|
1068
|
+
_context3.next = 24;
|
|
1142
1069
|
break;
|
|
1143
1070
|
}
|
|
1144
1071
|
|
|
1145
1072
|
throw new Error('Invalid image. Provide a valid and successfully loaded HTMLImageElement');
|
|
1146
1073
|
|
|
1147
|
-
case
|
|
1074
|
+
case 24:
|
|
1148
1075
|
this.isVirtualBackground = true;
|
|
1149
1076
|
this.backgroundImage = img;
|
|
1077
|
+
this.backgroundType = "image";
|
|
1150
1078
|
|
|
1151
|
-
case
|
|
1152
|
-
|
|
1079
|
+
case 27:
|
|
1080
|
+
_context3.next = 41;
|
|
1153
1081
|
break;
|
|
1154
1082
|
|
|
1155
|
-
case
|
|
1156
|
-
|
|
1083
|
+
case 29:
|
|
1084
|
+
if (!(path instanceof HTMLVideoElement)) {
|
|
1085
|
+
_context3.next = 40;
|
|
1086
|
+
break;
|
|
1087
|
+
}
|
|
1157
1088
|
|
|
1158
|
-
|
|
1089
|
+
this.log("setting background to video", path);
|
|
1090
|
+
this.isVirtualBackground = true;
|
|
1091
|
+
this.backgroundType = "video";
|
|
1092
|
+
this.backgroundVideo = path;
|
|
1093
|
+
this.backgroundVideo.crossOrigin = 'anonymous';
|
|
1094
|
+
this.backgroundVideo.muted = true;
|
|
1095
|
+
this.backgroundVideo.loop = true;
|
|
1096
|
+
this.backgroundVideo.oncanplaythrough = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee2() {
|
|
1097
|
+
return runtime_1.wrap(function _callee2$(_context2) {
|
|
1098
|
+
while (1) {
|
|
1099
|
+
switch (_context2.prev = _context2.next) {
|
|
1100
|
+
case 0:
|
|
1101
|
+
_context2.next = 2;
|
|
1102
|
+
return _this.backgroundVideo.play();
|
|
1103
|
+
|
|
1104
|
+
case 2:
|
|
1105
|
+
case "end":
|
|
1106
|
+
return _context2.stop();
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}, _callee2);
|
|
1110
|
+
}));
|
|
1111
|
+
_context3.next = 41;
|
|
1112
|
+
break;
|
|
1113
|
+
|
|
1114
|
+
case 40:
|
|
1115
|
+
throw new Error("Invalid background supplied, see the docs to check supported background type");
|
|
1116
|
+
|
|
1117
|
+
case 41:
|
|
1118
|
+
_context3.next = 44;
|
|
1119
|
+
break;
|
|
1120
|
+
|
|
1121
|
+
case 43:
|
|
1122
|
+
throw new Error("Invalid background supplied, see the docs to check supported background type");
|
|
1123
|
+
|
|
1124
|
+
case 44:
|
|
1159
1125
|
case "end":
|
|
1160
|
-
return
|
|
1126
|
+
return _context3.stop();
|
|
1161
1127
|
}
|
|
1162
1128
|
}
|
|
1163
|
-
},
|
|
1129
|
+
}, _callee3, this);
|
|
1164
1130
|
}));
|
|
1165
1131
|
|
|
1166
1132
|
function setBackground(_x) {
|
|
@@ -1172,9 +1138,15 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1172
1138
|
|
|
1173
1139
|
_proto.stop = function stop() {
|
|
1174
1140
|
if (this.isVirtualBackground) {
|
|
1175
|
-
var _this$backgroundImage;
|
|
1141
|
+
var _this$backgroundImage, _this$backgroundVideo;
|
|
1176
1142
|
|
|
1177
1143
|
(_this$backgroundImage = this.backgroundImage) == null ? void 0 : _this$backgroundImage.removeAttribute('src');
|
|
1144
|
+
(_this$backgroundVideo = this.backgroundVideo) == null ? void 0 : _this$backgroundVideo.removeAttribute('src');
|
|
1145
|
+
|
|
1146
|
+
if (this.backgroundType == "video") {
|
|
1147
|
+
this.backgroundVideo.loop = false;
|
|
1148
|
+
this.backgroundVideo = null;
|
|
1149
|
+
}
|
|
1178
1150
|
}
|
|
1179
1151
|
|
|
1180
1152
|
if (this.outputCtx) {
|
|
@@ -1191,11 +1163,25 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1191
1163
|
}
|
|
1192
1164
|
|
|
1193
1165
|
this.input = input;
|
|
1194
|
-
this.output = output;
|
|
1166
|
+
this.output = output; // if(!this.t1){
|
|
1167
|
+
// this.output.style.position = "fixed"
|
|
1168
|
+
// this.output.style.top = String(0)
|
|
1169
|
+
// document.body.append(this.output)
|
|
1170
|
+
// this.t1 = true
|
|
1171
|
+
// }
|
|
1172
|
+
|
|
1195
1173
|
var ctx = output.getContext('2d');
|
|
1196
1174
|
|
|
1197
1175
|
if (ctx.canvas.width !== input.width) {
|
|
1198
1176
|
ctx.canvas.width = input.width;
|
|
1177
|
+
|
|
1178
|
+
if (this.backgroundType == "video") {
|
|
1179
|
+
this.backgroundVideo.width = input.width;
|
|
1180
|
+
this.backgroundVideo.height = input.height;
|
|
1181
|
+
} // this.backgroundVideo!.style.top = String(0)
|
|
1182
|
+
// this.backgroundVideo!.style.position = "fixed"
|
|
1183
|
+
// document.body.append(this.backgroundVideo!)
|
|
1184
|
+
|
|
1199
1185
|
}
|
|
1200
1186
|
|
|
1201
1187
|
if (ctx.canvas.height !== input.height) {
|
|
@@ -1206,32 +1192,32 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1206
1192
|
this.imageAspectRatio = input.width / input.height;
|
|
1207
1193
|
|
|
1208
1194
|
if (this.imageAspectRatio <= 0) {
|
|
1209
|
-
throw new Error(
|
|
1195
|
+
throw new Error("Invalid input width/height");
|
|
1210
1196
|
}
|
|
1211
1197
|
|
|
1212
1198
|
var process = /*#__PURE__*/function () {
|
|
1213
|
-
var
|
|
1214
|
-
return runtime_1.wrap(function
|
|
1199
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee4() {
|
|
1200
|
+
return runtime_1.wrap(function _callee4$(_context4) {
|
|
1215
1201
|
while (1) {
|
|
1216
|
-
switch (
|
|
1202
|
+
switch (_context4.prev = _context4.next) {
|
|
1217
1203
|
case 0:
|
|
1218
|
-
|
|
1204
|
+
_context4.next = 2;
|
|
1219
1205
|
return _this2.runSegmentation(skipProcessing);
|
|
1220
1206
|
|
|
1221
1207
|
case 2:
|
|
1222
1208
|
case "end":
|
|
1223
|
-
return
|
|
1209
|
+
return _context4.stop();
|
|
1224
1210
|
}
|
|
1225
1211
|
}
|
|
1226
|
-
},
|
|
1212
|
+
}, _callee4);
|
|
1227
1213
|
}));
|
|
1228
1214
|
|
|
1229
1215
|
return function process() {
|
|
1230
|
-
return
|
|
1216
|
+
return _ref2.apply(this, arguments);
|
|
1231
1217
|
};
|
|
1232
1218
|
}();
|
|
1233
1219
|
|
|
1234
|
-
if (this.background ===
|
|
1220
|
+
if (this.background === "none" && !this.isVirtualBackground) {
|
|
1235
1221
|
this.outputCtx.globalCompositeOperation = 'copy';
|
|
1236
1222
|
this.outputCtx.filter = 'none';
|
|
1237
1223
|
this.outputCtx.drawImage(input, 0, 0, input.width, input.height);
|
|
@@ -1241,13 +1227,13 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1241
1227
|
};
|
|
1242
1228
|
|
|
1243
1229
|
_proto.setImage = /*#__PURE__*/function () {
|
|
1244
|
-
var _setImage = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1245
|
-
return runtime_1.wrap(function
|
|
1230
|
+
var _setImage = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee5(image) {
|
|
1231
|
+
return runtime_1.wrap(function _callee5$(_context5) {
|
|
1246
1232
|
while (1) {
|
|
1247
|
-
switch (
|
|
1233
|
+
switch (_context5.prev = _context5.next) {
|
|
1248
1234
|
case 0:
|
|
1249
1235
|
image.crossOrigin = 'anonymous';
|
|
1250
|
-
return
|
|
1236
|
+
return _context5.abrupt("return", new Promise(function (resolve, reject) {
|
|
1251
1237
|
image.onload = function () {
|
|
1252
1238
|
return resolve(image);
|
|
1253
1239
|
};
|
|
@@ -1257,10 +1243,10 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1257
1243
|
|
|
1258
1244
|
case 2:
|
|
1259
1245
|
case "end":
|
|
1260
|
-
return
|
|
1246
|
+
return _context5.stop();
|
|
1261
1247
|
}
|
|
1262
1248
|
}
|
|
1263
|
-
},
|
|
1249
|
+
}, _callee5);
|
|
1264
1250
|
}));
|
|
1265
1251
|
|
|
1266
1252
|
function setImage(_x2) {
|
|
@@ -1282,7 +1268,8 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1282
1268
|
|
|
1283
1269
|
_proto.resizeInputData = function resizeInputData() {
|
|
1284
1270
|
this.personMaskCtx.drawImage(this.input, 0, 0, this.input.width, this.input.height, 0, 0, this.personMaskWidth, this.personMaskHeight);
|
|
1285
|
-
var imageData = this.personMaskCtx.getImageData(0, 0, this.personMaskWidth, this.personMaskHeight);
|
|
1271
|
+
var imageData = this.personMaskCtx.getImageData(0, 0, this.personMaskWidth, this.personMaskHeight); //
|
|
1272
|
+
|
|
1286
1273
|
var inputMemoryOffset = this.tfLite._getInputMemoryOffset() / 4;
|
|
1287
1274
|
|
|
1288
1275
|
for (var i = 0; i < this.personMaskPixelCount; i++) {
|
|
@@ -1329,31 +1316,32 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1329
1316
|
|
|
1330
1317
|
this.drawPersonMask();
|
|
1331
1318
|
this.outputCtx.globalCompositeOperation = 'source-in';
|
|
1332
|
-
this.outputCtx.filter = 'none'; // //
|
|
1333
|
-
|
|
1334
|
-
this.outputCtx.drawImage(this.input, 0, 0);
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
//
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
//
|
|
1346
|
-
//
|
|
1347
|
-
//
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1319
|
+
this.outputCtx.filter = 'none'; // //Draw the foreground
|
|
1320
|
+
|
|
1321
|
+
this.outputCtx.drawImage(this.input, 0, 0);
|
|
1322
|
+
|
|
1323
|
+
if (this.enableSharpening) {
|
|
1324
|
+
this.sharpenFilter();
|
|
1325
|
+
} // //Draw the background
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
this.drawSegmentedBackground();
|
|
1329
|
+
};
|
|
1330
|
+
|
|
1331
|
+
_proto.sharpenFilter = function sharpenFilter() {
|
|
1332
|
+
// adding sharpening filter to each frame to improve edges and brightness
|
|
1333
|
+
// The basic idea is that you take the weighed sum of a rectangle of pixels from the source image and use that as the output value using convolution filter
|
|
1334
|
+
// It is applied intermediate output with black background and only mask data in frame
|
|
1335
|
+
// Filter currently used is 3 x 3 sharpening filter with values as shown:
|
|
1336
|
+
// [ 0, -1, 0,
|
|
1337
|
+
// -1, 5, -1,
|
|
1338
|
+
// 0, -1, 0 ]
|
|
1339
|
+
var outputImageData = this.outputCtx.getImageData(0, 0, this.output.width, this.output.height); // filters you may try
|
|
1340
|
+
// [-1, -1, -1, -1, 9, -1, -1, -1, -1]
|
|
1352
1341
|
//[0, -1, 0, -1, 5, -1, 0, -1, 0]
|
|
1353
1342
|
|
|
1354
|
-
var output = this.
|
|
1355
|
-
this.outputCtx.putImageData(output, 0, 0);
|
|
1356
|
-
//this.log(TAG, t1 - t0);
|
|
1343
|
+
var output = this.filters.convolute(outputImageData);
|
|
1344
|
+
this.outputCtx.putImageData(output, 0, 0);
|
|
1357
1345
|
};
|
|
1358
1346
|
|
|
1359
1347
|
_proto.drawPersonMask = function drawPersonMask() {
|
|
@@ -1366,40 +1354,42 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1366
1354
|
this.outputCtx.imageSmoothingQuality = 'high';
|
|
1367
1355
|
|
|
1368
1356
|
if (this.isVirtualBackground) {
|
|
1369
|
-
this.
|
|
1357
|
+
if (this.backgroundType === "video") {
|
|
1358
|
+
this.fitVideoToBackground();
|
|
1359
|
+
} else if (this.backgroundType === "image") {
|
|
1360
|
+
this.fitImageToBackground();
|
|
1361
|
+
}
|
|
1370
1362
|
} else {
|
|
1371
1363
|
this.addBlurToBackground();
|
|
1372
1364
|
}
|
|
1373
1365
|
};
|
|
1374
1366
|
|
|
1375
1367
|
_proto.runSegmentation = /*#__PURE__*/function () {
|
|
1376
|
-
var _runSegmentation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1377
|
-
|
|
1378
|
-
return runtime_1.wrap(function _callee5$(_context5) {
|
|
1368
|
+
var _runSegmentation = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee6(skipProcessing) {
|
|
1369
|
+
return runtime_1.wrap(function _callee6$(_context6) {
|
|
1379
1370
|
while (1) {
|
|
1380
|
-
switch (
|
|
1371
|
+
switch (_context6.prev = _context6.next) {
|
|
1381
1372
|
case 0:
|
|
1382
1373
|
if (!this.tfLite) {
|
|
1383
|
-
|
|
1374
|
+
_context6.next = 5;
|
|
1384
1375
|
break;
|
|
1385
1376
|
}
|
|
1386
1377
|
|
|
1387
|
-
start = performance.now();
|
|
1378
|
+
// const start = performance.now();
|
|
1388
1379
|
this.resizeInputData();
|
|
1389
|
-
|
|
1380
|
+
_context6.next = 4;
|
|
1390
1381
|
return this.infer(skipProcessing);
|
|
1391
1382
|
|
|
1392
|
-
case
|
|
1393
|
-
this.postProcessing();
|
|
1394
|
-
|
|
1395
|
-
this.log(TAG$1, 'time taken', end - start);
|
|
1383
|
+
case 4:
|
|
1384
|
+
this.postProcessing(); // const end = performance.now();
|
|
1385
|
+
// this.log(TAG,"time taken",end -start);
|
|
1396
1386
|
|
|
1397
|
-
case
|
|
1387
|
+
case 5:
|
|
1398
1388
|
case "end":
|
|
1399
|
-
return
|
|
1389
|
+
return _context6.stop();
|
|
1400
1390
|
}
|
|
1401
1391
|
}
|
|
1402
|
-
},
|
|
1392
|
+
}, _callee6, this);
|
|
1403
1393
|
}));
|
|
1404
1394
|
|
|
1405
1395
|
function runSegmentation(_x3) {
|
|
@@ -1409,6 +1399,23 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1409
1399
|
return runSegmentation;
|
|
1410
1400
|
}();
|
|
1411
1401
|
|
|
1402
|
+
_proto.fitVideoToBackground = function fitVideoToBackground() {
|
|
1403
|
+
// let input_width: any, input_height: any, x_off: any, y_off: any
|
|
1404
|
+
// if ((this.backgroundVideo!.width / this.backgroundVideo!.height) < this.imageAspectRatio) {
|
|
1405
|
+
// input_width = this.backgroundVideo!.width;
|
|
1406
|
+
// input_height = this.backgroundVideo!.width/ this.imageAspectRatio;
|
|
1407
|
+
// x_off = 0;
|
|
1408
|
+
// y_off = (this.backgroundVideo!.height - input_height) / 2;
|
|
1409
|
+
// }
|
|
1410
|
+
// else {
|
|
1411
|
+
// input_height = this.backgroundVideo!.height;
|
|
1412
|
+
// input_width = this.backgroundVideo!.height * this.imageAspectRatio;
|
|
1413
|
+
// y_off = 0;
|
|
1414
|
+
// x_off = (this.backgroundVideo!.width - input_width) / 2;
|
|
1415
|
+
// }
|
|
1416
|
+
this.outputCtx.drawImage(this.backgroundVideo, 0, 0, this.output.width, this.output.height);
|
|
1417
|
+
};
|
|
1418
|
+
|
|
1412
1419
|
_proto.fitImageToBackground = function fitImageToBackground() {
|
|
1413
1420
|
var input_width, input_height, x_offset, y_offset;
|
|
1414
1421
|
|
|
@@ -1428,11 +1435,11 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1428
1435
|
};
|
|
1429
1436
|
|
|
1430
1437
|
_proto.addBlurToBackground = /*#__PURE__*/function () {
|
|
1431
|
-
var _addBlurToBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function
|
|
1438
|
+
var _addBlurToBackground = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7() {
|
|
1432
1439
|
var blurValue;
|
|
1433
|
-
return runtime_1.wrap(function
|
|
1440
|
+
return runtime_1.wrap(function _callee7$(_context7) {
|
|
1434
1441
|
while (1) {
|
|
1435
|
-
switch (
|
|
1442
|
+
switch (_context7.prev = _context7.next) {
|
|
1436
1443
|
case 0:
|
|
1437
1444
|
blurValue = '15px';
|
|
1438
1445
|
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';
|
|
@@ -1441,10 +1448,10 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1441
1448
|
|
|
1442
1449
|
case 4:
|
|
1443
1450
|
case "end":
|
|
1444
|
-
return
|
|
1451
|
+
return _context7.stop();
|
|
1445
1452
|
}
|
|
1446
1453
|
}
|
|
1447
|
-
},
|
|
1454
|
+
}, _callee7, this);
|
|
1448
1455
|
}));
|
|
1449
1456
|
|
|
1450
1457
|
function addBlurToBackground() {
|
|
@@ -1454,6 +1461,80 @@ var HMSVirtualBackgroundPlugin = /*#__PURE__*/function () {
|
|
|
1454
1461
|
return addBlurToBackground;
|
|
1455
1462
|
}();
|
|
1456
1463
|
|
|
1464
|
+
_proto.initSharpenFilter = function initSharpenFilter() {
|
|
1465
|
+
var _this3 = this;
|
|
1466
|
+
|
|
1467
|
+
this.filters.tmpCanvas = document.createElement('canvas');
|
|
1468
|
+
this.filters.tmpCtx = this.filters.tmpCanvas.getContext('2d');
|
|
1469
|
+
|
|
1470
|
+
this.filters.createImageData = function (w, h) {
|
|
1471
|
+
return _this3.filters.tmpCtx.createImageData(w, h);
|
|
1472
|
+
};
|
|
1473
|
+
|
|
1474
|
+
this.filters.convolute = function (pixels, weights, opaque) {
|
|
1475
|
+
if (weights === void 0) {
|
|
1476
|
+
weights = [0, -1, 0, -1, 5, -1, 0, -1, 0];
|
|
1477
|
+
}
|
|
1478
|
+
|
|
1479
|
+
var side = Math.round(Math.sqrt(weights.length));
|
|
1480
|
+
var halfSide = Math.floor(side / 2);
|
|
1481
|
+
var src = pixels.data;
|
|
1482
|
+
var sw = pixels.width;
|
|
1483
|
+
var sh = pixels.height; // pad output by the convolution matrix
|
|
1484
|
+
|
|
1485
|
+
var w = sw;
|
|
1486
|
+
var h = sh;
|
|
1487
|
+
|
|
1488
|
+
var output = _this3.filters.createImageData(w, h);
|
|
1489
|
+
|
|
1490
|
+
var dst = output.data; // go through the destination image pixels
|
|
1491
|
+
|
|
1492
|
+
var alphaFac = opaque ? 1 : 0;
|
|
1493
|
+
|
|
1494
|
+
for (var y = 0; y < h; y = y + 1) {
|
|
1495
|
+
for (var x = 0; x < w; x = x + 1) {
|
|
1496
|
+
var dstOff = (y * w + x) * 4;
|
|
1497
|
+
|
|
1498
|
+
if (src[dstOff + 3] == 0) {
|
|
1499
|
+
continue;
|
|
1500
|
+
} else if (x < w && y < h) {
|
|
1501
|
+
var sy = y;
|
|
1502
|
+
var sx = x; // calculate the weighed sum of the source image pixels that
|
|
1503
|
+
// fall under the convolution matrix
|
|
1504
|
+
|
|
1505
|
+
var r = 0,
|
|
1506
|
+
g = 0,
|
|
1507
|
+
b = 0,
|
|
1508
|
+
a = 0;
|
|
1509
|
+
|
|
1510
|
+
for (var cy = 0; cy < side; cy++) {
|
|
1511
|
+
for (var cx = 0; cx < side; cx++) {
|
|
1512
|
+
var scy = sy + cy - halfSide;
|
|
1513
|
+
var scx = sx + cx - halfSide;
|
|
1514
|
+
|
|
1515
|
+
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
|
|
1516
|
+
var srcOff = (scy * sw + scx) * 4;
|
|
1517
|
+
var wt = weights[cy * side + cx];
|
|
1518
|
+
r += src[srcOff] * wt;
|
|
1519
|
+
g += src[srcOff + 1] * wt;
|
|
1520
|
+
b += src[srcOff + 2] * wt;
|
|
1521
|
+
a += src[srcOff + 3] * wt;
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
dst[dstOff] = r;
|
|
1527
|
+
dst[dstOff + 1] = g;
|
|
1528
|
+
dst[dstOff + 2] = b;
|
|
1529
|
+
dst[dstOff + 3] = a + alphaFac * (255 - a);
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
return output;
|
|
1535
|
+
};
|
|
1536
|
+
};
|
|
1537
|
+
|
|
1457
1538
|
return HMSVirtualBackgroundPlugin;
|
|
1458
1539
|
}();
|
|
1459
1540
|
|