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