| | import { env, AutoProcessor, AutoModel, RawImage } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2'; |
| |
|
| | |
| | env.allowLocalModels = false; |
| |
|
| | |
| | const status = document.getElementById('status'); |
| | const fileUpload = document.getElementById('upload'); |
| | const imageContainer = document.getElementById('container'); |
| | const example = document.getElementById('example'); |
| |
|
| | const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg'; |
| | const THRESHOLD = 0.2; |
| |
|
| | |
| | status.textContent = 'Loading model...'; |
| | const model_id = 'onnx-community/yolov10m'; |
| | const processor = await AutoProcessor.from_pretrained(model_id); |
| | const model = await AutoModel.from_pretrained(model_id, { quantized: true }); |
| | status.textContent = 'Ready'; |
| |
|
| | example.addEventListener('click', (e) => { |
| | e.preventDefault(); |
| | detect(EXAMPLE_URL); |
| | }); |
| |
|
| | fileUpload.addEventListener('change', function (e) { |
| | const file = e.target.files[0]; |
| | if (!file) { |
| | return; |
| | } |
| |
|
| | const reader = new FileReader(); |
| |
|
| | |
| | reader.onload = e2 => detect(e2.target.result); |
| |
|
| | reader.readAsDataURL(file); |
| | }); |
| |
|
| |
|
| | |
| | async function detect(url) { |
| | |
| | imageContainer.innerHTML = ''; |
| |
|
| | |
| | const image = await RawImage.fromURL(url); |
| |
|
| | |
| | const ar = image.width / image.height; |
| | const [cw, ch] = (ar > 1) ? [640, 640 / ar] : [640 * ar, 640]; |
| | imageContainer.style.width = `${cw}px`; |
| | imageContainer.style.height = `${ch}px`; |
| | imageContainer.style.backgroundImage = `url(${url})`; |
| |
|
| | status.textContent = 'Analysing...'; |
| |
|
| | |
| | const inputs = await processor(image); |
| |
|
| | |
| | const { output0 } = await model({ images: inputs.pixel_values }); |
| |
|
| | status.textContent = ''; |
| |
|
| | const sizes = inputs.reshaped_input_sizes[0].reverse(); |
| | output0.tolist()[0].forEach(x => renderBox(x, sizes)); |
| | } |
| |
|
| | |
| | function renderBox([xmin, ymin, xmax, ymax, score, id], [w, h]) { |
| | if (score < THRESHOLD) return; |
| |
|
| | |
| | const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0); |
| |
|
| | |
| | const boxElement = document.createElement('div'); |
| | boxElement.className = 'bounding-box'; |
| | Object.assign(boxElement.style, { |
| | borderColor: color, |
| | left: 100 * xmin / w + '%', |
| | top: 100 * ymin / h + '%', |
| | width: 100 * (xmax - xmin) / w + '%', |
| | height: 100 * (ymax - ymin) / h + '%', |
| | }) |
| |
|
| | |
| | const labelElement = document.createElement('span'); |
| | labelElement.textContent = `${model.config.id2label[id]} (${score.toFixed(2)})`.replaceAll(' ', '\u00a0'); |
| | labelElement.className = 'bounding-box-label'; |
| | labelElement.style.backgroundColor = color; |
| |
|
| | boxElement.appendChild(labelElement); |
| | imageContainer.appendChild(boxElement); |
| | } |
| |
|