Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
|
@@ -11,16 +11,16 @@ const example = document.getElementById('example');
|
|
| 11 |
|
| 12 |
const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
|
| 13 |
|
| 14 |
-
const
|
| 15 |
|
| 16 |
// Create a new object detection pipeline
|
| 17 |
status.textContent = 'Loading model...';
|
| 18 |
-
const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-
|
| 19 |
|
| 20 |
-
// For this demo, we resize the image
|
| 21 |
-
processor.feature_extractor.size = {
|
| 22 |
|
| 23 |
-
const model = await AutoModel.from_pretrained('Xenova/yolov9-
|
| 24 |
status.textContent = 'Ready';
|
| 25 |
|
| 26 |
example.addEventListener('click', (e) => {
|
|
@@ -48,7 +48,7 @@ async function detect(url) {
|
|
| 48 |
// Update UI
|
| 49 |
imageContainer.innerHTML = '';
|
| 50 |
imageContainer.style.backgroundImage = `url(${url})`;
|
| 51 |
-
|
| 52 |
// Read image
|
| 53 |
const image = await RawImage.fromURL(url);
|
| 54 |
|
|
@@ -61,17 +61,21 @@ async function detect(url) {
|
|
| 61 |
status.textContent = 'Analysing...';
|
| 62 |
|
| 63 |
// Preprocess image
|
| 64 |
-
const
|
| 65 |
|
| 66 |
// Predict bounding boxes
|
| 67 |
-
const { outputs } = await model(
|
| 68 |
-
|
| 69 |
status.textContent = '';
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
// Render a bounding box and label on the image
|
| 74 |
-
function renderBox([xmin, ymin, xmax, ymax, score, id]) {
|
|
|
|
|
|
|
| 75 |
// Generate a random color for the box
|
| 76 |
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 77 |
|
|
@@ -80,10 +84,10 @@ function renderBox([xmin, ymin, xmax, ymax, score, id]) {
|
|
| 80 |
boxElement.className = 'bounding-box';
|
| 81 |
Object.assign(boxElement.style, {
|
| 82 |
borderColor: color,
|
| 83 |
-
left: 100 * xmin /
|
| 84 |
-
top: 100 * ymin /
|
| 85 |
-
width: 100 * (xmax - xmin) /
|
| 86 |
-
height: 100 * (ymax - ymin) /
|
| 87 |
})
|
| 88 |
|
| 89 |
// Draw label
|
|
|
|
| 11 |
|
| 12 |
const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/city-streets.jpg';
|
| 13 |
|
| 14 |
+
const THRESHOLD = 0.25;
|
| 15 |
|
| 16 |
// Create a new object detection pipeline
|
| 17 |
status.textContent = 'Loading model...';
|
| 18 |
+
const processor = await AutoProcessor.from_pretrained('Xenova/yolov9-c_all');
|
| 19 |
|
| 20 |
+
// For this demo, we resize the image so that its shortest edge is 256px
|
| 21 |
+
processor.feature_extractor.size = { shortest_edge: 256 }
|
| 22 |
|
| 23 |
+
const model = await AutoModel.from_pretrained('Xenova/yolov9-c_all');
|
| 24 |
status.textContent = 'Ready';
|
| 25 |
|
| 26 |
example.addEventListener('click', (e) => {
|
|
|
|
| 48 |
// Update UI
|
| 49 |
imageContainer.innerHTML = '';
|
| 50 |
imageContainer.style.backgroundImage = `url(${url})`;
|
| 51 |
+
|
| 52 |
// Read image
|
| 53 |
const image = await RawImage.fromURL(url);
|
| 54 |
|
|
|
|
| 61 |
status.textContent = 'Analysing...';
|
| 62 |
|
| 63 |
// Preprocess image
|
| 64 |
+
const inputs = await processor(image);
|
| 65 |
|
| 66 |
// Predict bounding boxes
|
| 67 |
+
const { outputs } = await model(inputs);
|
| 68 |
+
|
| 69 |
status.textContent = '';
|
| 70 |
+
|
| 71 |
+
const sizes = inputs.reshaped_input_sizes[0].reverse();
|
| 72 |
+
outputs.tolist().forEach(x => renderBox(x, sizes));
|
| 73 |
}
|
| 74 |
|
| 75 |
// Render a bounding box and label on the image
|
| 76 |
+
function renderBox([xmin, ymin, xmax, ymax, score, id], [w, h]) {
|
| 77 |
+
if (score < THRESHOLD) return; // Skip boxes with low confidence
|
| 78 |
+
|
| 79 |
// Generate a random color for the box
|
| 80 |
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 81 |
|
|
|
|
| 84 |
boxElement.className = 'bounding-box';
|
| 85 |
Object.assign(boxElement.style, {
|
| 86 |
borderColor: color,
|
| 87 |
+
left: 100 * xmin / w + '%',
|
| 88 |
+
top: 100 * ymin / h + '%',
|
| 89 |
+
width: 100 * (xmax - xmin) / w + '%',
|
| 90 |
+
height: 100 * (ymax - ymin) / h + '%',
|
| 91 |
})
|
| 92 |
|
| 93 |
// Draw label
|