Update README.md
Browse files
README.md
CHANGED
@@ -6,4 +6,80 @@ pipeline_tag: keypoint-detection
|
|
6 |
|
7 |
https://huggingface.co/nielsr/vitpose-base-simple with ONNX weights to be compatible with Transformers.js.
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [🤗 Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).
|
|
|
6 |
|
7 |
https://huggingface.co/nielsr/vitpose-base-simple with ONNX weights to be compatible with Transformers.js.
|
8 |
|
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/@huggingface/transformers) using:
|
13 |
+
```bash
|
14 |
+
npm i @huggingface/transformers
|
15 |
+
```
|
16 |
+
|
17 |
+
**Example:** Pose estimation w/ `onnx-community/vitpose-base-simple`.
|
18 |
+
```js
|
19 |
+
import { AutoModel, AutoImageProcessor, RawImage } from '@huggingface/transformers';
|
20 |
+
|
21 |
+
// Load model and processor
|
22 |
+
const model_id = 'onnx-community/vitpose-base-simple';
|
23 |
+
const model = await AutoModel.from_pretrained(model_id);
|
24 |
+
const processor = await AutoImageProcessor.from_pretrained(model_id);
|
25 |
+
|
26 |
+
// Load image and prepare inputs
|
27 |
+
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ryan-gosling.jpg';
|
28 |
+
const image = await RawImage.read(url);
|
29 |
+
const inputs = await processor(image);
|
30 |
+
|
31 |
+
// Predict heatmaps
|
32 |
+
const { heatmaps } = await model(inputs);
|
33 |
+
|
34 |
+
// Post-process heatmaps to get keypoints and scores
|
35 |
+
const boxes = [[[0, 0, image.width, image.height]]];
|
36 |
+
const results = processor.post_process_pose_estimation(heatmaps, boxes)[0][0];
|
37 |
+
console.log(results);
|
38 |
+
```
|
39 |
+
|
40 |
+
Optionally, visualize the outputs (Node.js usage shown here, using the [`canvas`](https://www.npmjs.com/package/canvas) library):
|
41 |
+
```js
|
42 |
+
import { createCanvas, createImageData } from 'canvas';
|
43 |
+
|
44 |
+
// Create canvas and draw image
|
45 |
+
const canvas = createCanvas(image.width, image.height);
|
46 |
+
const ctx = canvas.getContext('2d');
|
47 |
+
const imageData = createImageData(image.rgba().data, image.width, image.height);
|
48 |
+
ctx.putImageData(imageData, 0, 0);
|
49 |
+
|
50 |
+
// Draw edges between keypoints
|
51 |
+
const points = results.keypoints;
|
52 |
+
ctx.lineWidth = 4;
|
53 |
+
ctx.strokeStyle = 'blue';
|
54 |
+
for (const [i, j] of model.config.edges) {
|
55 |
+
const [x1, y1] = points[i];
|
56 |
+
const [x2, y2] = points[j];
|
57 |
+
ctx.beginPath();
|
58 |
+
ctx.moveTo(x1, y1);
|
59 |
+
ctx.lineTo(x2, y2);
|
60 |
+
ctx.stroke();
|
61 |
+
}
|
62 |
+
|
63 |
+
// Draw circle at each keypoint
|
64 |
+
ctx.fillStyle = 'red';
|
65 |
+
for (const [x, y] of points) {
|
66 |
+
ctx.beginPath();
|
67 |
+
ctx.arc(x, y, 8, 0, 2 * Math.PI);
|
68 |
+
ctx.fill();
|
69 |
+
}
|
70 |
+
|
71 |
+
// Save image to file
|
72 |
+
import fs from 'fs';
|
73 |
+
const out = fs.createWriteStream('pose.png');
|
74 |
+
const stream = canvas.createPNGStream();
|
75 |
+
stream.pipe(out)
|
76 |
+
out.on('finish', () => console.log('The PNG file was created.'));
|
77 |
+
```
|
78 |
+
|
79 |
+
| Input image | Output image |
|
80 |
+
| :----------:|:------------:|
|
81 |
+
| ![image/jpeg](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/QpXlLNyLDKZUxXjokbUyy.jpeg) | ![image/png](https://cdn-uploads.huggingface.co/production/uploads/61b253b7ac5ecaae3d1efe0c/xj0jaKo9aAOux-NSU8U7S.png) |
|
82 |
+
|
83 |
+
---
|
84 |
+
|
85 |
Note: Having a separate repo for ONNX weights is intended to be a temporary solution until WebML gains more traction. If you would like to make your models web-ready, we recommend converting to ONNX using [🤗 Optimum](https://huggingface.co/docs/optimum/index) and structuring your repo like this one (with ONNX weights located in a subfolder named `onnx`).
|