Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers.js
|
| 3 |
+
tags:
|
| 4 |
+
- pose-estimation
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
```js
|
| 9 |
+
import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
|
| 10 |
+
|
| 11 |
+
// Load model and processor
|
| 12 |
+
const model_id = 'Xenova/RTMO-t';
|
| 13 |
+
const model = await AutoModel.from_pretrained(model_id, {
|
| 14 |
+
quantized: false,
|
| 15 |
+
});
|
| 16 |
+
const processor = await AutoProcessor.from_pretrained(model_id);
|
| 17 |
+
|
| 18 |
+
// Read image and run processor
|
| 19 |
+
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg';
|
| 20 |
+
const image = await RawImage.read(url);
|
| 21 |
+
const { pixel_values, original_sizes, reshaped_input_sizes } = await processor(image);
|
| 22 |
+
|
| 23 |
+
// Predict bounding boxes and keypoints
|
| 24 |
+
const { dets, keypoints } = await model({ input: pixel_values });
|
| 25 |
+
|
| 26 |
+
// Select the first image
|
| 27 |
+
const predicted_boxes = dets.tolist()[0];
|
| 28 |
+
const predicted_points = keypoints.tolist()[0];
|
| 29 |
+
const [width, height] = original_sizes[0];
|
| 30 |
+
const [resized_width, resized_height] = reshaped_input_sizes[0];
|
| 31 |
+
|
| 32 |
+
// Compute scale values
|
| 33 |
+
const xScale = width / resized_width;
|
| 34 |
+
const yScale = height / resized_height;
|
| 35 |
+
|
| 36 |
+
// Define thresholds
|
| 37 |
+
const point_threshold = 0.3;
|
| 38 |
+
const box_threshold = 0.3;
|
| 39 |
+
|
| 40 |
+
// Parse results
|
| 41 |
+
for (let i = 0; i < predicted_boxes.length; ++i) {
|
| 42 |
+
const [xmin, ymin, xmax, ymax, box_score] = predicted_boxes[i];
|
| 43 |
+
if (box_score < box_threshold) continue;
|
| 44 |
+
|
| 45 |
+
const x1 = (xmin * xScale).toFixed(2);
|
| 46 |
+
const y1 = (ymin * yScale).toFixed(2);
|
| 47 |
+
const x2 = (xmax * xScale).toFixed(2);
|
| 48 |
+
const y2 = (ymax * yScale).toFixed(2);
|
| 49 |
+
|
| 50 |
+
console.log(`Found person at [${x1}, ${y1}, ${x2}, ${y2}] with score ${box_score.toFixed(3)}`)
|
| 51 |
+
const points = predicted_points[i]; // of shape [17, 3]
|
| 52 |
+
for (let id = 0; id < points.length; ++id) {
|
| 53 |
+
const label = model.config.id2label[id];
|
| 54 |
+
const [x, y, point_score] = points[id];
|
| 55 |
+
if (point_score < point_threshold) continue;
|
| 56 |
+
console.log(` - ${label}: (${(x * xScale).toFixed(2)}, ${(y * yScale).toFixed(2)}) with score ${point_score.toFixed(3)}`);
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
<detail>
|
| 62 |
+
|
| 63 |
+
<summary>See example output</summary>
|
| 64 |
+
|
| 65 |
+
```
|
| 66 |
+
Found person at [275.55, 99.07, 433.03, 753.93] with score 0.989
|
| 67 |
+
- nose: (348.04, 178.71) with score 0.686
|
| 68 |
+
- left_eye: (357.65, 166.47) with score 0.347
|
| 69 |
+
- right_eye: (341.15, 164.56) with score 0.331
|
| 70 |
+
- left_shoulder: (375.16, 258.75) with score 0.999
|
| 71 |
+
- right_shoulder: (312.26, 241.00) with score 0.999
|
| 72 |
+
- left_elbow: (381.83, 360.03) with score 0.999
|
| 73 |
+
- right_elbow: (291.49, 326.95) with score 0.999
|
| 74 |
+
- left_wrist: (399.93, 455.04) with score 0.997
|
| 75 |
+
- right_wrist: (335.69, 327.09) with score 0.999
|
| 76 |
+
- left_hip: (357.00, 457.75) with score 0.999
|
| 77 |
+
- right_hip: (317.68, 474.91) with score 0.998
|
| 78 |
+
- left_knee: (385.42, 547.58) with score 0.998
|
| 79 |
+
- right_knee: (339.32, 648.62) with score 0.953
|
| 80 |
+
- left_ankle: (384.14, 699.15) with score 0.990
|
| 81 |
+
- right_ankle: (357.63, 621.90) with score 0.906
|
| 82 |
+
Found person at [67.16, 23.39, 338.29, 783.70] with score 0.932
|
| 83 |
+
- left_shoulder: (157.14, 159.59) with score 0.973
|
| 84 |
+
- right_shoulder: (159.63, 144.00) with score 0.872
|
| 85 |
+
- left_elbow: (210.02, 242.90) with score 0.982
|
| 86 |
+
- right_elbow: (201.28, 231.61) with score 0.694
|
| 87 |
+
- left_wrist: (258.17, 330.94) with score 0.978
|
| 88 |
+
- right_wrist: (205.45, 305.74) with score 0.654
|
| 89 |
+
- left_hip: (231.90, 367.39) with score 0.999
|
| 90 |
+
- right_hip: (240.19, 365.36) with score 1.000
|
| 91 |
+
- left_knee: (195.22, 574.91) with score 0.999
|
| 92 |
+
- right_knee: (216.01, 578.60) with score 1.000
|
| 93 |
+
- left_ankle: (283.08, 528.39) with score 1.000
|
| 94 |
+
- right_ankle: (300.10, 733.43) with score 0.999
|
| 95 |
+
Found person at [-0.17, 75.70, 109.33, 555.50] with score 0.806
|
| 96 |
+
- nose: (49.95, 123.09) with score 0.385
|
| 97 |
+
- left_shoulder: (78.57, 171.93) with score 0.983
|
| 98 |
+
- right_shoulder: (30.76, 174.85) with score 0.974
|
| 99 |
+
- left_elbow: (91.19, 248.35) with score 0.859
|
| 100 |
+
- right_elbow: (19.49, 241.54) with score 0.877
|
| 101 |
+
- left_wrist: (93.19, 314.57) with score 0.812
|
| 102 |
+
- right_wrist: (6.06, 301.15) with score 0.899
|
| 103 |
+
- left_hip: (69.81, 313.63) with score 0.995
|
| 104 |
+
- right_hip: (41.66, 315.41) with score 0.997
|
| 105 |
+
- left_knee: (76.36, 416.84) with score 0.999
|
| 106 |
+
- right_knee: (44.06, 420.83) with score 0.998
|
| 107 |
+
- left_ankle: (83.66, 514.07) with score 0.992
|
| 108 |
+
- right_ankle: (44.09, 519.74) with score 0.983
|
| 109 |
+
Found person at [348.03, 53.31, 432.29, 526.36] with score 0.328
|
| 110 |
+
- left_shoulder: (395.69, 156.16) with score 1.000
|
| 111 |
+
- right_shoulder: (351.04, 157.42) with score 0.917
|
| 112 |
+
- left_elbow: (413.08, 225.16) with score 0.995
|
| 113 |
+
- left_wrist: (418.51, 295.31) with score 0.985
|
| 114 |
+
- left_hip: (387.03, 287.25) with score 0.995
|
| 115 |
+
- right_hip: (359.71, 286.18) with score 0.939
|
| 116 |
+
- left_knee: (389.06, 393.34) with score 0.992
|
| 117 |
+
- right_knee: (363.29, 393.47) with score 0.922
|
| 118 |
+
- left_ankle: (391.54, 482.10) with score 0.970
|
| 119 |
+
- right_ankle: (365.87, 483.47) with score 0.847
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
</detail>
|