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