metadata
library_name: transformers.js
tags:
- pose-estimation
https://github.com/open-mmlab/mmpose/tree/main/projects/rtmo with ONNX weights to be compatible with Transformers.js.
Usage (Transformers.js)
If you haven't already, you can install the Transformers.js JavaScript library from NPM using:
npm i @xenova/transformers
Example: Perform pose-estimation w/ Xenova/RTMO-t.
import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
// Load model and processor
const model_id = 'Xenova/RTMO-t';
const model = await AutoModel.from_pretrained(model_id, {
quantized: false,
});
const processor = await AutoProcessor.from_pretrained(model_id);
// Read image and run processor
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg';
const image = await RawImage.read(url);
const { pixel_values, original_sizes, reshaped_input_sizes } = await processor(image);
// Predict bounding boxes and keypoints
const { dets, keypoints } = await model({ input: pixel_values });
// Select the first image
const predicted_boxes = dets.tolist()[0];
const predicted_points = keypoints.tolist()[0];
const [width, height] = original_sizes[0];
const [resized_width, resized_height] = reshaped_input_sizes[0];
// Compute scale values
const xScale = width / resized_width;
const yScale = height / resized_height;
// Define thresholds
const point_threshold = 0.3;
const box_threshold = 0.3;
// Parse results
for (let i = 0; i < predicted_boxes.length; ++i) {
const [xmin, ymin, xmax, ymax, box_score] = predicted_boxes[i];
if (box_score < box_threshold) continue;
const x1 = (xmin * xScale).toFixed(2);
const y1 = (ymin * yScale).toFixed(2);
const x2 = (xmax * xScale).toFixed(2);
const y2 = (ymax * yScale).toFixed(2);
console.log(`Found person at [${x1}, ${y1}, ${x2}, ${y2}] with score ${box_score.toFixed(3)}`)
const points = predicted_points[i]; // of shape [17, 3]
for (let id = 0; id < points.length; ++id) {
const label = model.config.id2label[id];
const [x, y, point_score] = points[id];
if (point_score < point_threshold) continue;
console.log(` - ${label}: (${(x * xScale).toFixed(2)}, ${(y * yScale).toFixed(2)}) with score ${point_score.toFixed(3)}`);
}
}
See example output
Found person at [275.55, 99.07, 433.03, 753.93] with score 0.989
- nose: (348.04, 178.71) with score 0.686
- left_eye: (357.65, 166.47) with score 0.347
- right_eye: (341.15, 164.56) with score 0.331
- left_shoulder: (375.16, 258.75) with score 0.999
- right_shoulder: (312.26, 241.00) with score 0.999
- left_elbow: (381.83, 360.03) with score 0.999
- right_elbow: (291.49, 326.95) with score 0.999
- left_wrist: (399.93, 455.04) with score 0.997
- right_wrist: (335.69, 327.09) with score 0.999
- left_hip: (357.00, 457.75) with score 0.999
- right_hip: (317.68, 474.91) with score 0.998
- left_knee: (385.42, 547.58) with score 0.998
- right_knee: (339.32, 648.62) with score 0.953
- left_ankle: (384.14, 699.15) with score 0.990
- right_ankle: (357.63, 621.90) with score 0.906
Found person at [67.16, 23.39, 338.29, 783.70] with score 0.932
- left_shoulder: (157.14, 159.59) with score 0.973
- right_shoulder: (159.63, 144.00) with score 0.872
- left_elbow: (210.02, 242.90) with score 0.982
- right_elbow: (201.28, 231.61) with score 0.694
- left_wrist: (258.17, 330.94) with score 0.978
- right_wrist: (205.45, 305.74) with score 0.654
- left_hip: (231.90, 367.39) with score 0.999
- right_hip: (240.19, 365.36) with score 1.000
- left_knee: (195.22, 574.91) with score 0.999
- right_knee: (216.01, 578.60) with score 1.000
- left_ankle: (283.08, 528.39) with score 1.000
- right_ankle: (300.10, 733.43) with score 0.999
Found person at [-0.17, 75.70, 109.33, 555.50] with score 0.806
- nose: (49.95, 123.09) with score 0.385
- left_shoulder: (78.57, 171.93) with score 0.983
- right_shoulder: (30.76, 174.85) with score 0.974
- left_elbow: (91.19, 248.35) with score 0.859
- right_elbow: (19.49, 241.54) with score 0.877
- left_wrist: (93.19, 314.57) with score 0.812
- right_wrist: (6.06, 301.15) with score 0.899
- left_hip: (69.81, 313.63) with score 0.995
- right_hip: (41.66, 315.41) with score 0.997
- left_knee: (76.36, 416.84) with score 0.999
- right_knee: (44.06, 420.83) with score 0.998
- left_ankle: (83.66, 514.07) with score 0.992
- right_ankle: (44.09, 519.74) with score 0.983
Found person at [348.03, 53.31, 432.29, 526.36] with score 0.328
- left_shoulder: (395.69, 156.16) with score 1.000
- right_shoulder: (351.04, 157.42) with score 0.917
- left_elbow: (413.08, 225.16) with score 0.995
- left_wrist: (418.51, 295.31) with score 0.985
- left_hip: (387.03, 287.25) with score 0.995
- right_hip: (359.71, 286.18) with score 0.939
- left_knee: (389.06, 393.34) with score 0.992
- right_knee: (363.29, 393.47) with score 0.922
- left_ankle: (391.54, 482.10) with score 0.970
- right_ankle: (365.87, 483.47) with score 0.847