Quazim0t0 commited on
Commit
2e2739c
·
verified ·
1 Parent(s): 022b71f

Upload 37 files

Browse files
src/components/BoxAnnotator.ts CHANGED
@@ -29,12 +29,14 @@ export function extractJsonFromMarkdown(markdown: string): any[] | null {
29
  export function drawBoundingBoxesOnCanvas(
30
  ctx: CanvasRenderingContext2D,
31
  boxes: { bbox_2d: number[]; label?: string }[],
32
- options?: { color?: string; lineWidth?: number; font?: string }
33
  ) {
34
  if (!Array.isArray(boxes)) return; // Prevent errors if boxes is undefined/null
35
  const color = options?.color || "#00FF00";
36
  const lineWidth = options?.lineWidth || 2;
37
  const font = options?.font || "16px Arial";
 
 
38
 
39
  ctx.save();
40
  ctx.strokeStyle = color;
@@ -44,11 +46,15 @@ export function drawBoundingBoxesOnCanvas(
44
 
45
  boxes.forEach((obj) => {
46
  const [x1, y1, x2, y2] = obj.bbox_2d;
 
 
 
 
47
  ctx.beginPath();
48
- ctx.rect(x1, y1, x2 - x1, y2 - y1);
49
  ctx.stroke();
50
  if (obj.label) {
51
- ctx.fillText(obj.label, x1 + 4, y1 - 4 < 10 ? y1 + 16 : y1 - 4);
52
  }
53
  });
54
 
 
29
  export function drawBoundingBoxesOnCanvas(
30
  ctx: CanvasRenderingContext2D,
31
  boxes: { bbox_2d: number[]; label?: string }[],
32
+ options?: { color?: string; lineWidth?: number; font?: string, scaleX?: number, scaleY?: number }
33
  ) {
34
  if (!Array.isArray(boxes)) return; // Prevent errors if boxes is undefined/null
35
  const color = options?.color || "#00FF00";
36
  const lineWidth = options?.lineWidth || 2;
37
  const font = options?.font || "16px Arial";
38
+ const scaleX = options?.scaleX ?? 1;
39
+ const scaleY = options?.scaleY ?? 1;
40
 
41
  ctx.save();
42
  ctx.strokeStyle = color;
 
46
 
47
  boxes.forEach((obj) => {
48
  const [x1, y1, x2, y2] = obj.bbox_2d;
49
+ const sx1 = x1 * scaleX;
50
+ const sy1 = y1 * scaleY;
51
+ const sx2 = x2 * scaleX;
52
+ const sy2 = y2 * scaleY;
53
  ctx.beginPath();
54
+ ctx.rect(sx1, sy1, sx2 - sx1, sy2 - sy1);
55
  ctx.stroke();
56
  if (obj.label) {
57
+ ctx.fillText(obj.label, sx1 + 4, sy1 - 4 < 10 ? sy1 + 16 : sy1 - 4);
58
  }
59
  });
60
 
src/components/MultiSourceCaptioningView.tsx CHANGED
@@ -103,7 +103,9 @@ export default function MultiSourceCaptioningView() {
103
  }
104
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
105
  if (Array.isArray(boxes) && boxes.length > 0) {
106
- drawBoundingBoxesOnCanvas(ctx, boxes);
 
 
107
  }
108
  });
109
  };
@@ -233,7 +235,9 @@ export default function MultiSourceCaptioningView() {
233
  }
234
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
235
  if (Array.isArray(boxes) && boxes.length > 0) {
236
- drawBoundingBoxesOnCanvas(ctx, boxes);
 
 
237
  }
238
  setImageProcessed(true);
239
  });
 
103
  }
104
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
105
  if (Array.isArray(boxes) && boxes.length > 0) {
106
+ const scaleX = canvas.width / video.videoWidth;
107
+ const scaleY = canvas.height / video.videoHeight;
108
+ drawBoundingBoxesOnCanvas(ctx, boxes, { scaleX, scaleY });
109
  }
110
  });
111
  };
 
235
  }
236
  if (boxes.length === 0) setInferenceStatus("No boxes detected or model output invalid.");
237
  if (Array.isArray(boxes) && boxes.length > 0) {
238
+ const scaleX = canvas.width / img.naturalWidth;
239
+ const scaleY = canvas.height / img.naturalHeight;
240
+ drawBoundingBoxesOnCanvas(ctx, boxes, { scaleX, scaleY });
241
  }
242
  setImageProcessed(true);
243
  });