aryan365 commited on
Commit
abce40f
·
verified ·
1 Parent(s): f342f31

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +32 -44
templates/index.html CHANGED
@@ -13,7 +13,7 @@
13
  margin: 0;
14
  flex-direction: column;
15
  }
16
-
17
  #camera-box {
18
  width: 640px;
19
  height: 480px;
@@ -24,23 +24,21 @@
24
  align-items: center;
25
  position: relative;
26
  }
27
-
28
  video {
29
  width: 100%;
30
  height: 100%;
31
  object-fit: cover;
32
- transform: scaleX(-1);
33
  }
34
-
35
  #prediction-box {
36
  margin-top: 10px;
37
  text-align: center;
38
  }
39
 
40
- #switch-camera-btn {
41
- margin-top: 10px;
42
- padding: 5px 10px;
43
- font-size: 16px;
44
  }
45
  </style>
46
  </head>
@@ -52,41 +50,40 @@
52
  <div id="prediction-box">
53
  <p>Prediction: <span id="prediction">N/A</span></p>
54
  </div>
55
-
56
- <button id="switch-camera-btn">Switch Camera</button>
57
 
58
  <script>
59
  const video = document.getElementById('video');
60
  const predictionElem = document.getElementById('prediction');
61
- const switchCameraBtn = document.getElementById('switch-camera-btn');
62
 
63
- let currentStream = null;
64
- let useFrontCamera = true; // Default to the front (selfie) camera
65
 
66
- // Function to start the video stream
67
- function startStream() {
68
- if (currentStream) {
69
- currentStream.getTracks().forEach(track => track.stop()); // Stop current stream if it exists
70
- }
71
-
72
- const constraints = {
73
  video: {
74
- facingMode: useFrontCamera ? 'user' : 'environment' // 'user' for front cam, 'environment' for back cam
75
  }
76
- };
77
-
78
- navigator.mediaDevices.getUserMedia(constraints)
79
- .then(stream => {
80
- currentStream = stream;
81
- video.srcObject = stream;
82
- })
83
- .catch(err => {
84
- console.error("Error accessing the camera:", err);
85
- });
86
  }
87
 
88
- // Call startStream initially to load the front camera by default
89
- startStream();
 
 
 
 
 
 
90
 
91
  // Capture a frame every second and send it to the backend
92
  setInterval(() => {
@@ -95,14 +92,11 @@
95
  canvas.height = video.videoHeight;
96
  const context = canvas.getContext('2d');
97
  context.drawImage(video, 0, 0, canvas.width, canvas.height);
98
- context.translate(canvas.width, 0);
99
- context.scale(-1, 1);
100
- context.drawImage(video, 0, 0, canvas.width, canvas.height);
101
-
102
  // Convert the image to base64
103
  const imageData = canvas.toDataURL('image/jpeg');
104
 
105
- // Send the image to the server
106
  fetch('/predict', {
107
  method: 'POST',
108
  headers: {
@@ -116,12 +110,6 @@
116
  })
117
  .catch(err => console.error('Error:', err));
118
  }, 1000);
119
-
120
- // Switch camera button functionality
121
- switchCameraBtn.addEventListener('click', () => {
122
- useFrontCamera = !useFrontCamera; // Toggle between front and back cameras
123
- startStream(); // Restart the stream with the new camera
124
- });
125
  </script>
126
  </body>
127
  </html>
 
13
  margin: 0;
14
  flex-direction: column;
15
  }
16
+
17
  #camera-box {
18
  width: 640px;
19
  height: 480px;
 
24
  align-items: center;
25
  position: relative;
26
  }
27
+
28
  video {
29
  width: 100%;
30
  height: 100%;
31
  object-fit: cover;
32
+ transform: scaleX(1); /* Start without mirroring for back camera */
33
  }
34
+
35
  #prediction-box {
36
  margin-top: 10px;
37
  text-align: center;
38
  }
39
 
40
+ #switch-camera {
41
+ margin-top: 20px;
 
 
42
  }
43
  </style>
44
  </head>
 
50
  <div id="prediction-box">
51
  <p>Prediction: <span id="prediction">N/A</span></p>
52
  </div>
53
+ <button id="switch-camera">Switch Camera</button>
 
54
 
55
  <script>
56
  const video = document.getElementById('video');
57
  const predictionElem = document.getElementById('prediction');
58
+ const switchCameraBtn = document.getElementById('switch-camera');
59
 
60
+ let useFrontCamera = true; // Start with front-facing camera
 
61
 
62
+ // Function to start the camera stream
63
+ function startCamera() {
64
+ navigator.mediaDevices.getUserMedia({
 
 
 
 
65
  video: {
66
+ facingMode: useFrontCamera ? 'user' : 'environment' // 'user' for front-facing, 'environment' for back camera
67
  }
68
+ })
69
+ .then(stream => {
70
+ video.srcObject = stream;
71
+ // Flip the video if using the front-facing camera
72
+ video.style.transform = useFrontCamera ? 'scaleX(-1)' : 'scaleX(1)';
73
+ })
74
+ .catch(err => {
75
+ console.error("Error accessing the camera:", err);
76
+ });
 
77
  }
78
 
79
+ // Start the camera with the front camera by default
80
+ startCamera();
81
+
82
+ // Switch camera button event listener
83
+ switchCameraBtn.addEventListener('click', () => {
84
+ useFrontCamera = !useFrontCamera; // Toggle camera mode
85
+ startCamera(); // Restart camera with the new mode
86
+ });
87
 
88
  // Capture a frame every second and send it to the backend
89
  setInterval(() => {
 
92
  canvas.height = video.videoHeight;
93
  const context = canvas.getContext('2d');
94
  context.drawImage(video, 0, 0, canvas.width, canvas.height);
95
+
 
 
 
96
  // Convert the image to base64
97
  const imageData = canvas.toDataURL('image/jpeg');
98
 
99
+ // Send the image to the server for prediction
100
  fetch('/predict', {
101
  method: 'POST',
102
  headers: {
 
110
  })
111
  .catch(err => console.error('Error:', err));
112
  }, 1000);
 
 
 
 
 
 
113
  </script>
114
  </body>
115
  </html>