|
| 1 | +import { AutoModel, AutoProcessor, RawImage } from "@huggingface/transformers"; |
| 2 | + |
| 3 | +/** |
| 4 | + * @typedef {Object} Detection |
| 5 | + * @property {number} x1 The x-coordinate of the top-left corner. |
| 6 | + * @property {number} y1 The y-coordinate of the top-left corner. |
| 7 | + * @property {number} x2 The x-coordinate of the bottom-right corner. |
| 8 | + * @property {number} y2 The y-coordinate of the bottom-right corner. |
| 9 | + * @property {number} score The confidence score of the detection. |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Compute Intersection over Union (IoU) between two detections. |
| 14 | + * @param {Detection} a The first detection. |
| 15 | + * @param {Detection} b The second detection. |
| 16 | + */ |
| 17 | +function iou(a, b) { |
| 18 | + const x1 = Math.max(a.x1, b.x1); |
| 19 | + const y1 = Math.max(a.y1, b.y1); |
| 20 | + const x2 = Math.min(a.x2, b.x2); |
| 21 | + const y2 = Math.min(a.y2, b.y2); |
| 22 | + |
| 23 | + const intersection = Math.max(0, x2 - x1) * Math.max(0, y2 - y1); |
| 24 | + const area1 = (a.x2 - a.x1) * (a.y2 - a.y1); |
| 25 | + const area2 = (b.x2 - b.x1) * (b.y2 - b.y1); |
| 26 | + const union = area1 + area2 - intersection; |
| 27 | + |
| 28 | + return intersection / union; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Run Non-Maximum Suppression (NMS) on a list of detections. |
| 33 | + * @param {Detection[]} detections The list of detections. |
| 34 | + * @param {number} iouThreshold The IoU threshold for NMS. |
| 35 | + */ |
| 36 | +export function nms(detections, iouThreshold) { |
| 37 | + const result = []; |
| 38 | + while (detections.length > 0) { |
| 39 | + const best = detections.reduce((acc, detection) => |
| 40 | + detection.score > acc.score ? detection : acc, |
| 41 | + ); |
| 42 | + result.push(best); |
| 43 | + detections = detections.filter( |
| 44 | + (detection) => iou(detection, best) < iouThreshold, |
| 45 | + ); |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | + |
| 50 | +export class Detector { |
| 51 | + /** |
| 52 | + * Create a new YOLOv8 detector. |
| 53 | + * @param {import('@huggingface/transformers').PreTrainedModel} model The model to use for detection |
| 54 | + * @param {import('@huggingface/transformers').Processor} processor The processor to use for detection |
| 55 | + */ |
| 56 | + constructor(model, processor) { |
| 57 | + this.model = model; |
| 58 | + this.processor = processor; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Run detection on an image. |
| 63 | + * @param {RawImage|string|URL} input The input image. |
| 64 | + * @param {Object} [options] The options for detection. |
| 65 | + * @param {number} [options.confidence_threshold=0.25] The confidence threshold. |
| 66 | + * @param {number} [options.iou_threshold=0.7] The IoU threshold for NMS. |
| 67 | + * @returns {Promise<Detection[]>} The list of detections |
| 68 | + */ |
| 69 | + async predict( |
| 70 | + input, |
| 71 | + { confidence_threshold = 0.25, iou_threshold = 0.7 } = {}, |
| 72 | + ) { |
| 73 | + const image = await RawImage.read(input); |
| 74 | + const { pixel_values } = await this.processor(image); |
| 75 | + |
| 76 | + // Run detection |
| 77 | + const { output0 } = await this.model({ images: pixel_values }); |
| 78 | + |
| 79 | + // Post-process output |
| 80 | + const permuted = output0[0].transpose(1, 0); |
| 81 | + // `permuted` is a Tensor of shape [ 5460, 5 ]: |
| 82 | + // - 5460 potential bounding boxes |
| 83 | + // - 5 parameters for each box: |
| 84 | + // - first 4 are coordinates for the bounding boxes (x-center, y-center, width, height) |
| 85 | + // - the last one is the confidence score |
| 86 | + |
| 87 | + // Format output |
| 88 | + const result = []; |
| 89 | + const [scaledHeight, scaledWidth] = pixel_values.dims.slice(-2); |
| 90 | + for (const [xc, yc, w, h, score] of permuted.tolist()) { |
| 91 | + // Filter if not confident enough |
| 92 | + if (score < confidence_threshold) continue; |
| 93 | + |
| 94 | + // Get pixel values, taking into account the original image size |
| 95 | + const x1 = ((xc - w / 2) / scaledWidth) * image.width; |
| 96 | + const y1 = ((yc - h / 2) / scaledHeight) * image.height; |
| 97 | + const x2 = ((xc + w / 2) / scaledWidth) * image.width; |
| 98 | + const y2 = ((yc + h / 2) / scaledHeight) * image.height; |
| 99 | + |
| 100 | + // Add to result |
| 101 | + result.push({ x1, x2, y1, y2, score }); |
| 102 | + } |
| 103 | + |
| 104 | + return nms(result, iou_threshold); |
| 105 | + } |
| 106 | + |
| 107 | + static async from_pretrained(model_id) { |
| 108 | + const model = await AutoModel.from_pretrained(model_id, { dtype: "fp32" }); |
| 109 | + const processor = await AutoProcessor.from_pretrained(model_id); |
| 110 | + return new Detector(model, processor); |
| 111 | + } |
| 112 | +} |
0 commit comments