Spaces:
Sleeping
Sleeping
import streamlit as st | |
const video = document.getElementById('video'); | |
const canvas = document.getElementById('canvas'); | |
const output = document.getElementById('output'); | |
const captureButton = document.getElementById('capture'); | |
// Access the mobile camera | |
navigator.mediaDevices.getUserMedia({ video: true }) | |
.then(stream => { | |
video.srcObject = stream; | |
}) | |
.catch(err => { | |
console.error("Error accessing the camera: ", err); | |
}); | |
captureButton.addEventListener('click', () => { | |
// Draw the video frame to the canvas | |
canvas.width = video.videoWidth; | |
canvas.height = video.videoHeight; | |
const context = canvas.getContext('2d'); | |
context.drawImage(video, 0, 0, canvas.width, canvas.height); | |
// Convert canvas to image data | |
const dataURL = canvas.toDataURL('image/png'); | |
// Process the image with Tesseract | |
Tesseract.recognize( | |
dataURL, | |
'kor', | |
{ | |
logger: m => console.log(m) | |
} | |
).then(({ data: { text } }) => { | |
console.log(text); | |
analyzeNutrition(text); | |
}); | |
}); | |
function analyzeNutrition(text) { | |
// Extract nutritional values (assuming sugar content is labeled as '당류' in Korean) | |
const regex = /당류\s*:\s*(\d+(\.\d+)?)/; // This regex might need adjustments based on label format | |
const match = text.match(regex); | |
let outputDiv = document.getElementById('output'); | |
if (match) { | |
const sugarContent = parseFloat(match[1]); | |
let message = `Sugar content: ${sugarContent}g - `; | |
if (sugarContent > 20) { | |
message += 'Dangerous'; | |
outputDiv.className = 'red'; | |
} else if (sugarContent > 10) { | |
message += 'Normal'; | |
outputDiv.className = 'yellow'; | |
} else { | |
message += 'Good'; | |
outputDiv.className = 'green'; | |
} | |
outputDiv.textContent = message; | |
} else { | |
outputDiv.textContent = 'Sugar content not found'; | |
outputDiv.className = ''; | |
} | |
} |