Emeritus-21 commited on
Commit
358a9a2
·
verified ·
1 Parent(s): 4309bf7

Update static/app.js

Browse files
Files changed (1) hide show
  1. static/app.js +29 -12
static/app.js CHANGED
@@ -1,31 +1,48 @@
1
  const video = document.getElementById('video');
 
 
 
2
  const predictionEl = document.getElementById('prediction');
3
  const confidenceEl = document.getElementById('confidence');
4
 
5
- // Start webcam
6
  navigator.mediaDevices.getUserMedia({ video: true })
7
- .then(stream => { video.srcObject = stream; })
8
- .catch(err => console.error("Webcam error:", err));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  setInterval(() => {
11
- // Crop whole frame to 228x228 to match training
12
  const roiCanvas = document.createElement('canvas');
13
- roiCanvas.width = 228;
14
- roiCanvas.height = 228;
15
  const roiCtx = roiCanvas.getContext('2d');
16
- roiCtx.drawImage(video, 0, 0, 228, 228);
17
-
18
  const imgData = roiCanvas.toDataURL('image/jpeg');
19
 
20
  fetch('/predict', {
21
  method: 'POST',
22
- headers: { 'Content-Type': 'application/json' },
23
  body: JSON.stringify({ image: imgData })
24
  })
25
  .then(res => res.json())
26
  .then(data => {
27
- predictionEl.textContent = `Prediction: ${data.prediction}`;
28
- confidenceEl.textContent = `Confidence: ${data.confidence.toFixed(1)}%`;
29
  })
30
- .catch(console.error);
31
  }, 500);
 
1
  const video = document.getElementById('video');
2
+ const canvas = document.getElementById('canvas');
3
+ const ctx = canvas.getContext('2d');
4
+
5
  const predictionEl = document.getElementById('prediction');
6
  const confidenceEl = document.getElementById('confidence');
7
 
 
8
  navigator.mediaDevices.getUserMedia({ video: true })
9
+ .then(stream => {
10
+ video.srcObject = stream;
11
+ video.play();
12
+ requestAnimationFrame(drawLoop);
13
+ })
14
+ .catch(err => console.error("Error accessing webcam:", err));
15
+
16
+ function drawLoop() {
17
+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
18
+
19
+ // Draw fixed ROI box
20
+ const x = 100, y = 60, w = 400, h = 360;
21
+ ctx.strokeStyle = 'red';
22
+ ctx.lineWidth = 3;
23
+ ctx.strokeRect(x, y, w, h);
24
+
25
+ requestAnimationFrame(drawLoop);
26
+ }
27
 
28
  setInterval(() => {
29
+ const x = 100, y = 60, w = 400, h = 360;
30
  const roiCanvas = document.createElement('canvas');
31
+ roiCanvas.width = w;
32
+ roiCanvas.height = h;
33
  const roiCtx = roiCanvas.getContext('2d');
34
+ roiCtx.drawImage(video, x, y, w, h, 0, 0, w, h);
 
35
  const imgData = roiCanvas.toDataURL('image/jpeg');
36
 
37
  fetch('/predict', {
38
  method: 'POST',
39
+ headers: {'Content-Type': 'application/json'},
40
  body: JSON.stringify({ image: imgData })
41
  })
42
  .then(res => res.json())
43
  .then(data => {
44
+ predictionEl.textContent = data.prediction;
45
+ confidenceEl.textContent = data.confidence.toFixed(1);
46
  })
47
+ .catch(err => console.error(err));
48
  }, 500);