Quazim0t0 commited on
Commit
e4a3834
·
verified ·
1 Parent(s): 974ac1d

Upload 37 files

Browse files
src/components/MultiSourceCaptioningView.tsx CHANGED
@@ -17,28 +17,29 @@ function parseFlatBoxArray(arr: any[]): { label: string, bbox_2d: number[] }[] {
17
  }
18
 
19
  function normalizeBoxes(raw: any): { label: string, bbox_2d: number[] }[] {
 
20
  let boxes = [];
21
  if (Array.isArray(raw)) {
22
  boxes = raw;
23
  } else if (typeof raw === "object" && raw !== null) {
24
  boxes = [raw];
25
  }
26
- // Normalize bbox_2d to flat [x1, y1, x2, y2] only if valid
27
- return boxes.map(obj => {
28
- let bbox = obj.bbox_2d;
29
- if (
30
- Array.isArray(bbox) &&
31
- bbox.length === 2 &&
32
- Array.isArray(bbox[0]) &&
33
- Array.isArray(bbox[1]) &&
34
- bbox[0].length === 2 &&
35
- bbox[1].length === 2
36
- ) {
37
- // Convert [[x1, y1], [x2, y2]] to [x1, y1, x2, y2]
38
- bbox = [bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1]];
39
- }
40
- return { ...obj, bbox_2d: bbox };
41
- });
42
  }
43
 
44
  function isImageFile(file: File) {
@@ -91,7 +92,9 @@ export default function MultiSourceCaptioningView() {
91
  }
92
  boxes = normalizeBoxes(boxes);
93
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
94
- drawBoundingBoxesOnCanvas(ctx, boxes);
 
 
95
  });
96
  };
97
 
@@ -212,7 +215,9 @@ export default function MultiSourceCaptioningView() {
212
  }
213
  boxes = normalizeBoxes(boxes);
214
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
215
- drawBoundingBoxesOnCanvas(ctx, boxes);
 
 
216
  setImageProcessed(true);
217
  });
218
  setProcessing(false);
 
17
  }
18
 
19
  function normalizeBoxes(raw: any): { label: string, bbox_2d: number[] }[] {
20
+ if (!raw) return [];
21
  let boxes = [];
22
  if (Array.isArray(raw)) {
23
  boxes = raw;
24
  } else if (typeof raw === "object" && raw !== null) {
25
  boxes = [raw];
26
  }
27
+ return boxes
28
+ .filter(obj => obj && obj.bbox_2d)
29
+ .map(obj => {
30
+ let bbox = obj.bbox_2d;
31
+ if (
32
+ Array.isArray(bbox) &&
33
+ bbox.length === 2 &&
34
+ Array.isArray(bbox[0]) &&
35
+ Array.isArray(bbox[1]) &&
36
+ bbox[0].length === 2 &&
37
+ bbox[1].length === 2
38
+ ) {
39
+ bbox = [bbox[0][0], bbox[0][1], bbox[1][0], bbox[1][1]];
40
+ }
41
+ return { ...obj, bbox_2d: bbox };
42
+ });
43
  }
44
 
45
  function isImageFile(file: File) {
 
92
  }
93
  boxes = normalizeBoxes(boxes);
94
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
95
+ if (Array.isArray(boxes) && boxes.length > 0) {
96
+ drawBoundingBoxesOnCanvas(ctx, boxes);
97
+ }
98
  });
99
  };
100
 
 
215
  }
216
  boxes = normalizeBoxes(boxes);
217
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
218
+ if (Array.isArray(boxes) && boxes.length > 0) {
219
+ drawBoundingBoxesOnCanvas(ctx, boxes);
220
+ }
221
  setImageProcessed(true);
222
  });
223
  setProcessing(false);