Pretrained PoseNet model in TensorFlow.js for real-time human pose estimation from images and video streams
—
Robust pose detection algorithm for images containing multiple people. Uses non-maximum suppression to avoid duplicate detections and sophisticated decoding to handle overlapping poses.
Detects and estimates multiple poses from input image using advanced decoding with part-based non-maximum suppression.
/**
* Estimate multiple person poses from input image
* @param input - Input image (various formats supported)
* @param config - Configuration options for multi-person inference
* @returns Promise resolving to array of detected poses
*/
estimateMultiplePoses(
input: PosenetInput,
config?: MultiPersonInferenceConfig
): Promise<Pose[]>;Usage Examples:
import * as posenet from '@tensorflow-models/posenet';
// Load model
const net = await posenet.load();
// Basic multiple pose estimation
const groupPhoto = document.getElementById('group-image') as HTMLImageElement;
const poses = await net.estimateMultiplePoses(groupPhoto);
console.log(`Detected ${poses.length} people`);
poses.forEach((pose, index) => {
console.log(`Person ${index + 1} confidence:`, pose.score);
});
// With custom detection parameters
const poses2 = await net.estimateMultiplePoses(groupPhoto, {
flipHorizontal: false,
maxDetections: 10, // Detect up to 10 people
scoreThreshold: 0.6, // Higher confidence threshold
nmsRadius: 25 // Larger suppression radius
});
// Filter high-quality poses
const goodPoses = poses2.filter(pose => pose.score > 0.7);
console.log(`Found ${goodPoses.length} high-quality poses`);
// Process each person's keypoints
poses.forEach((pose, personIndex) => {
const visibleKeypoints = pose.keypoints.filter(kp => kp.score > 0.5);
console.log(`Person ${personIndex}: ${visibleKeypoints.length} visible keypoints`);
// Find pose center (average of visible keypoints)
if (visibleKeypoints.length > 0) {
const center = visibleKeypoints.reduce(
(acc, kp) => ({ x: acc.x + kp.position.x, y: acc.y + kp.position.y }),
{ x: 0, y: 0 }
);
center.x /= visibleKeypoints.length;
center.y /= visibleKeypoints.length;
console.log(`Person ${personIndex} center:`, center);
}
});
// Real-time video processing
const video = document.getElementById('webcam') as HTMLVideoElement;
async function processVideoFrame() {
const poses = await net.estimateMultiplePoses(video, {
flipHorizontal: true,
maxDetections: 5,
scoreThreshold: 0.5,
nmsRadius: 20
});
// Draw poses on canvas or process data
drawPoses(poses);
requestAnimationFrame(processVideoFrame);
}Configuration options for multi-person pose estimation with advanced parameters.
/**
* Configuration interface for multiple person pose estimation
*/
interface MultiPersonInferenceConfig {
/** Whether to flip poses horizontally (useful for webcam feeds) */
flipHorizontal: boolean;
/** Maximum number of poses to detect in the image */
maxDetections?: number;
/** Minimum root part confidence score for pose detection */
scoreThreshold?: number;
/** Non-maximum suppression radius in pixels */
nmsRadius?: number;
}const MULTI_PERSON_INFERENCE_CONFIG: MultiPersonInferenceConfig = {
flipHorizontal: false,
maxDetections: 5,
scoreThreshold: 0.5,
nmsRadius: 20
};maxDetections (default: 5):
scoreThreshold (default: 0.5):
nmsRadius (default: 20):
flipHorizontal (default: false):
Multiple pose estimation supports the same input formats as single pose:
type PosenetInput =
| ImageData // Canvas ImageData object
| HTMLImageElement // HTML img element
| HTMLCanvasElement // HTML canvas element
| HTMLVideoElement // HTML video element (for real-time processing)
| tf.Tensor3D; // TensorFlow.js 3D tensorMultiple pose estimation returns a Promise that resolves to an array of Pose objects:
/**
* Array of detected poses, each with keypoints and confidence score
*/
Promise<Pose[]>
/**
* Individual detected pose
*/
interface Pose {
/** Array of 17 keypoints representing body parts */
keypoints: Keypoint[];
/** Overall pose confidence score (0-1) */
score: number;
}
/**
* Individual body part keypoint with position and confidence
*/
interface Keypoint {
/** Confidence score for this keypoint (0-1) */
score: number;
/** 2D position in image coordinates */
position: Vector2D;
/** Body part name (e.g., 'nose', 'leftWrist') */
part: string;
}The multi-person pose estimation algorithm uses a sophisticated "Fast Greedy Decoding" approach:
Ideal for:
Not ideal for:
The algorithm gracefully handles various challenging scenarios:
Install with Tessl CLI
npx tessl i tessl/npm-tensorflow-models--posenet