yolov8l-pose / README.md
Xenova's picture
Xenova HF staff
Update README.md
4367212 verified
metadata
library_name: transformers.js
tags:
  - pose-estimation
license: agpl-3.0

YOLOv8l-pose with ONNX weights to be compatible with Transformers.js.

Usage (Transformers.js)

If you haven't already, you can install the Transformers.js JavaScript library from NPM using:

npm i @xenova/transformers

Example: Perform pose-estimation w/ Xenova/yolov8l-pose.

import { AutoModel, AutoProcessor, RawImage } from '@xenova/transformers';

// Load model and processor
const model_id = 'Xenova/yolov8l-pose';
const model = await AutoModel.from_pretrained(model_id);
const processor = await AutoProcessor.from_pretrained(model_id);

// Read image and run processor
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg';
const image = await RawImage.read(url);
const { pixel_values } = await processor(image);

// Set thresholds
const threshold = 0.3; // Remove detections with low confidence
const iouThreshold = 0.5; // Used to remove duplicates
const pointThreshold = 0.3; // Hide uncertain points

// Predict bounding boxes and keypoints
const { output0 } = await model({ images: pixel_values });

// Post-process:
const permuted = output0[0].transpose(1, 0);
// `permuted` is a Tensor of shape [ 8400, 56 ]:
// - 8400 potential detections
// - 56 parameters for each box:
//   - 4 for the bounding box dimensions (x-center, y-center, width, height)
//   - 1 for the confidence score
//   - 17 * 3 = 51 for the pose keypoints: 17 labels, each with (x, y, visibilitiy)

// Example code to format it nicely:
const results = [];
const [scaledHeight, scaledWidth] = pixel_values.dims.slice(-2);
for (const [xc, yc, w, h, score, ...keypoints] of permuted.tolist()) {
    if (score < threshold) continue;

    // Get pixel values, taking into account the original image size
    const x1 = (xc - w / 2) / scaledWidth * image.width;
    const y1 = (yc - h / 2) / scaledHeight * image.height;
    const x2 = (xc + w / 2) / scaledWidth * image.width;
    const y2 = (yc + h / 2) / scaledHeight * image.height;
    results.push({ x1, x2, y1, y2, score, keypoints })
}


// Define helper functions
function removeDuplicates(detections, iouThreshold) {
    const filteredDetections = [];

    for (const detection of detections) {
        let isDuplicate = false;
        let duplicateIndex = -1;
        let maxIoU = 0;

        for (let i = 0; i < filteredDetections.length; ++i) {
            const filteredDetection = filteredDetections[i];
            const iou = calculateIoU(detection, filteredDetection);
            if (iou > iouThreshold) {
                isDuplicate = true;
                if (iou > maxIoU) {
                    maxIoU = iou;
                    duplicateIndex = i;
                }
            }
        }

        if (!isDuplicate) {
            filteredDetections.push(detection);
        } else if (duplicateIndex !== -1 && detection.score > filteredDetections[duplicateIndex].score) {
            filteredDetections[duplicateIndex] = detection;
        }
    }

    return filteredDetections;
}

function calculateIoU(detection1, detection2) {
    const xOverlap = Math.max(0, Math.min(detection1.x2, detection2.x2) - Math.max(detection1.x1, detection2.x1));
    const yOverlap = Math.max(0, Math.min(detection1.y2, detection2.y2) - Math.max(detection1.y1, detection2.y1));
    const overlapArea = xOverlap * yOverlap;

    const area1 = (detection1.x2 - detection1.x1) * (detection1.y2 - detection1.y1);
    const area2 = (detection2.x2 - detection2.x1) * (detection2.y2 - detection2.y1);
    const unionArea = area1 + area2 - overlapArea;

    return overlapArea / unionArea;
}

const filteredResults = removeDuplicates(results, iouThreshold);

