Update src/streamlit_app.py
Browse files- src/streamlit_app.py +39 -42
src/streamlit_app.py
CHANGED
@@ -5,84 +5,81 @@ import uuid
|
|
5 |
import urllib.parse
|
6 |
import pytz
|
7 |
from datetime import datetime
|
8 |
-
from ocr_engine import extract_weight_from_image #
|
9 |
|
10 |
# Setup
|
11 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
12 |
st.title("βοΈ Auto Weight Logger")
|
13 |
|
14 |
-
#
|
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 |
-
#
|
25 |
if st.button("π Clear / Retake"):
|
26 |
st.session_state.camera_key = str(uuid.uuid4())
|
27 |
-
st.rerun()
|
28 |
|
|
|
29 |
image_bytes = None
|
30 |
image = None
|
31 |
|
32 |
-
# Camera input
|
33 |
if input_mode == "Camera":
|
34 |
-
cam_photo = st.camera_input("π·
|
35 |
-
if cam_photo
|
36 |
image_bytes = cam_photo.getvalue()
|
37 |
|
38 |
-
# Upload input
|
39 |
elif input_mode == "Upload":
|
40 |
-
uploaded_file = st.file_uploader("π Upload
|
41 |
-
if uploaded_file
|
42 |
-
|
43 |
-
|
44 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
59 |
weight, confidence = extract_weight_from_image(image)
|
60 |
|
|
|
|
|
|
|
|
|
61 |
if not weight or confidence < 80:
|
62 |
-
st.
|
63 |
else:
|
64 |
-
|
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 |
-
|
72 |
-
|
73 |
-
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
|
81 |
-
|
82 |
-
|
83 |
|
84 |
except UnidentifiedImageError:
|
85 |
-
st.error("β
|
86 |
except Exception as e:
|
87 |
-
st.error("β
|
88 |
st.exception(e)
|
|
|
5 |
import urllib.parse
|
6 |
import pytz
|
7 |
from datetime import datetime
|
8 |
+
from ocr_engine import extract_weight_from_image # Make sure this returns (weight, confidence)
|
9 |
|
10 |
# Setup
|
11 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
12 |
st.title("βοΈ Auto Weight Logger")
|
13 |
|
14 |
+
# Session states
|
|
|
|
|
|
|
15 |
if "camera_key" not in st.session_state:
|
16 |
st.session_state.camera_key = str(uuid.uuid4())
|
17 |
|
18 |
# Input method
|
19 |
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
20 |
|
21 |
+
# Clear button
|
22 |
if st.button("π Clear / Retake"):
|
23 |
st.session_state.camera_key = str(uuid.uuid4())
|
24 |
+
st.rerun()
|
25 |
|
26 |
+
# Image bytes and capture
|
27 |
image_bytes = None
|
28 |
image = None
|
29 |
|
|
|
30 |
if input_mode == "Camera":
|
31 |
+
cam_photo = st.camera_input("π· Capture weight display", key=st.session_state.camera_key)
|
32 |
+
if cam_photo:
|
33 |
image_bytes = cam_photo.getvalue()
|
34 |
|
|
|
35 |
elif input_mode == "Upload":
|
36 |
+
uploaded_file = st.file_uploader("π Upload Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
37 |
+
if uploaded_file:
|
38 |
+
image_bytes = uploaded_file.read()
|
39 |
+
|
40 |
+
# Process image
|
|
|
|
|
|
|
|
|
41 |
if image_bytes:
|
42 |
try:
|
43 |
image = Image.open(io.BytesIO(image_bytes))
|
44 |
+
|
45 |
+
# Timestamp
|
46 |
+
ist = pytz.timezone("Asia/Kolkata")
|
47 |
+
captured_time = datetime.now(ist).strftime("%Y-%m-%d %I:%M:%S %p")
|
48 |
+
|
49 |
+
st.subheader("πΌοΈ Snapshot Image")
|
50 |
+
st.image(image, use_column_width=True)
|
51 |
|
52 |
if len(image_bytes) > 5 * 1024 * 1024:
|
53 |
+
st.error("β Image too large (>5MB). Please use a smaller image.")
|
54 |
st.stop()
|
55 |
|
56 |
+
with st.spinner("π Extracting weight..."):
|
57 |
weight, confidence = extract_weight_from_image(image)
|
58 |
|
59 |
+
# Output regardless of confidence
|
60 |
+
st.markdown(f"### π¦ Captured Weight: `{weight if weight else 'Not Detected'}`")
|
61 |
+
st.markdown(f"### π Captured At (IST): `{captured_time}`")
|
62 |
+
|
63 |
if not weight or confidence < 80:
|
64 |
+
st.warning(f"β οΈ Low OCR Confidence ({int(confidence)}%). You may retry.")
|
65 |
else:
|
66 |
+
st.success(f"β
High Confidence: {int(confidence)}%")
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# Salesforce redirect link
|
69 |
+
device_id = "BAL-001"
|
70 |
+
image_url = "" # optional
|
71 |
|
72 |
+
salesforce_url = (
|
73 |
+
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
74 |
+
f"weight_logger_page?WeightInput={urllib.parse.quote(str(weight))}"
|
75 |
+
f"&DeviceID={urllib.parse.quote(device_id)}&ImageURL={urllib.parse.quote(image_url)}"
|
76 |
+
)
|
77 |
|
78 |
+
st.markdown("### β
Send to Salesforce")
|
79 |
+
st.markdown(f"[π€ Click to Log Weight in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
80 |
|
81 |
except UnidentifiedImageError:
|
82 |
+
st.error("β Invalid image format.")
|
83 |
except Exception as e:
|
84 |
+
st.error("β Something went wrong while processing the image.")
|
85 |
st.exception(e)
|