KennethTM commited on
Commit
5d22ea0
·
verified ·
1 Parent(s): bf32711

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +30 -3
index.html CHANGED
@@ -275,7 +275,8 @@
275
  }
276
 
277
  // --- Image Processing ---
278
- function processImage(source, sourceWidth, sourceHeight) {
 
279
  const size = Math.min(sourceWidth, sourceHeight);
280
  const sx = (sourceWidth - size) / 2;
281
  const sy = (sourceHeight - size) / 2;
@@ -290,6 +291,30 @@
290
  );
291
  }
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  // --- API Call ---
294
  async function callPredictAPI() {
295
  try {
@@ -429,7 +454,8 @@
429
  return;
430
  }
431
 
432
- processImage(video, video.videoWidth, video.videoHeight);
 
433
  processCaptureOrUpload();
434
  });
435
 
@@ -441,7 +467,8 @@
441
  reader.onload = (e) => {
442
  const img = new Image();
443
  img.onload = () => {
444
- processImage(img, img.width, img.height);
 
445
  processCaptureOrUpload();
446
  };
447
  img.onerror = () => {
 
275
  }
276
 
277
  // --- Image Processing ---
278
+ function processImageCenterCrop(source, sourceWidth, sourceHeight) {
279
+ // Center crop to square for live camera
280
  const size = Math.min(sourceWidth, sourceHeight);
281
  const sx = (sourceWidth - size) / 2;
282
  const sy = (sourceHeight - size) / 2;
 
291
  );
292
  }
293
 
294
+ function processImageResizePad(source, sourceWidth, sourceHeight) {
295
+ // Resize longest dimension to 256 and zero-pad for uploads
296
+ const scale = FINAL_SIZE / Math.max(sourceWidth, sourceHeight);
297
+ const scaledWidth = sourceWidth * scale;
298
+ const scaledHeight = sourceHeight * scale;
299
+
300
+ // Calculate padding to center the image
301
+ const offsetX = (FINAL_SIZE - scaledWidth) / 2;
302
+ const offsetY = (FINAL_SIZE - scaledHeight) / 2;
303
+
304
+ // Clear canvas (black background = zero padding)
305
+ processingCtx.fillStyle = 'black';
306
+ processingCtx.fillRect(0, 0, FINAL_SIZE, FINAL_SIZE);
307
+
308
+ // Draw scaled image centered with padding
309
+ processingCtx.drawImage(
310
+ source,
311
+ 0, 0,
312
+ sourceWidth, sourceHeight,
313
+ offsetX, offsetY,
314
+ scaledWidth, scaledHeight
315
+ );
316
+ }
317
+
318
  // --- API Call ---
319
  async function callPredictAPI() {
320
  try {
 
454
  return;
455
  }
456
 
457
+ // Use center crop for live camera
458
+ processImageCenterCrop(video, video.videoWidth, video.videoHeight);
459
  processCaptureOrUpload();
460
  });
461
 
 
467
  reader.onload = (e) => {
468
  const img = new Image();
469
  img.onload = () => {
470
+ // Use resize+pad for uploads
471
+ processImageResizePad(img, img.width, img.height);
472
  processCaptureOrUpload();
473
  };
474
  img.onerror = () => {