ilar06 commited on
Commit
af4d7af
1 Parent(s): bb76bd2

Create index.html

Browse files
Files changed (1) hide show
  1. index.html +104 -0
index.html ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/@tensorflow/tfjs"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script>
9
+ <script src="https://cdn.jsdelivr.net/npm/tesseract.js"></script>
10
+ <style>
11
+ #output {
12
+ font-size: 20px;
13
+ margin-top: 20px;
14
+ }
15
+ .red {
16
+ color: red;
17
+ }
18
+ .yellow {
19
+ color: yellow;
20
+ }
21
+ .green {
22
+ color: green;
23
+ }
24
+ video {
25
+ width: 100%;
26
+ height: auto;
27
+ }
28
+ </style>
29
+ </head>
30
+ <body>
31
+ <h1>Candy Label Scanner</h1>
32
+ <video id="video" autoplay></video>
33
+ <button id="capture">Capture</button>
34
+ <canvas id="canvas" style="display: none;"></canvas>
35
+ <div id="output"></div>
36
+
37
+ <script>
38
+ const video = document.getElementById('video');
39
+ const canvas = document.getElementById('canvas');
40
+ const output = document.getElementById('output');
41
+ const captureButton = document.getElementById('capture');
42
+
43
+ // Access the mobile camera
44
+ navigator.mediaDevices.getUserMedia({ video: true })
45
+ .then(stream => {
46
+ video.srcObject = stream;
47
+ })
48
+ .catch(err => {
49
+ console.error("Error accessing the camera: ", err);
50
+ });
51
+
52
+ captureButton.addEventListener('click', () => {
53
+ // Draw the video frame to the canvas
54
+ canvas.width = video.videoWidth;
55
+ canvas.height = video.videoHeight;
56
+ const context = canvas.getContext('2d');
57
+ context.drawImage(video, 0, 0, canvas.width, canvas.height);
58
+
59
+ // Convert canvas to image data
60
+ const dataURL = canvas.toDataURL('image/png');
61
+
62
+ // Process the image with Tesseract
63
+ Tesseract.recognize(
64
+ dataURL,
65
+ 'kor',
66
+ {
67
+ logger: m => console.log(m)
68
+ }
69
+ ).then(({ data: { text } }) => {
70
+ console.log(text);
71
+ analyzeNutrition(text);
72
+ });
73
+ });
74
+
75
+ function analyzeNutrition(text) {
76
+ // Extract nutritional values (assuming sugar content is labeled as '당류' in Korean)
77
+ const regex = /당류\s*:\s*(\d+(\.\d+)?)/; // This regex might need adjustments based on label format
78
+ const match = text.match(regex);
79
+ let outputDiv = document.getElementById('output');
80
+
81
+ if (match) {
82
+ const sugarContent = parseFloat(match[1]);
83
+ let message = `Sugar content: ${sugarContent}g - `;
84
+
85
+ if (sugarContent > 20) {
86
+ message += 'Dangerous';
87
+ outputDiv.className = 'red';
88
+ } else if (sugarContent > 10) {
89
+ message += 'Normal';
90
+ outputDiv.className = 'yellow';
91
+ } else {
92
+ message += 'Good';
93
+ outputDiv.className = 'green';
94
+ }
95
+
96
+ outputDiv.textContent = message;
97
+ } else {
98
+ outputDiv.textContent = 'Sugar content not found';
99
+ outputDiv.className = '';
100
+ }
101
+ }
102
+ </script>
103
+ </body>
104
+ </html>