Update src/streamlit_app.py
Browse files- src/streamlit_app.py +33 -20
src/streamlit_app.py
CHANGED
@@ -1,65 +1,76 @@
|
|
1 |
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
import io
|
4 |
import uuid
|
5 |
import urllib.parse
|
|
|
|
|
6 |
from ocr_engine import extract_weight_from_image # your OCR function
|
7 |
|
8 |
# Setup
|
9 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
10 |
st.title("βοΈ Auto Weight Logger")
|
11 |
|
12 |
-
#
|
|
|
|
|
|
|
13 |
if "camera_key" not in st.session_state:
|
14 |
st.session_state.camera_key = str(uuid.uuid4())
|
15 |
|
16 |
-
# Input
|
17 |
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
18 |
|
19 |
-
#
|
20 |
if st.button("π Clear / Retake"):
|
21 |
st.session_state.camera_key = str(uuid.uuid4())
|
22 |
st.experimental_rerun()
|
23 |
|
24 |
-
# Variables
|
25 |
image_bytes = None
|
26 |
image = None
|
27 |
|
28 |
-
# Camera
|
29 |
if input_mode == "Camera":
|
30 |
cam_photo = st.camera_input("π· Take a photo of the weight display", key=st.session_state.camera_key)
|
31 |
if cam_photo is not None:
|
32 |
image_bytes = cam_photo.getvalue()
|
33 |
|
34 |
-
# Upload
|
35 |
elif input_mode == "Upload":
|
36 |
-
uploaded_file = st.file_uploader("π Upload
|
37 |
if uploaded_file is not None:
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
#
|
41 |
if image_bytes:
|
42 |
try:
|
43 |
image = Image.open(io.BytesIO(image_bytes))
|
44 |
-
st.image(image, caption="πΈ
|
45 |
|
46 |
if len(image_bytes) > 5 * 1024 * 1024:
|
47 |
-
st.error("β Image
|
48 |
st.stop()
|
49 |
|
50 |
with st.spinner("π Extracting weight using OCR..."):
|
51 |
weight, confidence = extract_weight_from_image(image)
|
52 |
|
53 |
-
st.write(f"π οΈ DEBUG: Weight = {weight}, Confidence = {confidence}")
|
54 |
-
|
55 |
if not weight or confidence < 80:
|
56 |
-
st.error(f"β οΈ
|
57 |
else:
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
#
|
61 |
device_id = "BAL-001"
|
62 |
-
image_url = "" # optional if
|
63 |
|
64 |
salesforce_url = (
|
65 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
@@ -68,8 +79,10 @@ if image_bytes:
|
|
68 |
)
|
69 |
|
70 |
st.markdown("### π€ Send to Salesforce")
|
71 |
-
st.markdown(f"[β
|
72 |
|
|
|
|
|
73 |
except Exception as e:
|
74 |
-
st.error("β
|
75 |
st.exception(e)
|
|
|
1 |
import streamlit as st
|
2 |
+
from PIL import Image, UnidentifiedImageError
|
3 |
import io
|
4 |
import uuid
|
5 |
import urllib.parse
|
6 |
+
import pytz
|
7 |
+
from datetime import datetime
|
8 |
from ocr_engine import extract_weight_from_image # your OCR function
|
9 |
|
10 |
# Setup
|
11 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
12 |
st.title("βοΈ Auto Weight Logger")
|
13 |
|
14 |
+
# IST timezone
|
15 |
+
ist = pytz.timezone('Asia/Kolkata')
|
16 |
+
|
17 |
+
# Session state
|
18 |
if "camera_key" not in st.session_state:
|
19 |
st.session_state.camera_key = str(uuid.uuid4())
|
20 |
|
21 |
+
# Input method
|
22 |
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
23 |
|
24 |
+
# Reset
|
25 |
if st.button("π Clear / Retake"):
|
26 |
st.session_state.camera_key = str(uuid.uuid4())
|
27 |
st.experimental_rerun()
|
28 |
|
|
|
29 |
image_bytes = None
|
30 |
image = None
|
31 |
|
32 |
+
# Camera input
|
33 |
if input_mode == "Camera":
|
34 |
cam_photo = st.camera_input("π· Take a photo of the weight display", key=st.session_state.camera_key)
|
35 |
if cam_photo is not None:
|
36 |
image_bytes = cam_photo.getvalue()
|
37 |
|
38 |
+
# Upload input
|
39 |
elif input_mode == "Upload":
|
40 |
+
uploaded_file = st.file_uploader("π Upload a JPG/PNG image", type=["jpg", "jpeg", "png"])
|
41 |
if uploaded_file is not None:
|
42 |
+
try:
|
43 |
+
image_bytes = uploaded_file.read()
|
44 |
+
except Exception as e:
|
45 |
+
st.error("β Failed to read uploaded file.")
|
46 |
+
st.exception(e)
|
47 |
|
48 |
+
# Image display and OCR
|
49 |
if image_bytes:
|
50 |
try:
|
51 |
image = Image.open(io.BytesIO(image_bytes))
|
52 |
+
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
53 |
|
54 |
if len(image_bytes) > 5 * 1024 * 1024:
|
55 |
+
st.error("β Image too large (>5MB).")
|
56 |
st.stop()
|
57 |
|
58 |
with st.spinner("π Extracting weight using OCR..."):
|
59 |
weight, confidence = extract_weight_from_image(image)
|
60 |
|
|
|
|
|
61 |
if not weight or confidence < 80:
|
62 |
+
st.error(f"β οΈ OCR Confidence too low ({int(confidence)}%). Try again.")
|
63 |
else:
|
64 |
+
# IST time
|
65 |
+
captured_time = datetime.now(ist).strftime("%Y-%m-%d %I:%M:%S %p")
|
66 |
+
|
67 |
+
st.success("β
OCR Success")
|
68 |
+
st.markdown(f"### π¦ Captured Weight: `{weight} g`")
|
69 |
+
st.markdown(f"### π Captured At (IST): `{captured_time}`")
|
70 |
|
71 |
+
# Salesforce link
|
72 |
device_id = "BAL-001"
|
73 |
+
image_url = "" # optional if stored externally
|
74 |
|
75 |
salesforce_url = (
|
76 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
|
79 |
)
|
80 |
|
81 |
st.markdown("### π€ Send to Salesforce")
|
82 |
+
st.markdown(f"[β
Click to Log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
83 |
|
84 |
+
except UnidentifiedImageError:
|
85 |
+
st.error("β Unsupported or invalid image format.")
|
86 |
except Exception as e:
|
87 |
+
st.error("β Unexpected error processing image.")
|
88 |
st.exception(e)
|