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 |
+
YOLOv8m-pose 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/yolov8m-pose`.
|
18 |
+
|
19 |
+
```js
|
20 |
+
import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
|
21 |
+
|
22 |
+
// Load model and processor
|
23 |
+
const model_id = 'Xenova/yolov8m-pose';
|
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.503101348877, 39.878777217864986, 644.8351860046387, 346.3689248085022] with score 0.655
|
127 |
+
- nose: (444.86, 91.25) with score 0.912
|
128 |
+
- left_eye: (449.55, 79.71) with score 0.912
|
129 |
+
- right_eye: (436.53, 82.54) with score 0.689
|
130 |
+
- left_ear: (457.66, 83.08) with score 0.774
|
131 |
+
- left_shoulder: (476.25, 126.43) with score 0.984
|
132 |
+
- right_shoulder: (419.05, 129.94) with score 0.675
|
133 |
+
- left_elbow: (495.99, 180.55) with score 0.960
|
134 |
+
- left_wrist: (504.15, 233.96) with score 0.888
|
135 |
+
- left_hip: (469.08, 227.61) with score 0.961
|
136 |
+
- right_hip: (428.82, 228.95) with score 0.821
|
137 |
+
- left_knee: (474.97, 301.15) with score 0.919
|
138 |
+
- right_knee: (434.24, 305.24) with score 0.704
|
139 |
+
- left_ankle: (467.31, 384.83) with score 0.625
|
140 |
+
- right_ankle: (439.09, 379.35) with score 0.378
|
141 |
+
Found person at [-0.08985519409179688, 56.876064038276674, 158.62728118896484, 371.25909755229947] with score 0.902
|
142 |
+
- nose: (61.15, 102.21) with score 0.979
|
143 |
+
- left_eye: (66.59, 91.92) with score 0.939
|
144 |
+
- right_eye: (51.35, 95.02) with score 0.905
|
145 |
+
- left_ear: (70.82, 97.11) with score 0.778
|
146 |
+
- right_ear: (48.08, 97.46) with score 0.655
|
147 |
+
- left_shoulder: (84.60, 139.95) with score 0.997
|
148 |
+
- right_shoulder: (38.36, 139.32) with score 0.996
|
149 |
+
- left_elbow: (98.25, 196.80) with score 0.990
|
150 |
+
- right_elbow: (24.83, 188.15) with score 0.981
|
151 |
+
- left_wrist: (103.38, 252.91) with score 0.977
|
152 |
+
- right_wrist: (9.42, 233.04) with score 0.965
|
153 |
+
- left_hip: (82.91, 247.50) with score 0.999
|
154 |
+
- right_hip: (51.28, 248.31) with score 0.999
|
155 |
+
- left_knee: (85.25, 326.65) with score 0.997
|
156 |
+
- right_knee: (49.12, 330.50) with score 0.996
|
157 |
+
- left_ankle: (96.84, 419.45) with score 0.964
|
158 |
+
- right_ankle: (51.88, 416.89) with score 0.960
|
159 |
+
Found person at [109.41852569580077, 13.203005981445314, 505.06954193115234, 532.9905454635621] with score 0.911
|
160 |
+
- nose: (126.16, 102.84) with score 0.586
|
161 |
+
- left_eye: (125.44, 84.07) with score 0.352
|
162 |
+
- left_ear: (137.38, 77.79) with score 0.722
|
163 |
+
- left_shoulder: (181.75, 122.32) with score 0.997
|
164 |
+
- right_shoulder: (180.20, 152.15) with score 0.998
|
165 |
+
- left_elbow: (262.31, 202.36) with score 0.996
|
166 |
+
- right_elbow: (194.94, 277.60) with score 0.997
|
167 |
+
- left_wrist: (298.87, 269.32) with score 0.987
|
168 |
+
- right_wrist: (132.86, 281.44) with score 0.990
|
169 |
+
- left_hip: (272.70, 284.47) with score 1.000
|
170 |
+
- right_hip: (274.35, 307.48) with score 1.000
|
171 |
+
- left_knee: (247.66, 441.74) with score 0.997
|
172 |
+
- right_knee: (256.27, 500.82) with score 0.998
|
173 |
+
- left_ankle: (340.54, 455.33) with score 0.848
|
174 |
+
- right_ankle: (338.54, 543.24) with score 0.882
|
175 |
+
Found person at [425.35156250000006, 68.73829221725464, 640.3047943115234, 494.19192361831665] with score 0.901
|
176 |
+
- nose: (425.40, 147.53) with score 0.995
|
177 |
+
- left_eye: (432.33, 133.12) with score 0.985
|
178 |
+
- right_eye: (410.70, 135.98) with score 0.969
|
179 |
+
- left_ear: (440.72, 134.14) with score 0.901
|
180 |
+
- right_ear: (400.69, 134.89) with score 0.800
|
181 |
+
- left_shoulder: (455.11, 201.19) with score 1.000
|
182 |
+
- right_shoulder: (368.64, 201.60) with score 0.999
|
183 |
+
- left_elbow: (455.25, 292.03) with score 0.998
|
184 |
+
- right_elbow: (350.65, 258.24) with score 0.989
|
185 |
+
- left_wrist: (475.06, 370.36) with score 0.992
|
186 |
+
- right_wrist: (398.78, 263.84) with score 0.975
|
187 |
+
- left_hip: (441.94, 359.78) with score 1.000
|
188 |
+
- right_hip: (384.06, 368.70) with score 1.000
|
189 |
+
- left_knee: (462.74, 452.41) with score 0.998
|
190 |
+
- right_knee: (395.50, 488.42) with score 0.997
|
191 |
+
- left_ankle: (465.12, 540.38) with score 0.960
|
192 |
+
- right_ankle: (433.43, 569.37) with score 0.938
|
193 |
+
```
|
194 |
+
</details>
|