Spaces:
Sleeping
Sleeping
Delete Tesseract.js
Browse files- Tesseract.js +0 -66
Tesseract.js
DELETED
@@ -1,66 +0,0 @@
|
|
1 |
-
import { pipeline, env } from 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
|
2 |
-
|
3 |
-
const video = document.getElementById('video');
|
4 |
-
const canvas = document.getElementById('canvas');
|
5 |
-
const output = document.getElementById('output');
|
6 |
-
const captureButton = document.getElementById('capture');
|
7 |
-
|
8 |
-
// Access the mobile camera
|
9 |
-
navigator.mediaDevices.getUserMedia({ video: true })
|
10 |
-
.then(stream => {
|
11 |
-
video.srcObject = stream;
|
12 |
-
})
|
13 |
-
.catch(err => {
|
14 |
-
console.error("Error accessing the camera: ", err);
|
15 |
-
});
|
16 |
-
|
17 |
-
captureButton.addEventListener('click', (m) => {
|
18 |
-
// Draw the video frame to the canvas
|
19 |
-
canvas.width = video.videoWidth;
|
20 |
-
canvas.height = video.videoHeight;
|
21 |
-
const context = canvas.getContext('2d');
|
22 |
-
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
23 |
-
|
24 |
-
// Convert canvas to image data
|
25 |
-
const dataURL = canvas.toDataURL('image/png');
|
26 |
-
|
27 |
-
// Process the image with Tesseract
|
28 |
-
Tesseract.recognize(
|
29 |
-
dataURL,
|
30 |
-
'kor',
|
31 |
-
{
|
32 |
-
logger: m => console.log(m)
|
33 |
-
}
|
34 |
-
).then(({ data: { text } }) => {
|
35 |
-
console.log(text);
|
36 |
-
analyzeNutrition(text);
|
37 |
-
});
|
38 |
-
});
|
39 |
-
|
40 |
-
function analyzeNutrition(text) {
|
41 |
-
// Extract nutritional values (assuming sugar content is labeled as '당류' in Korean)
|
42 |
-
const regex = /당류\s*:\s*(\d+(\.\d+)?)/; // This regex might need adjustments based on label format
|
43 |
-
const match = text.match(regex);
|
44 |
-
let outputDiv = document.getElementById('output');
|
45 |
-
|
46 |
-
if (match) {
|
47 |
-
const sugarContent = parseFloat(match[1]);
|
48 |
-
let message = `Sugar content: ${sugarContent}g - `;
|
49 |
-
|
50 |
-
if (sugarContent > 20) {
|
51 |
-
message += 'Dangerous';
|
52 |
-
outputDiv.className = 'red';
|
53 |
-
} else if (sugarContent > 10) {
|
54 |
-
message += 'Normal';
|
55 |
-
outputDiv.className = 'yellow';
|
56 |
-
} else {
|
57 |
-
message += 'Good';
|
58 |
-
outputDiv.className = 'green';
|
59 |
-
}
|
60 |
-
|
61 |
-
outputDiv.textContent = message;
|
62 |
-
} else {
|
63 |
-
outputDiv.textContent = 'Sugar content not found';
|
64 |
-
outputDiv.className = '';
|
65 |
-
}
|
66 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|