🚀 transformers.js
This project adapts the ONNX weights of the nielsr/vitpose - base - simple model to be compatible with Transformers.js, enabling keypoint detection tasks.
🚀 Quick Start
The model at https://huggingface.co/nielsr/vitpose-base-simple with ONNX weights is made compatible with Transformers.js.
📦 Installation
If you haven't already, you can install the Transformers.js JavaScript library from NPM using:
npm i @huggingface/transformers
💻 Usage Examples
Basic Usage
Example: Pose estimation w/ onnx-community/vitpose-base-simple
.
import { AutoModel, AutoImageProcessor, RawImage } from '@huggingface/transformers';
const model_id = 'onnx-community/vitpose-base-simple';
const model = await AutoModel.from_pretrained(model_id);
const processor = await AutoImageProcessor.from_pretrained(model_id);
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ryan-gosling.jpg';
const image = await RawImage.read(url);
const inputs = await processor(image);
const { heatmaps } = await model(inputs);
const boxes = [[[0, 0, image.width, image.height]]];
const results = processor.post_process_pose_estimation(heatmaps, boxes)[0][0];
console.log(results);
Advanced Usage
Optionally, visualize the outputs (Node.js usage shown here, using the canvas
library):
import { createCanvas, createImageData } from 'canvas';
const canvas = createCanvas(image.width, image.height);
const ctx = canvas.getContext('2d');
const imageData = createImageData(image.rgba().data, image.width, image.height);
ctx.putImageData(imageData, 0, 0);
const points = results.keypoints;
ctx.lineWidth = 4;
ctx.strokeStyle = 'blue';
for (const [i, j] of model.config.edges) {
const [x1, y1] = points[i];
const [x2, y2] = points[j];
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
ctx.fillStyle = 'red';
for (const [x, y] of points) {
ctx.beginPath();
ctx.arc(x, y, 8, 0, 2 * Math.PI);
ctx.fill();
}
import fs from 'fs';
const out = fs.createWriteStream('pose.png');
const stream = canvas.createPNGStream();
stream.pipe(out)
out.on('finish', () => console.log('The PNG file was created.'));
Input image |
Output image |
 |
 |
⚠️ Important 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 and structuring your repo like this one (with ONNX weights located in a subfolder named onnx
).