// Display results
for (const { x1, x2, y1, y2, score, keypoints } of filteredResults) {
    console.log(`Found person at [${x1}, ${y1}, ${x2}, ${y2}] with score ${score.toFixed(3)}`)
    for (let i = 0; i < keypoints.length; i += 3) {
        const label = model.config.id2label[Math.floor(i / 3)];
        const [x, y, point_score] = keypoints.slice(i, i + 3);
        if (point_score < pointThreshold) continue;
        console.log(`  - ${label}: (${x.toFixed(2)}, ${y.toFixed(2)}) with score ${point_score.toFixed(3)}`);
    }
}
See example output
Found person at [539.2378807067871, 41.92433733940124, 642.9805946350098, 334.98332471847533] with score 0.727
  - nose: (445.67, 84.43) with score 0.976
  - left_eye: (451.88, 76.89) with score 0.983
  - right_eye: (440.39, 76.33) with score 0.888
  - left_ear: (463.89, 81.68) with score 0.837
  - left_shoulder: (478.95, 123.91) with score 0.993
  - right_shoulder: (419.52, 123.44) with score 0.694
  - left_elbow: (501.07, 180.46) with score 0.979
  - left_wrist: (504.60, 238.34) with score 0.950
  - left_hip: (469.53, 220.77) with score 0.985
  - right_hip: (431.21, 222.54) with score 0.875
  - left_knee: (473.45, 302.16) with score 0.972
  - right_knee: (432.61, 302.91) with score 0.759
  - left_ankle: (467.74, 380.37) with score 0.874
  - right_ankle: (438.06, 381.94) with score 0.516
Found person at [0.59722900390625, 59.435689163208, 157.59026527404785, 370.3985949516296] with score 0.927
  - nose: (56.99, 100.53) with score 0.959
  - left_eye: (63.46, 94.19) with score 0.930
  - right_eye: (51.11, 96.48) with score 0.846
  - left_ear: (73.43, 97.84) with score 0.798
  - right_ear: (46.36, 99.41) with score 0.484
  - left_shoulder: (84.93, 134.17) with score 0.988
  - right_shoulder: (41.60, 133.96) with score 0.976
  - left_elbow: (96.33, 189.89) with score 0.959
  - right_elbow: (24.60, 192.73) with score 0.879
  - left_wrist: (104.79, 258.62) with score 0.928
  - right_wrist: (7.89, 238.55) with score 0.830
  - left_hip: (83.23, 234.45) with score 0.993
  - right_hip: (53.89, 235.50) with score 0.991
  - left_knee: (87.80, 326.73) with score 0.988
  - right_knee: (49.44, 327.89) with score 0.982
  - left_ankle: (100.93, 416.88) with score 0.925
  - right_ankle: (44.52, 421.24) with score 0.912
Found person at [112.88127899169922, 13.998864459991454, 504.09095764160156, 533.4011061668397] with score 0.943
  - nose: (122.64, 98.36) with score 0.366
  - left_ear: (132.43, 77.58) with score 0.794
  - left_shoulder: (196.67, 124.78) with score 0.999
  - right_shoulder: (176.97, 142.00) with score 0.998
  - left_elbow: (256.79, 196.00) with score 0.998
  - right_elbow: (182.85, 279.47) with score 0.994
  - left_wrist: (305.44, 270.10) with score 0.982
  - right_wrist: (129.72, 281.09) with score 0.963
  - left_hip: (275.59, 290.38) with score 1.000
  - right_hip: (263.91, 310.60) with score 1.000
  - left_knee: (237.89, 445.88) with score 0.998
  - right_knee: (249.66, 477.34) with score 0.998
  - left_ankle: (349.25, 438.70) with score 0.940
  - right_ankle: (338.20, 586.62) with score 0.935
Found person at [424.730339050293, 67.2046113729477, 639.5703506469727, 493.03533136844635] with score 0.944
  - nose: (416.55, 141.74) with score 0.991
  - left_eye: (428.51, 130.99) with score 0.962
  - right_eye: (408.83, 130.86) with score 0.938
  - left_ear: (441.95, 133.48) with score 0.832
  - right_ear: (399.56, 133.27) with score 0.652
  - left_shoulder: (440.79, 193.75) with score 0.999
  - right_shoulder: (372.38, 208.42) with score 0.998
  - left_elbow: (453.56, 290.07) with score 0.995
  - right_elbow: (350.56, 262.83) with score 0.992
  - left_wrist: (482.36, 363.64) with score 0.995
  - right_wrist: (398.84, 267.30) with score 0.993
  - left_hip: (435.96, 362.27) with score 0.999
  - right_hip: (388.40, 383.41) with score 0.999
  - left_knee: (460.50, 425.60) with score 0.994
  - right_knee: (403.19, 516.76) with score 0.992
  - left_ankle: (459.31, 558.19) with score 0.893
  - right_ankle: (426.29, 552.55) with score 0.868