Update README.md
Browse files
README.md
CHANGED
@@ -3,4 +3,192 @@ library_name: transformers.js
|
|
3 |
tags:
|
4 |
- pose-estimation
|
5 |
license: agpl-3.0
|
6 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
tags:
|
4 |
- pose-estimation
|
5 |
license: agpl-3.0
|
6 |
+
---
|
7 |
+
|
8 |
+
YOLOv8x-pose-p6 with ONNX weights to be compatible with Transformers.js.
|
9 |
+
|
10 |
+
## Usage (Transformers.js)
|
11 |
+
|
12 |
+
If you haven't already, you can install the [Transformers.js](https://huggingface.co/docs/transformers.js) JavaScript library from [NPM](https://www.npmjs.com/package/@xenova/transformers) using:
|
13 |
+
```bash
|
14 |
+
npm i @xenova/transformers
|
15 |
+
```
|
16 |
+
|
17 |
+
**Example:** Perform pose-estimation w/ `Xenova/yolov8x-pose-p6`.
|
18 |
+
|
19 |
+
```js
|
20 |
+
import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
|
21 |
+
|
22 |
+
// Load model and processor
|
23 |
+
const model_id = 'Xenova/yolov8x-pose-p6';
|
24 |
+
const model = await AutoModel.from_pretrained(model_id);
|
25 |
+
const processor = await AutoProcessor.from_pretrained(model_id);
|
26 |
+
|
27 |
+
// Read image and run processor
|
28 |
+
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg';
|
29 |
+
const image = await RawImage.read(url);
|
30 |
+
const { pixel_values } = await processor(image);
|
31 |
+
|
32 |
+
// Set thresholds
|
33 |
+
const threshold = 0.3; // Remove detections with low confidence
|
34 |
+
const iouThreshold = 0.5; // Used to remove duplicates
|
35 |
+
const pointThreshold = 0.3; // Hide uncertain points
|
36 |
+
|
37 |
+
// Predict bounding boxes and keypoints
|
38 |
+
const { output0 } = await model({ images: pixel_values });
|
39 |
+
|
40 |
+
// Post-process:
|
41 |
+
const permuted = output0[0].transpose(1, 0);
|
42 |
+
// `permuted` is a Tensor of shape [ 8400, 56 ]:
|
43 |
+
// - 8400 potential detections
|
44 |
+
// - 56 parameters for each box:
|
45 |
+
// - 4 for the bounding box dimensions (x-center, y-center, width, height)
|
46 |
+
// - 1 for the confidence score
|
47 |
+
// - 17 * 3 = 51 for the pose keypoints: 17 labels, each with (x, y, visibilitiy)
|
48 |
+
|
49 |
+
// Example code to format it nicely:
|
50 |
+
const results = [];
|
51 |
+
const [scaledHeight, scaledWidth] = pixel_values.dims.slice(-2);
|
52 |
+
for (const [xc, yc, w, h, score, ...keypoints] of permuted.tolist()) {
|
53 |
+
if (score < threshold) continue;
|
54 |
+
|
55 |
+
// Get pixel values, taking into account the original image size
|
56 |
+
const x1 = (xc - w / 2) / scaledWidth * image.width;
|
57 |
+
const y1 = (yc - h / 2) / scaledHeight * image.height;
|
58 |
+
const x2 = (xc + w / 2) / scaledWidth * image.width;
|
59 |
+
const y2 = (yc + h / 2) / scaledHeight * image.height;
|
60 |
+
results.push({ x1, x2, y1, y2, score, keypoints })
|
61 |
+
}
|
62 |
+
|
63 |
+
|
64 |
+
// Define helper functions
|
65 |
+
function removeDuplicates(detections, iouThreshold) {
|
66 |
+
const filteredDetections = [];
|
67 |
+
|
68 |
+
for (const detection of detections) {
|
69 |
+
let isDuplicate = false;
|
70 |
+
let duplicateIndex = -1;
|
71 |
+
let maxIoU = 0;
|
72 |
+
|
73 |
+
for (let i = 0; i < filteredDetections.length; ++i) {
|
74 |
+
const filteredDetection = filteredDetections[i];
|
75 |
+
const iou = calculateIoU(detection, filteredDetection);
|
76 |
+
if (iou > iouThreshold) {
|
77 |
+
isDuplicate = true;
|
78 |
+
if (iou > maxIoU) {
|
79 |
+
maxIoU = iou;
|
80 |
+
duplicateIndex = i;
|
81 |
+
}
|
82 |
+
}
|
83 |
+
}
|
84 |
+
|
85 |
+
if (!isDuplicate) {
|
86 |
+
filteredDetections.push(detection);
|
87 |
+
} else if (duplicateIndex !== -1 && detection.score > filteredDetections[duplicateIndex].score) {
|
88 |
+
filteredDetections[duplicateIndex] = detection;
|
89 |
+
}
|
90 |
+
}
|
91 |
+
|
92 |
+
return filteredDetections;
|
93 |
+
}
|
94 |
+
|
95 |
+
function calculateIoU(detection1, detection2) {
|
96 |
+
const xOverlap = Math.max(0, Math.min(detection1.x2, detection2.x2) - Math.max(detection1.x1, detection2.x1));
|
97 |
+
const yOverlap = Math.max(0, Math.min(detection1.y2, detection2.y2) - Math.max(detection1.y1, detection2.y1));
|
98 |
+
const overlapArea = xOverlap * yOverlap;
|
99 |
+
|
100 |
+
const area1 = (detection1.x2 - detection1.x1) * (detection1.y2 - detection1.y1);
|
101 |
+
const area2 = (detection2.x2 - detection2.x1) * (detection2.y2 - detection2.y1);
|
102 |
+
const unionArea = area1 + area2 - overlapArea;
|
103 |
+
|
104 |
+
return overlapArea / unionArea;
|
105 |
+
}
|
106 |
+
|
107 |
+
const filteredResults = removeDuplicates(results, iouThreshold);
|
108 |
+
|
109 |
+
// Display results
|
110 |
+
for (const { x1, x2, y1, y2, score, keypoints } of filteredResults) {
|
111 |
+
console.log(`Found person at [${x1}, ${y1}, ${x2}, ${y2}] with score ${score.toFixed(3)}`)
|
112 |
+
for (let i = 0; i < keypoints.length; i += 3) {
|
113 |
+
const label = model.config.id2label[Math.floor(i / 3)];
|
114 |
+
const [x, y, point_score] = keypoints.slice(i, i + 3);
|
115 |
+
if (point_score < pointThreshold) continue;
|
116 |
+
console.log(` - ${label}: (${x.toFixed(2)}, ${y.toFixed(2)}) with score ${point_score.toFixed(3)}`);
|
117 |
+
}
|
118 |
+
}
|
119 |
+
```
|
120 |
+
|
121 |
+
<details>
|
122 |
+
|
123 |
+
<summary>See example output</summary>
|
124 |
+
|
125 |
+
```
|
126 |
+
Found person at [535.95703125, 43.12074284553528, 644.3259429931641, 337.3436294078827] with score 0.760
|
127 |
+
- nose: (885.58, 179.72) with score 0.975
|
128 |
+
- left_eye: (897.09, 165.24) with score 0.976
|
129 |
+
- right_eye: (874.85, 164.54) with score 0.851
|
130 |
+
- left_ear: (914.39, 169.48) with score 0.806
|
131 |
+
- left_shoulder: (947.49, 252.34) with score 0.996
|
132 |
+
- right_shoulder: (840.67, 244.42) with score 0.665
|
133 |
+
- left_elbow: (1001.36, 351.66) with score 0.983
|
134 |
+
- left_wrist: (1011.84, 472.31) with score 0.954
|
135 |
+
- left_hip: (931.52, 446.28) with score 0.986
|
136 |
+
- right_hip: (860.66, 442.87) with score 0.828
|
137 |
+
- left_knee: (930.67, 625.64) with score 0.979
|
138 |
+
- right_knee: (872.17, 620.36) with score 0.735
|
139 |
+
- left_ankle: (929.01, 772.34) with score 0.880
|
140 |
+
- right_ankle: (882.23, 778.68) with score 0.454
|
141 |
+
Found person at [0.4024791717529297, 59.50179467201233, 156.87244415283203, 370.64377751350406] with score 0.853
|
142 |
+
- nose: (115.39, 198.06) with score 0.918
|
143 |
+
- left_eye: (120.26, 177.71) with score 0.830
|
144 |
+
- right_eye: (105.47, 179.69) with score 0.757
|
145 |
+
- left_ear: (144.87, 185.18) with score 0.711
|
146 |
+
- right_ear: (97.69, 188.45) with score 0.468
|
147 |
+
- left_shoulder: (178.03, 268.88) with score 0.975
|
148 |
+
- right_shoulder: (80.69, 273.99) with score 0.954
|
149 |
+
- left_elbow: (203.06, 383.33) with score 0.923
|
150 |
+
- right_elbow: (43.32, 376.35) with score 0.856
|
151 |
+
- left_wrist: (215.74, 504.02) with score 0.888
|
152 |
+
- right_wrist: (6.77, 462.65) with score 0.812
|
153 |
+
- left_hip: (165.70, 473.24) with score 0.990
|
154 |
+
- right_hip: (97.84, 471.69) with score 0.986
|
155 |
+
- left_knee: (183.26, 646.61) with score 0.991
|
156 |
+
- right_knee: (104.04, 651.17) with score 0.989
|
157 |
+
- left_ankle: (199.88, 823.24) with score 0.966
|
158 |
+
- right_ankle: (104.66, 827.66) with score 0.963
|
159 |
+
Found person at [107.49130249023438, 12.557352638244629, 501.3542175292969, 527.4827188491821] with score 0.872
|
160 |
+
- nose: (246.06, 180.81) with score 0.722
|
161 |
+
- left_eye: (236.99, 148.85) with score 0.523
|
162 |
+
- left_ear: (289.26, 152.23) with score 0.770
|
163 |
+
- left_shoulder: (391.63, 256.55) with score 0.992
|
164 |
+
- right_shoulder: (363.28, 294.56) with score 0.979
|
165 |
+
- left_elbow: (514.37, 404.61) with score 0.990
|
166 |
+
- right_elbow: (353.58, 523.61) with score 0.957
|
167 |
+
- left_wrist: (607.64, 530.43) with score 0.985
|
168 |
+
- right_wrist: (246.78, 536.33) with score 0.950
|
169 |
+
- left_hip: (563.45, 577.89) with score 0.998
|
170 |
+
- right_hip: (544.08, 613.29) with score 0.997
|
171 |
+
- left_knee: (466.57, 862.51) with score 0.996
|
172 |
+
- right_knee: (518.49, 977.99) with score 0.996
|
173 |
+
- left_ankle: (691.56, 844.49) with score 0.960
|
174 |
+
- right_ankle: (671.32, 1100.90) with score 0.953
|
175 |
+
Found person at [424.73594665527344, 68.82870757579803, 640.3419494628906, 492.8904126405716] with score 0.887
|
176 |
+
- nose: (840.26, 289.19) with score 0.991
|
177 |
+
- left_eye: (851.23, 259.92) with score 0.956
|
178 |
+
- right_eye: (823.10, 256.35) with score 0.955
|
179 |
+
- left_ear: (889.52, 278.10) with score 0.668
|
180 |
+
- right_ear: (799.80, 264.64) with score 0.771
|
181 |
+
- left_shoulder: (903.87, 398.65) with score 0.997
|
182 |
+
- right_shoulder: (743.88, 403.37) with score 0.988
|
183 |
+
- left_elbow: (921.63, 589.83) with score 0.989
|
184 |
+
- right_elbow: (699.56, 527.09) with score 0.934
|
185 |
+
- left_wrist: (959.21, 728.84) with score 0.984
|
186 |
+
- right_wrist: (790.88, 519.34) with score 0.945
|
187 |
+
- left_hip: (873.51, 720.07) with score 0.996
|
188 |
+
- right_hip: (762.29, 760.91) with score 0.990
|
189 |
+
- left_knee: (945.33, 841.65) with score 0.987
|
190 |
+
- right_knee: (813.06, 1072.57) with score 0.964
|
191 |
+
- left_ankle: (918.48, 1129.20) with score 0.871
|
192 |
+
- right_ankle: (886.91, 1053.95) with score 0.716
|
193 |
+
```
|
194 |
+
</details>
|