Views
No views yet
npm i @huggingface/transformersonnx-community/vitpose-base-simple.1import { AutoModel, AutoImageProcessor, RawImage } from '@huggingface/transformers';
2
3// Load model and processor
4const model_id = 'onnx-community/vitpose-base-simple';
5const model = await AutoModel.from_pretrained(model_id);
6const processor = await AutoImageProcessor.from_pretrained(model_id);
7
8// Load image and prepare inputs
9const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ryan-gosling.jpg';
10const image = await RawImage.read(url);
11const inputs = await processor(image);
12
13// Predict heatmaps
14const { heatmaps } = await model(inputs);
15
16// Post-process heatmaps to get keypoints and scores
17const boxes = [[[0, 0, image.width, image.height]]];
18const results = processor.post_process_pose_estimation(heatmaps, boxes)[0][0];
19console.log(results);canvas library):1import { createCanvas, createImageData } from 'canvas';
2
3// Create canvas and draw image
4const canvas = createCanvas(image.width, image.height);
5const ctx = canvas.getContext('2d');
6const imageData = createImageData(image.rgba().data, image.width, image.height);
7ctx.putImageData(imageData, 0, 0);
8
9// Draw edges between keypoints
10const points = results.keypoints;
11ctx.lineWidth = 4;
12ctx.strokeStyle = 'blue';
13for (const [i, j] of model.config.edges) {
14 const [x1, y1] = points[i];
15 const [x2, y2] = points[j];
16 ctx.beginPath();
17 ctx.moveTo(x1, y1);
18 ctx.lineTo(x2, y2);
19 ctx.stroke();
20}
21
22// Draw circle at each keypoint
23ctx.fillStyle = 'red';
24for (const [x, y] of points) {
25 ctx.beginPath();
26 ctx.arc(x, y, 8, 0, 2 * Math.PI);
27 ctx.fill();
28}
29
30// Save image to file
31import fs from 'fs';
32const out = fs.createWriteStream('pose.png');
33const stream = canvas.createPNGStream();
34stream.pipe(out)
35out.on('finish', () => console.log('The PNG file was created.'));| Input image | Output image |
|---|---|
![]() | ![]() |
onnx).