devlim commited on
Commit
df85a4c
โ€ข
1 Parent(s): a0eab63

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +17 -32
templates/index.html CHANGED
@@ -83,7 +83,7 @@
83
  <h1>์„ฑ๋ถ„๋ถ„์„ํ‘œ</h1>
84
  <video id="video" autoplay></video>
85
  <button id="snap">์‚ฌ์ง„ ์ฐ๊ธฐ</button>
86
- <canvas id="canvas" style="display:none;"></canvas>
87
  <p id="analysisResult" class="result"></p>
88
  </div>
89
 
@@ -93,7 +93,7 @@
93
  const snap = document.getElementById('snap');
94
  const context = canvas.getContext('2d');
95
  const analysisResult = document.getElementById('analysisResult');
96
-
97
  navigator.mediaDevices.getUserMedia({ video: true })
98
  .then(stream => {
99
  video.srcObject = stream;
@@ -102,15 +102,6 @@
102
  console.error("Error accessing webcam: " + err);
103
  });
104
 
105
- function dataURLToFile(dataurl, filename) {
106
- let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
107
- bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
108
- while (n--) {
109
- u8arr[n] = bstr.charCodeAt(n);
110
- }
111
- return new File([u8arr], filename, { type: mime });
112
- }
113
-
114
  function processAnalysisResult(result) {
115
  const match = result.match(/\d+/);
116
  if (match) {
@@ -127,7 +118,7 @@
127
  message = 'dangerous';
128
  className = 'dangerous';
129
  }
130
- analysisResult.textContent = `${message}: ${sugarContent}g`;
131
  analysisResult.className = className;
132
  } else {
133
  analysisResult.textContent = 'Error: Could not analyze image.';
@@ -138,28 +129,22 @@
138
  snap.addEventListener('click', () => {
139
  context.drawImage(video, 0, 0, canvas.width, canvas.height);
140
  const dataURL = canvas.toDataURL('image/png');
141
- const file = dataURLToFile(dataURL, 'captured_image.png');
142
-
143
- const formData = new FormData();
144
- formData.append('image', file);
145
-
146
  fetch('/save_image', {
147
  method: 'POST',
148
- body: formData
149
- })
150
- .then(response => response.json())
151
- .then(data => {
152
- if (data.analysis_result) {
153
- processAnalysisResult(data.analysis_result);
154
- } else {
155
- analysisResult.textContent = 'Error: ์„ฑ๋ถ„๋ถ„์„ํ‘œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.';
156
- analysisResult.className = '';
157
- }
158
- alert(data.message);
159
- })
160
- .catch(error => {
161
- console.error('Error:', error);
162
- });
163
  });
164
  </script>
165
  </body>
 
83
  <h1>์„ฑ๋ถ„๋ถ„์„ํ‘œ</h1>
84
  <video id="video" autoplay></video>
85
  <button id="snap">์‚ฌ์ง„ ์ฐ๊ธฐ</button>
86
+ <canvas id="canvas" style="display: none;"></canvas>
87
  <p id="analysisResult" class="result"></p>
88
  </div>
89
 
 
93
  const snap = document.getElementById('snap');
94
  const context = canvas.getContext('2d');
95
  const analysisResult = document.getElementById('analysisResult');
96
+
97
  navigator.mediaDevices.getUserMedia({ video: true })
98
  .then(stream => {
99
  video.srcObject = stream;
 
102
  console.error("Error accessing webcam: " + err);
103
  });
104
 
 
 
 
 
 
 
 
 
 
105
  function processAnalysisResult(result) {
106
  const match = result.match(/\d+/);
107
  if (match) {
 
118
  message = 'dangerous';
119
  className = 'dangerous';
120
  }
121
+ analysisResult.textContent = `${message} (${sugarContent}g)`;
122
  analysisResult.className = className;
123
  } else {
124
  analysisResult.textContent = 'Error: Could not analyze image.';
 
129
  snap.addEventListener('click', () => {
130
  context.drawImage(video, 0, 0, canvas.width, canvas.height);
131
  const dataURL = canvas.toDataURL('image/png');
 
 
 
 
 
132
  fetch('/save_image', {
133
  method: 'POST',
134
+ headers: {
135
+ 'Content-Type': 'application/json',
136
+ },
137
+ body: JSON.stringify({ image: dataURL })
138
+ }).then(response => response.json())
139
+ .then(data => {
140
+ if (data.analysis_result) {
141
+ processAnalysisResult(data.analysis_result);
142
+ } else {
143
+ analysisResult.textContent = 'Error: ์„ฑ๋ถ„๋ถ„์„ํ‘œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.';
144
+ analysisResult.className = '';
145
+ }
146
+ alert(data.message);
147
+ });
 
148
  });
149
  </script>
150
  </body>