Subh775 commited on
Commit
3224a0d
·
verified ·
1 Parent(s): 432ef90

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +22 -16
index.html CHANGED
@@ -208,7 +208,7 @@
208
  }
209
 
210
  initEventListeners() {
211
- // FIX: Simplified event listeners
212
  const fileInput = document.getElementById('file-input');
213
  const uploadArea = document.getElementById('upload-area');
214
 
@@ -254,6 +254,7 @@
254
  this.uploadedImage = img;
255
  this.reset();
256
  this.drawImageScaled(this.inputCanvas, this.inputCtx, img);
 
257
  this.outputCanvas.width = this.inputCanvas.width;
258
  this.outputCanvas.height = this.inputCanvas.height;
259
  this.maskCanvas.width = this.inputCanvas.width;
@@ -374,11 +375,7 @@
374
  renderOutput() {
375
  if (!this.predictedMask) return;
376
 
377
- const tempCanvas = document.createElement('canvas');
378
- tempCanvas.width = this.outputCanvas.width;
379
- tempCanvas.height = this.outputCanvas.height;
380
- const tempCtx = tempCanvas.getContext('2d');
381
-
382
  this.outputCtx.clearRect(0,0, this.outputCanvas.width, this.outputCanvas.height);
383
  this.drawImageScaled(this.outputCanvas, this.outputCtx, this.uploadedImage);
384
 
@@ -386,7 +383,12 @@
386
  this.drawImageScaled(this.outputCanvas, this.outputCtx, this.predictedMask);
387
  return;
388
  }
389
-
 
 
 
 
 
390
  tempCtx.drawImage(this.predictedMask, 0, 0, tempCanvas.width, tempCanvas.height);
391
 
392
  if (this.currentView === 'filled') {
@@ -399,23 +401,27 @@
399
  this.outputCtx.globalAlpha = 1.0;
400
 
401
  } else if (this.currentView === 'outline') {
402
- // This logic now requires OpenCV.js, loaded via the script tag
403
  const src = cv.imread(tempCanvas);
404
  const contours = new cv.MatVector();
405
  const hierarchy = new cv.Mat();
 
406
  cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
407
  cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
408
 
409
- const color = new cv.Scalar(255, 255, 255, 255); // White color
410
- // Create a transparent mat to draw contours on
411
- let contourImg = new cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC4);
412
  for (let i = 0; i < contours.size(); ++i) {
413
- cv.drawContours(contourImg, contours, i, color, 2, cv.LINE_8, hierarchy, 100);
 
 
 
 
 
 
 
414
  }
415
- // Display the contour image on the canvas
416
- cv.imshow(this.outputCanvas, contourImg);
417
-
418
- src.delete(); contours.delete(); hierarchy.delete(); contourImg.delete();
419
  }
420
  }
421
 
 
208
  }
209
 
210
  initEventListeners() {
211
+ // FIX: Simplified event listeners for robust uploading
212
  const fileInput = document.getElementById('file-input');
213
  const uploadArea = document.getElementById('upload-area');
214
 
 
254
  this.uploadedImage = img;
255
  this.reset();
256
  this.drawImageScaled(this.inputCanvas, this.inputCtx, img);
257
+ // Also set the size for other canvases
258
  this.outputCanvas.width = this.inputCanvas.width;
259
  this.outputCanvas.height = this.inputCanvas.height;
260
  this.maskCanvas.width = this.inputCanvas.width;
 
375
  renderOutput() {
376
  if (!this.predictedMask) return;
377
 
378
+ // Always draw the original image on the output canvas first
 
 
 
 
379
  this.outputCtx.clearRect(0,0, this.outputCanvas.width, this.outputCanvas.height);
380
  this.drawImageScaled(this.outputCanvas, this.outputCtx, this.uploadedImage);
381
 
 
383
  this.drawImageScaled(this.outputCanvas, this.outputCtx, this.predictedMask);
384
  return;
385
  }
386
+
387
+ // Create a temporary canvas to work with the mask
388
+ const tempCanvas = document.createElement('canvas');
389
+ const tempCtx = tempCanvas.getContext('2d');
390
+ tempCanvas.width = this.outputCanvas.width;
391
+ tempCanvas.height = this.outputCanvas.height;
392
  tempCtx.drawImage(this.predictedMask, 0, 0, tempCanvas.width, tempCanvas.height);
393
 
394
  if (this.currentView === 'filled') {
 
401
  this.outputCtx.globalAlpha = 1.0;
402
 
403
  } else if (this.currentView === 'outline') {
 
404
  const src = cv.imread(tempCanvas);
405
  const contours = new cv.MatVector();
406
  const hierarchy = new cv.Mat();
407
+ // Convert to grayscale for findContours
408
  cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
409
  cv.findContours(src, contours, hierarchy, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE);
410
 
411
+ // Draw contours directly onto the output canvas
412
+ this.outputCtx.strokeStyle = 'white';
413
+ this.outputCtx.lineWidth = 2;
414
  for (let i = 0; i < contours.size(); ++i) {
415
+ const contour = contours.get(i).data32S; // Get contour points
416
+ this.outputCtx.beginPath();
417
+ this.outputCtx.moveTo(contour[0], contour[1]);
418
+ for (let j = 2; j < contour.length; j += 2) {
419
+ this.outputCtx.lineTo(contour[j], contour[j+1]);
420
+ }
421
+ this.outputCtx.closePath();
422
+ this.outputCtx.stroke();
423
  }
424
+ src.delete(); contours.delete(); hierarchy.delete();
 
 
 
425
  }
426
  }
427