human/types/src/human.d.ts

219 lines
8.7 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* Human main module
2021-10-27 15:45:38 +02:00
* @default Human Library
* @summary <https://github.com/vladmandic/human>
2021-10-25 19:09:00 +02:00
* @author <https://github.com/vladmandic>
2021-10-27 15:45:38 +02:00
* @copyright <https://github.com/vladmandic>
* @license MIT
2021-05-25 14:58:20 +02:00
*/
2021-10-21 16:54:51 +02:00
import { Env } from './util/env';
2021-03-17 23:57:00 +01:00
import * as tf from '../dist/tfjs.esm.js';
2021-10-21 16:54:51 +02:00
import * as draw from './util/draw';
import * as facemesh from './face/facemesh';
import * as match from './face/match';
import * as models from './models';
2021-11-14 17:22:52 +01:00
import type { Input, Tensor, DrawOptions, Config, Result, AnyCanvas } from './exports';
2021-10-25 19:09:00 +02:00
export * from './exports';
/** Instance of TensorFlow/JS used by Human
* - Can be TFJS that is bundled with `Human` or a manually imported TFJS library
* @external [API](https://js.tensorflow.org/api/latest/)
2021-05-31 00:45:39 +02:00
*/
2021-10-25 19:09:00 +02:00
export declare type TensorFlow = typeof tf;
/** **Human** library main class
2021-03-17 23:57:00 +01:00
*
* All methods and properties are available only as members of Human class
*
* - Configuration object definition: {@link Config}
* - Results object definition: {@link Result}
* - Possible inputs: {@link Input}
2021-05-31 00:45:39 +02:00
*
* @param userConfig: {@link Config}
2021-10-25 19:09:00 +02:00
* @returns instance of {@link Human}
2021-03-17 23:57:00 +01:00
*/
export declare class Human {
#private;
/** Current version of Human library in *semver* format */
2021-09-11 03:21:29 +02:00
version: string;
2021-04-13 17:05:52 +02:00
/** Current configuration
2021-11-08 13:36:26 +01:00
* - Defaults: [config](https://github.com/vladmandic/human/blob/main/src/config.ts#L262)
2021-04-13 17:05:52 +02:00
*/
2021-03-17 23:57:00 +01:00
config: Config;
2021-05-30 00:29:57 +02:00
/** Last known result of detect run
* - Can be accessed anytime after initial detection
2021-09-24 15:55:27 +02:00
*/
2021-05-30 00:29:57 +02:00
result: Result;
2021-04-13 17:05:52 +02:00
/** Current state of Human library
* - Can be polled to determine operations that are currently executed
* - Progresses through: 'config', 'check', 'backend', 'load', 'run:<model>', 'idle'
2021-04-13 17:05:52 +02:00
*/
2021-03-18 01:16:40 +01:00
state: string;
2021-09-11 22:11:00 +02:00
/** currenty processed image tensor and canvas */
process: {
2021-04-13 17:05:52 +02:00
tensor: Tensor | null;
2021-11-14 17:22:52 +01:00
canvas: AnyCanvas | null;
2021-03-17 23:57:00 +01:00
};
/** Instance of TensorFlow/JS used by Human
* - Can be embedded or externally provided
* @internal
2021-09-24 15:55:27 +02:00
*
* [TFJS API]<https://js.tensorflow.org/api/latest/>
2021-04-13 17:05:52 +02:00
*/
2021-03-17 23:57:00 +01:00
tf: TensorFlow;
/** Object containing environment information used for diagnostics */
2021-10-21 16:54:51 +02:00
env: Env;
2021-06-18 15:16:21 +02:00
/** Draw helper classes that can draw detected objects on canvas using specified draw
2021-10-25 19:09:00 +02:00
* @property options global settings for all draw operations, can be overriden for each draw method {@link DrawOptions}
2021-04-13 17:05:52 +02:00
*/
2021-03-17 23:57:00 +01:00
draw: {
2021-10-21 16:54:51 +02:00
canvas: typeof draw.canvas;
face: typeof draw.face;
body: typeof draw.body;
hand: typeof draw.hand;
gesture: typeof draw.gesture;
object: typeof draw.object;
person: typeof draw.person;
all: typeof draw.all;
2021-09-16 00:58:54 +02:00
options: DrawOptions;
2021-03-17 23:57:00 +01:00
};
/** Currently loaded models
* @internal
2021-09-24 15:55:27 +02:00
* {@link Models}
*/
2021-09-23 20:09:41 +02:00
models: models.Models;
2021-09-11 22:11:00 +02:00
/** Container for events dispatched by Human
2021-10-25 19:09:00 +02:00
* {@type} EventTarget
2021-09-11 22:11:00 +02:00
* Possible events:
* - `create`: triggered when Human object is instantiated
* - `load`: triggered when models are loaded (explicitly or on-demand)
2021-09-22 21:16:14 +02:00
* - `image`: triggered when input image is processed
2021-09-11 22:11:00 +02:00
* - `result`: triggered when detection is complete
* - `warmup`: triggered when warmup is complete
* - `error`: triggered on some errors
2021-09-11 22:11:00 +02:00
*/
events: EventTarget | undefined;
/** Reference face triangualtion array of 468 points, used for triangle references between points */
2021-03-29 21:59:16 +02:00
faceTriangulation: typeof facemesh.triangulation;
/** Refernce UV map of 468 values, used for 3D mapping of the face mesh */
2021-03-29 21:59:16 +02:00
faceUVMap: typeof facemesh.uvmap;
2021-04-13 17:05:52 +02:00
/** Performance object that contains values for all recently performed operations */
2021-06-03 15:41:53 +02:00
performance: Record<string, number>;
/** WebGL debug info */
gl: Record<string, unknown>;
/** Constructor for **Human** library that is futher used for all operations
*
2021-10-25 19:09:00 +02:00
* @param {Config} userConfig
* @returns {Human}
2021-04-13 17:05:52 +02:00
*/
2021-09-11 22:11:00 +02:00
constructor(userConfig?: Partial<Config>);
2021-03-21 12:49:55 +01:00
/** @hidden */
2021-08-17 14:51:17 +02:00
analyze: (...msg: string[]) => void;
2021-09-19 20:07:53 +02:00
/** Reset configuration to default values */
reset(): void;
2021-09-19 20:07:53 +02:00
/** Validate current configuration schema */
validate(userConfig?: Partial<Config>): {
2021-09-19 20:07:53 +02:00
reason: string;
where: string;
expected?: string;
}[];
/** Exports face matching methods */
similarity: typeof match.similarity;
distance: typeof match.distance;
match: typeof match.match;
2021-10-19 17:28:59 +02:00
/** Utility wrapper for performance.now() */
now(): number;
/** Process input as return canvas and tensor
*
* @param input: {@link Input}
2021-10-25 19:09:00 +02:00
* @param {boolean} input.getTensor should image processing also return tensor or just canvas
* @returns { tensor, canvas }
*/
2021-11-06 15:21:51 +01:00
image(input: Input, getTensor?: boolean): Promise<{
tensor: Tensor<import("@tensorflow/tfjs-core").Rank> | null;
2021-11-14 17:22:52 +01:00
canvas: AnyCanvas | null;
2021-11-06 15:21:51 +01:00
}>;
2021-09-22 21:16:14 +02:00
/** Segmentation method takes any input and returns processed canvas with body segmentation
* - Segmentation is not triggered as part of detect process
*
* Returns:
*
* @param input: {@link Input}
* @param background?: {@link Input}
2021-10-25 19:09:00 +02:00
* - Optional parameter background is used to fill the background with specific input
* @returns {object}
* - `data` as raw data array with per-pixel segmentation values
* - `canvas` as canvas which is input image filtered with segementation data and optionally merged with background image. canvas alpha values are set to segmentation values for easy merging
* - `alpha` as grayscale canvas that represents segmentation alpha values
*/
2021-09-22 21:16:14 +02:00
segmentation(input: Input, background?: Input): Promise<{
2021-11-12 21:07:23 +01:00
data: number[] | Tensor;
2021-11-14 17:22:52 +01:00
canvas: AnyCanvas | null;
alpha: AnyCanvas | null;
2021-09-22 21:16:14 +02:00
}>;
/** Enhance method performs additional enhacements to face image previously detected for futher processing
*
2021-05-31 00:45:39 +02:00
* @param input: Tensor as provided in human.result.face[n].tensor
2021-04-13 17:05:52 +02:00
* @returns Tensor
*/
2021-03-17 23:57:00 +01:00
enhance(input: Tensor): Tensor | null;
2021-11-07 16:03:33 +01:00
/** Compare two input tensors for pixel simmilarity
* - use `human.image` to process any valid input and get a tensor that can be used for compare
* - when passing manually generated tensors:
* - both input tensors must be in format [1, height, width, 3]
* - if resolution of tensors does not match, second tensor will be resized to match resolution of the first tensor
* @returns {number}
* - return value is pixel similarity score normalized by input resolution and rgb channels
*/
compare(firstImageTensor: Tensor, secondImageTensor: Tensor): Promise<number>;
/** Explicit backend initialization
* - Normally done implicitly during initial load phase
* - Call to explictly register and initialize TFJS backend without any other operations
2021-09-21 04:06:49 +02:00
* - Use when changing backend during runtime
*
2021-10-25 19:09:00 +02:00
* @returns {void}
*/
2021-09-21 04:06:49 +02:00
init(): Promise<void>;
2021-04-13 17:05:52 +02:00
/** Load method preloads all configured models on-demand
* - Not explicitly required as any required model is load implicitly on it's first run
*
* @param userConfig?: {@link Config}
* @return Promise<void>
2021-04-13 17:05:52 +02:00
*/
2021-09-11 22:11:00 +02:00
load(userConfig?: Partial<Config>): Promise<void>;
/** @hidden */
emit: (event: string) => void;
/** Runs interpolation using last known result and returns smoothened result
* Interpolation is based on time since last known result so can be called independently
*
* @param result?: {@link Result} optional use specific result set to run interpolation on
* @returns result: {@link Result}
*/
2021-09-15 19:59:18 +02:00
next(result?: Result): Result;
/** Warmup method pre-initializes all configured models for faster inference
* - can take significant time on startup
* - only used for `webgl` and `humangl` backends
* @param userConfig?: {@link Config}
2021-09-15 19:59:18 +02:00
* @returns result: {@link Result}
*/
2021-09-15 19:59:18 +02:00
warmup(userConfig?: Partial<Config>): Promise<Result | {
error: any;
}>;
2021-11-05 20:35:53 +01:00
/** Run detect with tensorflow profiling
* - result object will contain total exeuction time information for top-20 kernels
* - actual detection object can be accessed via `human.result`
*/
profile(input: Input, userConfig?: Partial<Config>): Promise<Record<string, number>>;
2021-04-13 17:05:52 +02:00
/** Main detection method
* - Analyze configuration: {@link Config}
2021-09-22 21:16:14 +02:00
* - Pre-process input: {@link Input}
2021-04-13 17:05:52 +02:00
* - Run inference for all configured models
* - Process and return result: {@link Result}
*
* @param input: {@link Input}
* @param userConfig?: {@link Config}
* @returns result: {@link Result}
2021-04-13 17:05:52 +02:00
*/
2021-11-14 17:22:52 +01:00
detect(input: Input, userConfig?: Partial<Config>): Promise<Result>;
2021-03-17 23:57:00 +01:00
}
/** Class Human as default export */
2021-03-17 23:57:00 +01:00
export { Human as default };