Xenova HF staff commited on
Commit
4fc37fa
1 Parent(s): bdcc393

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +193 -1
README.md CHANGED
@@ -3,4 +3,196 @@ 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
+ YOLOv8s-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/yolov8s-pose`.
18
+
19
+ ```js
20
+ import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';
21
+
22
+ // Load model and processor
23
+ const model_id = 'Xenova/yolov8s-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 [533.1403350830078, 39.96531672477722, 645.8853149414062, 296.1657429695129] with score 0.739
127
+ - nose: (443.99, 91.98) with score 0.970
128
+ - left_eye: (449.84, 85.01) with score 0.968
129
+ - right_eye: (436.28, 86.54) with score 0.839
130
+ - left_ear: (458.69, 87.08) with score 0.822
131
+ - right_ear: (427.88, 89.20) with score 0.317
132
+ - left_shoulder: (471.29, 128.05) with score 0.991
133
+ - right_shoulder: (421.84, 127.22) with score 0.788
134
+ - left_elbow: (494.03, 174.09) with score 0.976
135
+ - right_elbow: (405.83, 162.81) with score 0.367
136
+ - left_wrist: (505.29, 232.06) with score 0.955
137
+ - right_wrist: (411.89, 213.05) with score 0.470
138
+ - left_hip: (469.48, 217.49) with score 0.978
139
+ - right_hip: (438.79, 216.48) with score 0.901
140
+ - left_knee: (474.03, 283.00) with score 0.957
141
+ - right_knee: (448.00, 287.90) with score 0.808
142
+ - left_ankle: (472.06, 339.67) with score 0.815
143
+ - right_ankle: (447.15, 340.44) with score 0.576
144
+ Found person at [0.03232002258300781, 57.89646775722503, 156.35095596313477, 370.9132190942764] with score 0.908
145
+ - nose: (60.48, 105.82) with score 0.975
146
+ - left_eye: (64.86, 100.59) with score 0.952
147
+ - right_eye: (55.12, 100.60) with score 0.855
148
+ - left_ear: (73.04, 101.96) with score 0.820
149
+ - right_ear: (51.07, 103.28) with score 0.482
150
+ - left_shoulder: (85.74, 137.77) with score 0.996
151
+ - right_shoulder: (42.04, 137.63) with score 0.988
152
+ - left_elbow: (101.10, 190.45) with score 0.988
153
+ - right_elbow: (25.75, 186.44) with score 0.937
154
+ - left_wrist: (115.93, 250.05) with score 0.975
155
+ - right_wrist: (7.39, 233.44) with score 0.918
156
+ - left_hip: (80.15, 242.20) with score 0.999
157
+ - right_hip: (52.69, 239.82) with score 0.999
158
+ - left_knee: (93.29, 326.00) with score 0.999
159
+ - right_knee: (57.42, 329.04) with score 0.998
160
+ - left_ankle: (100.24, 413.83) with score 0.992
161
+ - right_ankle: (50.47, 417.93) with score 0.988
162
+ Found person at [106.16920471191406, 8.419264698028565, 515.0135803222656, 530.6886708259583] with score 0.819
163
+ - nose: (134.03, 111.15) with score 0.921
164
+ - left_eye: (137.51, 100.95) with score 0.824
165
+ - right_eye: (131.82, 97.53) with score 0.489
166
+ - left_ear: (147.19, 92.96) with score 0.792
167
+ - left_shoulder: (188.28, 127.51) with score 0.993
168
+ - right_shoulder: (181.81, 149.32) with score 0.995
169
+ - left_elbow: (258.49, 199.10) with score 0.984
170
+ - right_elbow: (181.43, 251.27) with score 0.988
171
+ - left_wrist: (311.74, 257.93) with score 0.979
172
+ - right_wrist: (129.68, 284.38) with score 0.984
173
+ - left_hip: (267.43, 299.85) with score 1.000
174
+ - right_hip: (277.05, 307.50) with score 1.000
175
+ - left_knee: (232.15, 427.54) with score 0.999
176
+ - right_knee: (278.99, 453.09) with score 0.999
177
+ - left_ankle: (352.68, 457.89) with score 0.990
178
+ - right_ankle: (362.15, 554.69) with score 0.993
179
+ Found person at [425.3855133056641, 73.76281919479369, 640.6651306152344, 502.32841634750366] with score 0.876
180
+ - nose: (416.15, 149.68) with score 0.996
181
+ - left_eye: (430.34, 139.56) with score 0.984
182
+ - right_eye: (412.88, 142.56) with score 0.976
183
+ - left_ear: (446.59, 142.21) with score 0.843
184
+ - right_ear: (398.82, 144.52) with score 0.740
185
+ - left_shoulder: (436.54, 197.92) with score 0.999
186
+ - right_shoulder: (362.94, 210.20) with score 0.996
187
+ - left_elbow: (460.06, 293.80) with score 0.992
188
+ - right_elbow: (352.33, 262.09) with score 0.966
189
+ - left_wrist: (491.33, 364.20) with score 0.986
190
+ - right_wrist: (402.62, 272.23) with score 0.956
191
+ - left_hip: (429.79, 354.94) with score 0.999
192
+ - right_hip: (383.27, 372.77) with score 0.999
193
+ - left_knee: (461.07, 437.73) with score 0.998
194
+ - right_knee: (410.89, 522.05) with score 0.995
195
+ - left_ankle: (460.74, 552.53) with score 0.966
196
+ - right_ankle: (429.00, 560.54) with score 0.940
197
+ ```
198
+ </details>