GlucolorPYTHON / app.py
ilar06's picture
Create app.py
0cbde15 verified
raw
history blame
1.97 kB
import streamlit as st
import cv2
import pytesseract
from PIL import Image
import numpy as np
st.title("Candy Label Scanner")
# Function to capture video from the camera
def capture_video():
cap = cv2.VideoCapture(0)
stframe = st.empty()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Display the video frame
stframe.image(frame, channels="BGR")
# Capture the frame when the button is pressed
if st.button("Capture", key="capture_button"):
cap.release()
return frame
# Function to analyze the captured image for nutritional values
def analyze_image(image):
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Use Tesseract to do OCR on the image
text = pytesseract.image_to_string(gray, lang='kor')
# Display the extracted text
st.subheader("Extracted Text")
st.text(text)
# Extract the sugar content using a regex pattern
import re
pattern = re.compile(r"당류\s*:\s*(\d+(\.\d+)?)\s*g")
match = pattern.search(text)
if match:
sugar_content = float(match.group(1))
st.subheader("Sugar Content")
st.write(f"Sugar content: {sugar_content}g")
# Determine the potential harm based on the sugar content
if sugar_content > 20:
st.markdown('<p style="color:red;">Dangerous</p>', unsafe_allow_html=True)
elif sugar_content > 10:
st.markdown('<p style="color:yellow;">Normal</p>', unsafe_allow_html=True)
else:
st.markdown('<p style="color:green;">Good</p>', unsafe_allow_html=True)
else:
st.write("Sugar content not found")
# Main application logic
if __name__ == "__main__":
st.write("Click 'Start Camera' to scan a candy label")
if st.button("Start Camera"):
frame = capture_video()
if frame is not None:
analyze_image(frame)