@tensorflow-models/body-pix
Version:
Pretrained BodyPix model in TensorFlow.js
161 lines (160 loc) • 7.91 kB
TypeScript
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import { Color, PartSegmentation, PersonSegmentation } from './types';
import { SemanticPartSegmentation, SemanticPersonSegmentation } from './types';
export type Canvas = HTMLCanvasElement | OffscreenCanvas;
type ImageType = HTMLImageElement | HTMLVideoElement | Canvas;
/**
* Given the output from estimating multi-person segmentation, generates an
* image with foreground and background color at each pixel determined by the
* corresponding binary segmentation value at the pixel from the output. In
* other words, pixels where there is a person will be colored with foreground
* color and where there is not a person will be colored with background color.
*
* @param personOrPartSegmentation The output from
* `segmentPerson`, `segmentMultiPerson`,
* `segmentPersonParts` or `segmentMultiPersonParts`. They can
* be SemanticPersonSegmentation object, an array of PersonSegmentation object,
* SemanticPartSegmentation object, or an array of PartSegmentation object.
*
* @param foreground Default to {r:0, g:0, b:0, a: 0}. The foreground color
* (r,g,b,a) for visualizing pixels that belong to people.
*
* @param background Default to {r:0, g:0, b:0, a: 255}. The background color
* (r,g,b,a) for visualizing pixels that don't belong to people.
*
* @param drawContour Default to false. Whether to draw the contour around each
* person's segmentation mask or body part mask.
*
* @param foregroundIds Default to [1]. The integer values that represent
* foreground. For person segmentation, 1 is the foreground. For body part
* segmentation, it can be a subset of all body parts ids.
*
* @returns An ImageData with the same width and height of
* all the PersonSegmentation in multiPersonSegmentation, with opacity and
* transparency at each pixel determined by the corresponding binary
* segmentation value at the pixel from the output.
*/
export declare function toMask(personOrPartSegmentation: SemanticPersonSegmentation | SemanticPartSegmentation | PersonSegmentation[] | PartSegmentation[], foreground?: Color, background?: Color, drawContour?: boolean, foregroundIds?: number[]): ImageData;
/**
* Given the output from person body part segmentation (or multi-person
* instance body part segmentation) and an array of colors indexed by part id,
* generates an image with the corresponding color for each part at each pixel,
* and white pixels where there is no part.
*
* @param partSegmentation The output from segmentPersonParts
* or segmentMultiPersonParts. The former is a SemanticPartSegmentation
* object and later is an array of PartSegmentation object.
*
* @param partColors A multi-dimensional array of rgb colors indexed by
* part id. Must have 24 colors, one for every part.
*
* @returns An ImageData with the same width and height of all the element in
* multiPersonPartSegmentation, with the corresponding color for each part at
* each pixel, and black pixels where there is no part.
*/
export declare function toColoredPartMask(partSegmentation: SemanticPartSegmentation | PartSegmentation[], partColors?: Array<[number, number, number]>): ImageData;
/**
* Given an image and a maskImage of type ImageData, draws the image with the
* mask on top of it onto a canvas.
*
* @param canvas The canvas to be drawn onto.
*
* @param image The original image to apply the mask to.
*
* @param maskImage An ImageData containing the mask. Ideally this should be
* generated by toMask or toColoredPartMask.
*
* @param maskOpacity The opacity of the mask when drawing it on top of the
* image. Defaults to 0.7. Should be a float between 0 and 1.
*
* @param maskBlurAmount How many pixels to blur the mask by. Defaults to 0.
* Should be an integer between 0 and 20.
*
* @param flipHorizontal If the result should be flipped horizontally. Defaults
* to false.
*/
export declare function drawMask(canvas: Canvas, image: ImageType, maskImage: ImageData | null, maskOpacity?: number, maskBlurAmount?: number, flipHorizontal?: boolean): void;
/**
* Given an image and a maskImage of type ImageData, draws the image with the
* pixelated mask on top of it onto a canvas.
*
* @param canvas The canvas to be drawn onto.
*
* @param image The original image to apply the mask to.
*
* @param maskImage An ImageData containing the mask. Ideally this should be
* generated by toColoredPartMask.
*
* @param maskOpacity The opacity of the mask when drawing it on top of the
* image. Defaults to 0.7. Should be a float between 0 and 1.
*
* @param maskBlurAmount How many pixels to blur the mask by. Defaults to 0.
* Should be an integer between 0 and 20.
*
* @param flipHorizontal If the result should be flipped horizontally. Defaults
* to false.
*
* @param pixelCellWidth The width of each pixel cell. Default to 10 px.
*/
export declare function drawPixelatedMask(canvas: Canvas, image: ImageType, maskImage: ImageData, maskOpacity?: number, maskBlurAmount?: number, flipHorizontal?: boolean, pixelCellWidth?: number): void;
/**
* Given a personSegmentation and an image, draws the image with its background
* blurred onto the canvas.
*
* @param canvas The canvas to draw the background-blurred image onto.
*
* @param image The image to blur the background of and draw.
*
* @param personSegmentation A SemanticPersonSegmentation or an array of
* PersonSegmentation object.
*
* @param backgroundBlurAmount How many pixels in the background blend into each
* other. Defaults to 3. Should be an integer between 1 and 20.
*
* @param edgeBlurAmount How many pixels to blur on the edge between the person
* and the background by. Defaults to 3. Should be an integer between 0 and 20.
*
* @param flipHorizontal If the output should be flipped horizontally. Defaults
* to false.
*/
export declare function drawBokehEffect(canvas: Canvas, image: ImageType, multiPersonSegmentation: SemanticPersonSegmentation | PersonSegmentation[], backgroundBlurAmount?: number, edgeBlurAmount?: number, flipHorizontal?: boolean): void;
/**
* Given a personSegmentation and an image, draws the image with its background
* blurred onto the canvas.
*
* @param canvas The canvas to draw the background-blurred image onto.
*
* @param image The image to blur the background of and draw.
*
* @param partSegmentation A SemanticPartSegmentation or an array of
* PartSegmentation object.
*
* @param bodyPartIdsToBlur Default to [0, 1] (left-face and right-face). An
* array of body part ids to blur. Each must be one of the 24 body part ids.
*
* @param backgroundBlurAmount How many pixels in the background blend into each
* other. Defaults to 3. Should be an integer between 1 and 20.
*
* @param edgeBlurAmount How many pixels to blur on the edge between the person
* and the background by. Defaults to 3. Should be an integer between 0 and 20.
*
* @param flipHorizontal If the output should be flipped horizontally. Defaults
* to false.
*/
export declare function blurBodyPart(canvas: Canvas, image: ImageType, partSegmentation: SemanticPartSegmentation | PartSegmentation[], bodyPartIdsToBlur?: number[], backgroundBlurAmount?: number, edgeBlurAmount?: number, flipHorizontal?: boolean): void;
export {};