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