fix Option

This commit is contained in:
Harvey Zack committed 2024-10-08 11:53:26 +08:00
1 parent fdfc44806c
commit 7a1240b889
4 files changed
+89 -29

No files matched your search

@@ -158,9 +158,22 @@ function artplayerPluginDanmukuMask(option = {}) {
let ctx = null;
let animationFrameId = null;
let isInitialized = false;
const config = {
solutionPath: option.solutionPath || "https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation",
modelSelection: option.modelSelection || 1,
smoothSegmentation: option.smoothSegmentation !== undefined ? option.smoothSegmentation : true,
minDetectionConfidence: option.minDetectionConfidence || 0.5,
minTrackingConfidence: option.minTrackingConfidence || 0.5,
selfieMode: option.selfieMode || false,
drawContour: option.drawContour || false,
foregroundThreshold: option.foregroundThreshold || 0.5,
opacity: option.opacity || 1,
maskBlurAmount: option.maskBlurAmount || 3
};
async function initTensorFlow() {
try {
await _tfjsCore.setBackend("webgl");
console.log("Using WebGL backend");
} catch (e) {
console.warn("WebGL backend not available, falling back to CPU");
await _tfjsCore.setBackend("cpu");
@@ -172,22 +185,31 @@ function artplayerPluginDanmukuMask(option = {}) {
const segmenterConfig = {
runtime: "mediapipe",
modelType: "general",
solutionPath: option.solutionPath || "https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation",
modelSelection: option.modelSelection || 1,
smoothSegmentation: option.smoothSegmentation !== undefined ? option.smoothSegmentation : true,
minDetectionConfidence: option.minDetectionConfidence || 0.5,
minTrackingConfidence: option.minTrackingConfidence || 0.5,
selfieMode: option.selfieMode || false
solutionPath: config.solutionPath,
modelSelection: config.modelSelection,
smoothSegmentation: config.smoothSegmentation,
minDetectionConfidence: config.minDetectionConfidence,
minTrackingConfidence: config.minTrackingConfidence,
selfieMode: config.selfieMode
};
try {
segmenter = await _bodySegmentation.createSegmenter(model, segmenterConfig);
$danmuku.style.maskMode = "alpha";
isInitialized = true;
console.log("Segmenter initialized successfully");
} catch (error) {
console.error("Error initializing segmenter:", error);
isInitialized = false;
}
}
function setupDanmukuStyle() {
Object.assign($danmuku.style, {
maskMode: "alpha",
maskSize: "contain",
maskRepeat: "no-repeat",
backgroundSize: "contain",
backgroundRepeat: "no-repeat"
});
}
function createCanvas() {
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
@@ -220,19 +242,20 @@ function artplayerPluginDanmukuMask(option = {}) {
animationFrameId = requestAnimationFrame(segmentBody);
return;
}
const foregroundThreshold = option.foregroundThreshold || 0.6;
const mask = await _bodySegmentation.toBinaryMask(segmentation, {
const foregroundColor = {
r: 255,
g: 255,
b: 255,
a: 255
}, {
};
const backgroundColor = {
r: 0,
g: 0,
b: 0,
a: 255
}, false, foregroundThreshold);
await _bodySegmentation.drawMask(canvas, $video, mask, 1, 1);
};
const mask = await _bodySegmentation.toBinaryMask(segmentation, foregroundColor, backgroundColor, config.drawContour, config.foregroundThreshold);
await _bodySegmentation.drawMask(canvas, $video, mask, config.opacity, config.maskBlurAmount);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
ctx.putImageData(makeWhiteTransparent(imageData), 0, 0);
$danmuku.style.maskImage = `url(${canvas.toDataURL()})`;
@@ -244,6 +267,7 @@ function artplayerPluginDanmukuMask(option = {}) {
async function startSegmentation() {
if (!isInitialized) await initSegmenter();
if (!canvas) createCanvas();
setupDanmukuStyle();
segmentBody();
}
function stopSegmentation() {