Spaces:
Sleeping
Sleeping
Update ocr.html
Browse files
ocr.html
CHANGED
@@ -32,71 +32,5 @@
|
|
32 |
<canvas id="canvas" style="display: none;"></canvas>
|
33 |
<div id="output"></div>
|
34 |
|
35 |
-
<script>
|
36 |
-
const video = document.getElementById('video');
|
37 |
-
const canvas = document.getElementById('canvas');
|
38 |
-
const output = document.getElementById('output');
|
39 |
-
const captureButton = document.getElementById('capture');
|
40 |
-
|
41 |
-
// Access the mobile camera
|
42 |
-
navigator.mediaDevices.getUserMedia({ video: true })
|
43 |
-
.then(stream => {
|
44 |
-
video.srcObject = stream;
|
45 |
-
})
|
46 |
-
.catch(err => {
|
47 |
-
console.error("Error accessing the camera: ", err);
|
48 |
-
});
|
49 |
-
|
50 |
-
captureButton.addEventListener('click', () => {
|
51 |
-
// Draw the video frame to the canvas
|
52 |
-
canvas.width = video.videoWidth;
|
53 |
-
canvas.height = video.videoHeight;
|
54 |
-
const context = canvas.getContext('2d');
|
55 |
-
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
56 |
-
|
57 |
-
// Convert canvas to image data
|
58 |
-
const dataURL = canvas.toDataURL('image/png');
|
59 |
-
|
60 |
-
// Process the image with Tesseract
|
61 |
-
Tesseract.recognize(
|
62 |
-
dataURL,
|
63 |
-
'kor',
|
64 |
-
{
|
65 |
-
logger: m => console.log(m)
|
66 |
-
}
|
67 |
-
).then(({ data: { text } }) => {
|
68 |
-
console.log(text);
|
69 |
-
analyzeNutrition(text);
|
70 |
-
});
|
71 |
-
});
|
72 |
-
|
73 |
-
function analyzeNutrition(text) {
|
74 |
-
// Extract nutritional values (assuming sugar content is labeled as '당류' in Korean)
|
75 |
-
const regex = /당류\s*:\s*(\d+(\.\d+)?)/; // This regex might need adjustments based on label format
|
76 |
-
const match = text.match(regex);
|
77 |
-
let outputDiv = document.getElementById('output');
|
78 |
-
|
79 |
-
if (match) {
|
80 |
-
const sugarContent = parseFloat(match[1]);
|
81 |
-
let message = `Sugar content: ${sugarContent}g - `;
|
82 |
-
|
83 |
-
if (sugarContent > 20) {
|
84 |
-
message += 'Dangerous';
|
85 |
-
outputDiv.className = 'red';
|
86 |
-
} else if (sugarContent > 10) {
|
87 |
-
message += 'Normal';
|
88 |
-
outputDiv.className = 'yellow';
|
89 |
-
} else {
|
90 |
-
message += 'Good';
|
91 |
-
outputDiv.className = 'green';
|
92 |
-
}
|
93 |
-
|
94 |
-
outputDiv.textContent = message;
|
95 |
-
} else {
|
96 |
-
outputDiv.textContent = 'Sugar content not found';
|
97 |
-
outputDiv.className = '';
|
98 |
-
}
|
99 |
-
}
|
100 |
-
</script>
|
101 |
</body>
|
102 |
</html>
|
|
|
32 |
<canvas id="canvas" style="display: none;"></canvas>
|
33 |
<div id="output"></div>
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
</body>
|
36 |
</html>
|