Update README.md
Browse files
README.md
CHANGED
@@ -3,4 +3,191 @@ 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 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`.
|
18 |
+
|
19 |
+
```js
|
20 |
+
import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
|
21 |
+
|
22 |
+
// Load model and processor
|
23 |
+
const model_id = 'Xenova/yolov8x-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.7708740234375, 45.77457022666931, 644.4645690917969, 312.20427117347714] with score 0.697
|
127 |
+
- nose: (441.61, 87.47) with score 0.966
|
128 |
+
- left_eye: (449.36, 79.91) with score 0.988
|
129 |
+
- right_eye: (436.36, 79.56) with score 0.850
|
130 |
+
- left_ear: (462.02, 83.57) with score 0.919
|
131 |
+
- left_shoulder: (478.73, 127.16) with score 0.994
|
132 |
+
- right_shoulder: (420.37, 126.47) with score 0.703
|
133 |
+
- left_elbow: (503.33, 180.38) with score 0.977
|
134 |
+
- left_wrist: (506.53, 236.52) with score 0.924
|
135 |
+
- left_hip: (470.67, 223.60) with score 0.982
|
136 |
+
- right_hip: (432.32, 223.90) with score 0.851
|
137 |
+
- left_knee: (470.86, 306.20) with score 0.949
|
138 |
+
- right_knee: (428.56, 306.69) with score 0.601
|
139 |
+
- left_ankle: (463.92, 383.59) with score 0.737
|
140 |
+
Found person at [-0.06377220153808594, 61.59769003391266, 156.24676704406738, 370.5519897222519] with score 0.926
|
141 |
+
- nose: (59.61, 100.49) with score 0.979
|
142 |
+
- left_eye: (66.44, 96.11) with score 0.954
|
143 |
+
- right_eye: (55.82, 96.21) with score 0.908
|
144 |
+
- left_ear: (76.90, 98.52) with score 0.819
|
145 |
+
- right_ear: (49.82, 102.11) with score 0.571
|
146 |
+
- left_shoulder: (87.07, 135.82) with score 0.990
|
147 |
+
- right_shoulder: (36.53, 134.96) with score 0.987
|
148 |
+
- left_elbow: (102.21, 193.66) with score 0.970
|
149 |
+
- right_elbow: (24.85, 187.30) with score 0.947
|
150 |
+
- left_wrist: (110.61, 245.75) with score 0.962
|
151 |
+
- right_wrist: (6.28, 233.46) with score 0.939
|
152 |
+
- left_hip: (82.71, 230.04) with score 0.997
|
153 |
+
- right_hip: (48.15, 235.65) with score 0.995
|
154 |
+
- left_knee: (95.27, 321.57) with score 0.993
|
155 |
+
- right_knee: (52.73, 320.56) with score 0.991
|
156 |
+
- left_ankle: (100.90, 415.89) with score 0.948
|
157 |
+
- right_ankle: (56.65, 417.09) with score 0.942
|
158 |
+
Found person at [109.67742919921875, 12.466975402832032, 501.75636291503906, 533.3693368911744] with score 0.934
|
159 |
+
- nose: (126.43, 96.98) with score 0.715
|
160 |
+
- left_eye: (126.52, 88.36) with score 0.664
|
161 |
+
- left_ear: (136.92, 78.79) with score 0.934
|
162 |
+
- left_shoulder: (191.69, 125.31) with score 0.998
|
163 |
+
- right_shoulder: (166.08, 138.95) with score 0.993
|
164 |
+
- left_elbow: (254.38, 194.23) with score 0.997
|
165 |
+
- right_elbow: (186.09, 258.25) with score 0.986
|
166 |
+
- left_wrist: (309.75, 260.93) with score 0.990
|
167 |
+
- right_wrist: (133.20, 283.14) with score 0.973
|
168 |
+
- left_hip: (281.07, 280.72) with score 1.000
|
169 |
+
- right_hip: (258.20, 300.47) with score 1.000
|
170 |
+
- left_knee: (228.48, 442.67) with score 0.999
|
171 |
+
- right_knee: (250.90, 474.40) with score 0.999
|
172 |
+
- left_ankle: (343.96, 435.26) with score 0.979
|
173 |
+
- right_ankle: (340.41, 601.64) with score 0.971
|
174 |
+
Found person at [422.38683700561523, 67.97338972091676, 638.0375099182129, 493.7016093254089] with score 0.932
|
175 |
+
- nose: (417.60, 144.74) with score 0.989
|
176 |
+
- left_eye: (426.67, 134.88) with score 0.959
|
177 |
+
- right_eye: (410.81, 135.93) with score 0.952
|
178 |
+
- left_ear: (443.39, 137.08) with score 0.771
|
179 |
+
- right_ear: (400.11, 142.05) with score 0.753
|
180 |
+
- left_shoulder: (446.92, 202.43) with score 0.997
|
181 |
+
- right_shoulder: (374.31, 196.36) with score 0.993
|
182 |
+
- left_elbow: (458.77, 287.40) with score 0.990
|
183 |
+
- right_elbow: (355.46, 260.60) with score 0.971
|
184 |
+
- left_wrist: (488.87, 354.68) with score 0.984
|
185 |
+
- right_wrist: (402.03, 263.57) with score 0.978
|
186 |
+
- left_hip: (432.69, 349.58) with score 0.998
|
187 |
+
- right_hip: (381.51, 366.30) with score 0.996
|
188 |
+
- left_knee: (463.97, 447.94) with score 0.991
|
189 |
+
- right_knee: (403.90, 511.95) with score 0.978
|
190 |
+
- left_ankle: (450.14, 562.29) with score 0.889
|
191 |
+
- right_ankle: (436.81, 548.29) with score 0.759
|
192 |
+
```
|
193 |
+
</details>
|