Create prob-index.js
Browse files- prob-index.js +82 -0
prob-index.js
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
|
2 |
+
|
3 |
+
// Since we will download the model from the Hugging Face Hub, we can skip the local model check
|
4 |
+
env.allowLocalModels = false;
|
5 |
+
|
6 |
+
// Reference the elements that we will need
|
7 |
+
const status = document.getElementById('status');
|
8 |
+
const fileUpload = document.getElementById('upload');
|
9 |
+
const imageContainer = document.getElementById('container');
|
10 |
+
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 |
+
// Create a new object detection pipeline
|
15 |
+
status.textContent = 'Loading model...';
|
16 |
+
// To-Do #1 pipeline API를 사용하여 detr-resnet-50 object detection 모델의 instance를 detector라는 이름을 붙여 생성하십시오.
|
17 |
+
// DETR 모델 참고 문서 https://huggingface.co/facebook/detr-resnet-50
|
18 |
+
const detector = await '???';
|
19 |
+
status.textContent = 'Ready';
|
20 |
+
|
21 |
+
example.addEventListener('click', (e) => {
|
22 |
+
e.preventDefault();
|
23 |
+
detect(EXAMPLE_URL);
|
24 |
+
});
|
25 |
+
|
26 |
+
fileUpload.addEventListener('change', function (e) {
|
27 |
+
const file = e.target.files[0];
|
28 |
+
if (!file) {
|
29 |
+
return;
|
30 |
+
}
|
31 |
+
|
32 |
+
const reader = new FileReader();
|
33 |
+
|
34 |
+
// Set up a callback when the file is loaded
|
35 |
+
reader.onload = e2 => detect(e2.target.result);
|
36 |
+
|
37 |
+
reader.readAsDataURL(file);
|
38 |
+
});
|
39 |
+
|
40 |
+
|
41 |
+
// Detect objects in the image
|
42 |
+
async function detect(img) {
|
43 |
+
imageContainer.innerHTML = '';
|
44 |
+
imageContainer.style.backgroundImage = `url(${img})`;
|
45 |
+
|
46 |
+
status.textContent = 'Analysing...';
|
47 |
+
// To-Do #2 객체 탐지를 위한 오브젝트에 threshold를 0.5, percentage를 true로 지정하고 그 결과를 output에 저장하십시오
|
48 |
+
const output = ???(
|
49 |
+
// threshold 값을 지정하고 쉼표를 붙이시오
|
50 |
+
// percentage 지정
|
51 |
+
);
|
52 |
+
status.textContent = '';
|
53 |
+
output.forEach(renderBox);
|
54 |
+
}
|
55 |
+
|
56 |
+
// Render a bounding box and label on the image
|
57 |
+
function renderBox({ box, label }) {
|
58 |
+
const { xmax, xmin, ymax, ymin } = box;
|
59 |
+
|
60 |
+
// Generate a random color for the box
|
61 |
+
const color = '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, 0);
|
62 |
+
|
63 |
+
// Draw the box
|
64 |
+
const boxElement = document.createElement('div');
|
65 |
+
boxElement.className = 'bounding-box';
|
66 |
+
Object.assign(boxElement.style, {
|
67 |
+
borderColor: color,
|
68 |
+
left: 100 * xmin + '%',
|
69 |
+
top: 100 * ymin + '%',
|
70 |
+
width: 100 * (xmax - xmin) + '%',
|
71 |
+
height: 100 * (ymax - ymin) + '%',
|
72 |
+
})
|
73 |
+
|
74 |
+
// Draw label
|
75 |
+
const labelElement = document.createElement('span');
|
76 |
+
labelElement.textContent = label;
|
77 |
+
labelElement.className = 'bounding-box-label';
|
78 |
+
labelElement.style.backgroundColor = color;
|
79 |
+
|
80 |
+
boxElement.appendChild(labelElement);
|
81 |
+
imageContainer.appendChild(boxElement);
|
82 |
+
}
|