Arsala Grey commited on
Commit
4f78fc8
1 Parent(s): e7384c3

fixed error when resetting. Also fixed zIndex issue

Browse files
Files changed (1) hide show
  1. index.js +26 -19
index.js CHANGED
@@ -31,9 +31,13 @@ const drawDetectedObjects = (img, detections) => {
31
  box.classList.add('bounding-box');
32
  box.style.left = `${xmin}px`;
33
  box.style.top = `${ymin}px`;
34
- box.style.width = `${xmax - xmin}px`;
35
- box.style.height = `${ymax - ymin}px`;
 
 
36
 
 
 
37
  const tooltip = document.createElement('div');
38
  tooltip.classList.add('tooltip');
39
  tooltip.innerText = `${label}: ${confidence}`;
@@ -41,12 +45,14 @@ const drawDetectedObjects = (img, detections) => {
41
 
42
  detectedObjectBoxes.push(box)
43
  });
44
-
45
  detectedObjectBoxes.sort((a, b) => {
46
- const aSize = a.style.width * a.style.height
47
- const bSize = b.style.width * b.style.height
48
- return aSize - bSize
49
- }).forEach(box => container.appendChild(box))
 
 
50
  };
51
  const app = createApp({
52
  setup() {
@@ -67,9 +73,7 @@ const app = createApp({
67
  })
68
 
69
  const run = async () => {
70
- if (detectedObjects.value.length > 0) {
71
- removeDetectedObjects()
72
- }
73
  loading.value = true;
74
  try {
75
  const hf = new HfInference(token.value);
@@ -87,21 +91,24 @@ const app = createApp({
87
  didErrorOccur.value = true
88
  }
89
  };
90
-
91
- const removeDetectedObjects = () => {
92
- document.getElementById('detected-objects-container').remove()
93
- document.getElementsByClassName('bounding-box').forEach(box => box.remove())
94
- detectedObjects.value = []
95
- }
96
-
97
  const reset = () => {
98
- removeDetectedObjects()
 
 
 
 
 
 
 
 
99
  didErrorOccur.value = false
 
100
  }
101
 
102
  const shuffle = async () => {
103
- imageUrl.value = await getRandomImageUrl()
104
  reset()
 
105
  };
106
 
107
  onMounted(async () => {
 
31
  box.classList.add('bounding-box');
32
  box.style.left = `${xmin}px`;
33
  box.style.top = `${ymin}px`;
34
+ const width = xmax - xmin
35
+ const height = ymax - ymin
36
+ box.style.width = `${width}px`;
37
+ box.style.height = `${height}px`;
38
 
39
+ const area = width * height
40
+ box.area = area
41
  const tooltip = document.createElement('div');
42
  tooltip.classList.add('tooltip');
43
  tooltip.innerText = `${label}: ${confidence}`;
 
45
 
46
  detectedObjectBoxes.push(box)
47
  });
48
+ let zIndex = 0
49
  detectedObjectBoxes.sort((a, b) => {
50
+ return b.area - a.area
51
+ }).forEach(box => {
52
+ box.style.zIndex = zIndex
53
+ container.appendChild(box)
54
+ zIndex++
55
+ })
56
  };
57
  const app = createApp({
58
  setup() {
 
73
  })
74
 
75
  const run = async () => {
76
+ reset()
 
 
77
  loading.value = true;
78
  try {
79
  const hf = new HfInference(token.value);
 
91
  didErrorOccur.value = true
92
  }
93
  };
 
 
 
 
 
 
 
94
  const reset = () => {
95
+ try {
96
+ document.getElementById('detected-objects-container')?.remove()
97
+ const boundingBoxes = document.getElementsByClassName('bounding-box')
98
+ for (const box of boundingBoxes) {
99
+ box.remove()
100
+ }
101
+ } catch (e) {
102
+ console.error(e)
103
+ }
104
  didErrorOccur.value = false
105
+ detectedObjects.value = []
106
  }
107
 
108
  const shuffle = async () => {
109
+ imageUrl.value = ""
110
  reset()
111
+ imageUrl.value = await getRandomImageUrl()
112
  };
113
 
114
  onMounted(async () => {