blur and body segmentation tweaks, adding kernel presets

This commit is contained in:
2023-03-24 14:48:44 +01:00
parent ccbd3e4365
commit 3fd8f610fe
6 changed files with 234 additions and 49 deletions

View File

@@ -15,28 +15,17 @@ import {
} from '@tensorflow-models/body-segmentation'
import {canvasPool2D} from "../../canvas/CanvasPool";
import {waitForMedia} from "../../utils";
import { mediaSize } from '../canvas';
const inputs = {
image: {
type: 'Image'
} as ImageVar,
width: {
type: 'Number',
default: 100,
step: 1,
min: 1
} as NumberVar,
height: {
type: 'Number',
default: 100,
step: 1,
min: 1,
} as NumberVar,
// blur mask edges by number of pixels
maskBlurAmount: {
type: 'Number',
default: 0,
step: 1,
default: 1,
step: 0.1,
} as NumberVar,
// Default to 0.5. The minimum probability to color a pixel as foreground rather than background. The alpha channel integer values will be taken as the probabilities (for more information refer to Segmentation type's documentation).
foregroundThreshold: {
@@ -70,29 +59,6 @@ export default class BodySegmentation extends Node<typeof inputs, typeof outputs
super('BodySegmentation', inputs, outputs);
}
get width() {
return this.__in.width.value;
}
get height() {
return this.__in.height.value;
}
get image() {
if (!this.__in.image.value) {
return null;
} else {
const {width, height} = this.__in.image.value;
return {
image: this.__in.image.value,
top: 0,
left: 0,
width,
height,
};
}
}
async initSegmenter() {
if (this.segmenter) {
return;
@@ -108,13 +74,14 @@ export default class BodySegmentation extends Node<typeof inputs, typeof outputs
async segmentPeople() {
await this.initSegmenter();
await waitForMedia(this.in.image.value);
const {width, height} = mediaSize(this.__in.image.value);
if (!this.in.image.value) {
return;
}
const originalImageCanvas = canvasPool2D.createCanvas();
originalImageCanvas.width = this.in.width.value;
originalImageCanvas.height = this.in.height.value
originalImageCanvas.width = width;
originalImageCanvas.height = height;
originalImageCanvas.acquire();
const originalCanvasCtx = originalImageCanvas.getContext('2d');
@@ -137,16 +104,15 @@ export default class BodySegmentation extends Node<typeof inputs, typeof outputs
const canvas = canvasPool2D.createCanvas();
canvas.acquire();
const ctx = canvas.getContext('2d');
canvas.width = this.in.width.value;
canvas.height = this.in.height.value
canvas.width = width;
canvas.height = height;
const maskBlurAmount = this.in.maskBlurAmount.value;
await drawMask(canvas, canvas as CanvasImageSource, backgroundDarkeningMask, 1, maskBlurAmount);
this.out.image.value = canvas;
this.out.originalImage.value = originalImageCanvas;
this.out.width.value = this.in.width.value;
this.out.height.value = this.in.height.value;
this.out.width.value = width;
this.out.height.value = height;
}
async _update(){