ruchi
commited on
Commit
·
0f22e71
1
Parent(s):
b4acb11
Init commit
Browse files
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
---
|
|
|
|
| 1 |
---
|
| 2 |
+
title: VanillaJS
|
| 3 |
+
emoji: 🏢
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
---
|
index.html
CHANGED
|
@@ -1,19 +1,24 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
|
| 4 |
+
<head>
|
| 5 |
+
<meta charset="UTF-8" />
|
| 6 |
+
<link rel="stylesheet" href="style.css" />
|
| 7 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 8 |
+
<title>Transformers.js - Object Detection demo</title>
|
| 9 |
+
</head>
|
| 10 |
+
|
| 11 |
+
<body>
|
| 12 |
+
<main class="container">
|
| 13 |
+
<label class="custom-file-upload">
|
| 14 |
+
<input id="file-upload" type="file" accept="image/*" />
|
| 15 |
+
<img class="upload-icon" src="https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/upload-icon.png" />
|
| 16 |
+
Upload image
|
| 17 |
+
</label>
|
| 18 |
+
<div id="image-container"></div>
|
| 19 |
+
<p id="status"></p>
|
| 20 |
+
</main>
|
| 21 |
+
<script src="index.js" type="module"></script>
|
| 22 |
+
</body>
|
| 23 |
+
|
| 24 |
+
</html>
|
index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0';
|
| 3 |
+
|
| 4 |
+
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
|
| 5 |
+
env.allowLocalModels = false;
|
| 6 |
+
|
| 7 |
+
// Reference the elements that we will need
|
| 8 |
+
const status = document.getElementById('status');
|
| 9 |
+
const fileUpload = document.getElementById('file-upload');
|
| 10 |
+
const imageContainer = document.getElementById('image-container');
|
| 11 |
+
|
| 12 |
+
// Create a new object detection pipeline
|
| 13 |
+
status.textContent = 'Loading model...';
|
| 14 |
+
const detector = await pipeline('object-detection', 'Xenova/detr-resnet-101');
|
| 15 |
+
status.textContent = 'Ready';
|
| 16 |
+
|
| 17 |
+
fileUpload.addEventListener('change', function (e) {
|
| 18 |
+
const file = e.target.files[0];
|
| 19 |
+
if (!file) {
|
| 20 |
+
return;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
const reader = new FileReader();
|
| 24 |
+
|
| 25 |
+
// Set up a callback when the file is loaded
|
| 26 |
+
reader.onload = function (e2) {
|
| 27 |
+
imageContainer.innerHTML = '';
|
| 28 |
+
const image = document.createElement('img');
|
| 29 |
+
image.src = e2.target.result;
|
| 30 |
+
imageContainer.appendChild(image);
|
| 31 |
+
detect(image);
|
| 32 |
+
};
|
| 33 |
+
reader.readAsDataURL(file);
|
| 34 |
+
});
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
// Detect objects in the image
|
| 38 |
+
async function detect(img) {
|
| 39 |
+
status.textContent = 'Analysing...';
|
| 40 |
+
const output = await detector(img.src, {
|
| 41 |
+
threshold: 0.5,
|
| 42 |
+
percentage: true,
|
| 43 |
+
});
|
| 44 |
+
status.textContent = '';
|
| 45 |
+
output.forEach(renderBox);
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// Render a bounding box and label on the image
|
| 49 |
+
function renderBox({ box, label }) {
|
| 50 |
+
const { xmax, xmin, ymax, ymin } = box;
|
| 51 |
+
|
| 52 |
+
// Generate a random color for the box
|
| 53 |
+
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
| 54 |
+
|
| 55 |
+
// Draw the box
|
| 56 |
+
const boxElement = document.createElement('div');
|
| 57 |
+
boxElement.className = 'bounding-box';
|
| 58 |
+
Object.assign(boxElement.style, {
|
| 59 |
+
borderColor: color,
|
| 60 |
+
left: 100 * xmin + '%',
|
| 61 |
+
top: 100 * ymin + '%',
|
| 62 |
+
width: 100 * (xmax - xmin) + '%',
|
| 63 |
+
height: 100 * (ymax - ymin) + '%',
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
// Draw label
|
| 67 |
+
const labelElement = document.createElement('span');
|
| 68 |
+
labelElement.textContent = label;
|
| 69 |
+
labelElement.className = 'bounding-box-label';
|
| 70 |
+
labelElement.style.backgroundColor = color;
|
| 71 |
+
|
| 72 |
+
boxElement.appendChild(labelElement);
|
| 73 |
+
imageContainer.appendChild(boxElement);
|
| 74 |
+
}
|
style.css
CHANGED
|
@@ -1,28 +1,58 @@
|
|
|
|
|
| 1 |
body {
|
| 2 |
-
|
| 3 |
-
font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
|
| 4 |
}
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
margin: 0 auto;
|
| 21 |
-
padding: 16px;
|
| 22 |
-
border: 1px solid lightgray;
|
| 23 |
-
border-radius: 16px;
|
| 24 |
}
|
| 25 |
|
| 26 |
-
.
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
html,
|
| 2 |
body {
|
| 3 |
+
font-family: Arial, Helvetica, sans-serif;
|
|
|
|
| 4 |
}
|
| 5 |
|
| 6 |
+
.container {
|
| 7 |
+
margin: 40px auto;
|
| 8 |
+
width: max(50vw, 400px);
|
| 9 |
+
display: flex;
|
| 10 |
+
flex-direction: column;
|
| 11 |
+
align-items: center;
|
| 12 |
}
|
| 13 |
|
| 14 |
+
|
| 15 |
+
.custom-file-upload {
|
| 16 |
+
display: flex;
|
| 17 |
+
align-items: center;
|
| 18 |
+
cursor: pointer;
|
| 19 |
+
gap: 10px;
|
| 20 |
+
border: 2px solid black;
|
| 21 |
+
padding: 8px 16px;
|
| 22 |
+
cursor: pointer;
|
| 23 |
+
border-radius: 6px;
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
#file-upload {
|
| 27 |
+
display: none;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
.upload-icon {
|
| 31 |
+
width: 30px;
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
#image-container {
|
| 35 |
+
width: 100%;
|
| 36 |
+
margin-top: 20px;
|
| 37 |
+
position: relative;
|
| 38 |
}
|
| 39 |
|
| 40 |
+
#image-container>img {
|
| 41 |
+
width: 100%;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
+
.bounding-box {
|
| 45 |
+
position: absolute;
|
| 46 |
+
box-sizing: border-box;
|
| 47 |
+
border-width: 2px;
|
| 48 |
+
border-style: solid;
|
| 49 |
}
|
| 50 |
+
|
| 51 |
+
.bounding-box-label {
|
| 52 |
+
color: white;
|
| 53 |
+
position: absolute;
|
| 54 |
+
font-size: 12px;
|
| 55 |
+
margin-top: -16px;
|
| 56 |
+
margin-left: -2px;
|
| 57 |
+
padding: 1px;
|
| 58 |
+
}
|