Update src/streamlit_app.py
Browse files- src/streamlit_app.py +32 -8
src/streamlit_app.py
CHANGED
@@ -6,58 +6,82 @@ import urllib.parse
|
|
6 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
7 |
st.title("βοΈ Auto Weight Logger")
|
8 |
|
9 |
-
#
|
|
|
|
|
10 |
if "reset" not in st.session_state:
|
11 |
st.session_state.reset = False
|
12 |
|
13 |
-
#
|
|
|
|
|
14 |
if st.session_state.reset:
|
15 |
-
st.session_state.reset = False #
|
16 |
st.rerun()
|
17 |
|
18 |
-
#
|
|
|
|
|
19 |
if st.button("π Clear Photo"):
|
20 |
st.session_state.reset = True
|
21 |
-
st.
|
22 |
|
23 |
-
#
|
|
|
|
|
24 |
img_data = st.camera_input("π· Capture the weight display")
|
25 |
|
26 |
if img_data:
|
27 |
st.success("β
Image captured successfully!")
|
28 |
|
|
|
29 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
30 |
st.error("β Image too large (>5MB). Please try again.")
|
31 |
st.stop()
|
32 |
|
|
|
33 |
image = Image.open(img_data)
|
34 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
35 |
|
|
|
|
|
|
|
36 |
with st.spinner("π Extracting weight..."):
|
37 |
weight, confidence = extract_weight_from_image(image)
|
38 |
|
39 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
40 |
|
|
|
|
|
|
|
41 |
if not weight or confidence < 80:
|
42 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the image.")
|
43 |
if st.button("π Retry"):
|
44 |
st.session_state.reset = True
|
45 |
-
st.
|
46 |
st.stop()
|
47 |
|
|
|
|
|
|
|
48 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
49 |
|
|
|
50 |
device_id = "BAL-001"
|
51 |
-
image_url = "" # Optional
|
52 |
|
53 |
encoded_weight = urllib.parse.quote(str(weight))
|
54 |
encoded_device = urllib.parse.quote(device_id)
|
55 |
encoded_image_url = urllib.parse.quote(image_url)
|
56 |
|
|
|
57 |
salesforce_url = (
|
58 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
59 |
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
60 |
)
|
61 |
|
|
|
|
|
|
|
62 |
st.markdown("### π€ Send to Salesforce")
|
63 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
|
|
6 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
7 |
st.title("βοΈ Auto Weight Logger")
|
8 |
|
9 |
+
# ------------------------------
|
10 |
+
# β
Setup session state for clearing
|
11 |
+
# ------------------------------
|
12 |
if "reset" not in st.session_state:
|
13 |
st.session_state.reset = False
|
14 |
|
15 |
+
# ------------------------------
|
16 |
+
# π If reset is active, clear and rerun once
|
17 |
+
# ------------------------------
|
18 |
if st.session_state.reset:
|
19 |
+
st.session_state.reset = False # clear the flag
|
20 |
st.rerun()
|
21 |
|
22 |
+
# ------------------------------
|
23 |
+
# π Clear Photo Button
|
24 |
+
# ------------------------------
|
25 |
if st.button("π Clear Photo"):
|
26 |
st.session_state.reset = True
|
27 |
+
st.rerun()
|
28 |
|
29 |
+
# ------------------------------
|
30 |
+
# π· Capture Image from Webcam
|
31 |
+
# ------------------------------
|
32 |
img_data = st.camera_input("π· Capture the weight display")
|
33 |
|
34 |
if img_data:
|
35 |
st.success("β
Image captured successfully!")
|
36 |
|
37 |
+
# β
Optional: check file size
|
38 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
39 |
st.error("β Image too large (>5MB). Please try again.")
|
40 |
st.stop()
|
41 |
|
42 |
+
# Open and display image
|
43 |
image = Image.open(img_data)
|
44 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
45 |
|
46 |
+
# --------------------------
|
47 |
+
# π§ Extract weight via OCR
|
48 |
+
# --------------------------
|
49 |
with st.spinner("π Extracting weight..."):
|
50 |
weight, confidence = extract_weight_from_image(image)
|
51 |
|
52 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
53 |
|
54 |
+
# --------------------------
|
55 |
+
# β Retry if confidence low
|
56 |
+
# --------------------------
|
57 |
if not weight or confidence < 80:
|
58 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the image.")
|
59 |
if st.button("π Retry"):
|
60 |
st.session_state.reset = True
|
61 |
+
st.rerun()
|
62 |
st.stop()
|
63 |
|
64 |
+
# --------------------------
|
65 |
+
# β
Success β show result
|
66 |
+
# --------------------------
|
67 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
68 |
|
69 |
+
# Salesforce parameters
|
70 |
device_id = "BAL-001"
|
71 |
+
image_url = "" # Optional: set if you host the image
|
72 |
|
73 |
encoded_weight = urllib.parse.quote(str(weight))
|
74 |
encoded_device = urllib.parse.quote(device_id)
|
75 |
encoded_image_url = urllib.parse.quote(image_url)
|
76 |
|
77 |
+
# β
Salesforce VF Page URL
|
78 |
salesforce_url = (
|
79 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
80 |
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
81 |
)
|
82 |
|
83 |
+
# --------------------------
|
84 |
+
# π Display Salesforce link
|
85 |
+
# --------------------------
|
86 |
st.markdown("### π€ Send to Salesforce")
|
87 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|