Spaces:
Sleeping
Sleeping
Update ocr.html
Browse files
ocr.html
CHANGED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Candy Label Scanner</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/tesseract.js@2.1.1/dist/tesseract.min.js"></script>
|
| 8 |
+
<style>
|
| 9 |
+
#output {
|
| 10 |
+
font-size: 20px;
|
| 11 |
+
margin-top: 20px;
|
| 12 |
+
}
|
| 13 |
+
.red {
|
| 14 |
+
color: red;
|
| 15 |
+
}
|
| 16 |
+
.yellow {
|
| 17 |
+
color: yellow;
|
| 18 |
+
}
|
| 19 |
+
.green {
|
| 20 |
+
color: green;
|
| 21 |
+
}
|
| 22 |
+
video {
|
| 23 |
+
width: 100%;
|
| 24 |
+
height: auto;
|
| 25 |
+
}
|
| 26 |
+
</style>
|
| 27 |
+
</head>
|
| 28 |
+
<body>
|
| 29 |
+
<h1>Candy Label Scanner</h1>
|
| 30 |
+
<video id="video" autoplay></video>
|
| 31 |
+
<button id="capture">Capture</button>
|
| 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>
|