File size: 1,966 Bytes
0cbde15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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)