Beeniebeen commited on
Commit
8cd8543
ยท
verified ยท
1 Parent(s): b7422b8

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +31 -5
index.js CHANGED
@@ -13,6 +13,34 @@ const EXAMPLE_URL = 'https://huggingface.co/datasets/Xenova/transformers.js-docs
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 '???';
@@ -44,15 +72,13 @@ async function detect(img) {
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;
 
13
 
14
  // Create a new object detection pipeline
15
  status.textContent = 'Loading model...';
16
+
17
+ ์•„๋ž˜ ์ฝ”๋“œ๋Š” ์ฃผ์–ด์ง„ ์ฃผ์„์„ ๊ธฐ๋ฐ˜์œผ๋กœ detr-resnet-50 Object Detection ๋ชจ๋ธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด Hugging Face์˜ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
18
+
19
+ javascript
20
+ Copy code
21
+ // ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
22
+ const { pipeline } = require('@huggingface/node-fetch');
23
+ const { detr } = require('@transformers/detr-resnet-50');
24
+
25
+ // ์ƒํƒœ ํ…์ŠคํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
26
+ status.textContent = 'Loading model...';
27
+
28
+ // DETR ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
29
+ (async () => {
30
+ try {
31
+ // Object Detection ๋ชจ๋ธ์„ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
32
+ const detector = await pipeline(detr, { model: 'facebook/detr-resnet-50' });
33
+
34
+ // ๋ชจ๋ธ ๋กœ๋”ฉ์ด ์™„๋ฃŒ๋˜๋ฉด ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
35
+ status.textContent = 'Model loaded successfully.';
36
+
37
+ // ์ƒ์„ฑ๋œ detector๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Object Detection์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
38
+ // ์˜ˆ์‹œ: const result = await detector('input image');
39
+ } catch (error) {
40
+ // ๋ชจ๋ธ ๋กœ๋”ฉ ์ค‘์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
41
+ status.textContent = 'Error loading model: ' + error.message;
42
+ }
43
+ })();
44
  // To-Do #1 pipeline API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ detr-resnet-50 object detection ๋ชจ๋ธ์˜ instance๋ฅผ detector๋ผ๋Š” ์ด๋ฆ„์„ ๋ถ™์—ฌ ์ƒ์„ฑํ•˜์‹ญ์‹œ์˜ค.
45
  // DETR ๋ชจ๋ธ ์ฐธ๊ณ  ๋ฌธ์„œ https://huggingface.co/facebook/detr-resnet-50
46
  const detector = await '???';
 
72
  imageContainer.style.backgroundImage = `url(${img})`;
73
 
74
  status.textContent = 'Analysing...';
75
+ // ๊ฐ์ฒด ํƒ์ง€๋ฅผ ์œ„ํ•œ ํŒŒ์ดํ”„๋ผ์ธ์„ ์ƒ์„ฑํ•˜๊ณ  threshold์™€ percentage๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
76
+ const output = await detector(img, { threshold: 0.5, percentage: true });
 
 
 
77
  status.textContent = '';
78
  output.forEach(renderBox);
79
  }
80
 
81
+
82
  // Render a bounding box and label on the image
83
  function renderBox({ box, label }) {
84
  const { xmax, xmin, ymax, ymin } = box;