ilar06 commited on
Commit
0cbde15
1 Parent(s): 41a8187

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import cv2
3
+ import pytesseract
4
+ from PIL import Image
5
+ import numpy as np
6
+
7
+ st.title("Candy Label Scanner")
8
+
9
+ # Function to capture video from the camera
10
+ def capture_video():
11
+ cap = cv2.VideoCapture(0)
12
+ stframe = st.empty()
13
+
14
+ while cap.isOpened():
15
+ ret, frame = cap.read()
16
+ if not ret:
17
+ break
18
+
19
+ # Display the video frame
20
+ stframe.image(frame, channels="BGR")
21
+
22
+ # Capture the frame when the button is pressed
23
+ if st.button("Capture", key="capture_button"):
24
+ cap.release()
25
+ return frame
26
+
27
+ # Function to analyze the captured image for nutritional values
28
+ def analyze_image(image):
29
+ # Convert the image to grayscale
30
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
31
+
32
+ # Use Tesseract to do OCR on the image
33
+ text = pytesseract.image_to_string(gray, lang='kor')
34
+
35
+ # Display the extracted text
36
+ st.subheader("Extracted Text")
37
+ st.text(text)
38
+
39
+ # Extract the sugar content using a regex pattern
40
+ import re
41
+ pattern = re.compile(r"당류\s*:\s*(\d+(\.\d+)?)\s*g")
42
+ match = pattern.search(text)
43
+
44
+ if match:
45
+ sugar_content = float(match.group(1))
46
+ st.subheader("Sugar Content")
47
+ st.write(f"Sugar content: {sugar_content}g")
48
+
49
+ # Determine the potential harm based on the sugar content
50
+ if sugar_content > 20:
51
+ st.markdown('<p style="color:red;">Dangerous</p>', unsafe_allow_html=True)
52
+ elif sugar_content > 10:
53
+ st.markdown('<p style="color:yellow;">Normal</p>', unsafe_allow_html=True)
54
+ else:
55
+ st.markdown('<p style="color:green;">Good</p>', unsafe_allow_html=True)
56
+ else:
57
+ st.write("Sugar content not found")
58
+
59
+ # Main application logic
60
+ if __name__ == "__main__":
61
+ st.write("Click 'Start Camera' to scan a candy label")
62
+ if st.button("Start Camera"):
63
+ frame = capture_video()
64
+ if frame is not None:
65
+ analyze_image(frame)