Update src/streamlit_app.py
Browse files- src/streamlit_app.py +16 -17
src/streamlit_app.py
CHANGED
@@ -7,30 +7,28 @@ st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
|
7 |
st.title("βοΈ Auto Weight Logger")
|
8 |
|
9 |
# ------------------------------
|
10 |
-
# β
|
11 |
# ------------------------------
|
12 |
-
if "
|
13 |
-
st.session_state.
|
14 |
|
15 |
-
#
|
16 |
-
if st.
|
17 |
-
st.session_state.
|
18 |
st.rerun()
|
19 |
|
20 |
-
#
|
21 |
-
if
|
22 |
-
st.session_state.
|
23 |
st.rerun()
|
24 |
|
25 |
# ------------------------------
|
26 |
# π· Show camera input
|
27 |
# ------------------------------
|
28 |
-
img_data =
|
29 |
-
if st.session_state.show_camera:
|
30 |
-
img_data = st.camera_input("π· Capture the weight display")
|
31 |
|
32 |
# ------------------------------
|
33 |
-
# β
|
34 |
# ------------------------------
|
35 |
if img_data:
|
36 |
st.success("β
Image captured successfully!")
|
@@ -42,25 +40,26 @@ if img_data:
|
|
42 |
image = Image.open(img_data)
|
43 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
44 |
|
|
|
45 |
with st.spinner("π Extracting weight..."):
|
46 |
weight, confidence = extract_weight_from_image(image)
|
47 |
|
48 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
49 |
|
50 |
-
# β
|
51 |
if not weight or confidence < 80:
|
52 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
|
53 |
if st.button("π Retake Photo"):
|
54 |
-
st.session_state.
|
55 |
st.rerun()
|
56 |
st.stop()
|
57 |
|
58 |
# β
Success
|
59 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
60 |
|
61 |
-
# Salesforce
|
62 |
device_id = "BAL-001"
|
63 |
-
image_url = "" # Optional
|
64 |
|
65 |
encoded_weight = urllib.parse.quote(str(weight))
|
66 |
encoded_device = urllib.parse.quote(device_id)
|
|
|
7 |
st.title("βοΈ Auto Weight Logger")
|
8 |
|
9 |
# ------------------------------
|
10 |
+
# β
Setup session state for reset
|
11 |
# ------------------------------
|
12 |
+
if "reset" not in st.session_state:
|
13 |
+
st.session_state.reset = False
|
14 |
|
15 |
+
# π Handle reset if needed (immediately rerun and then show camera again)
|
16 |
+
if st.session_state.reset:
|
17 |
+
st.session_state.reset = False # Clear flag
|
18 |
st.rerun()
|
19 |
|
20 |
+
# π Clear / Retake button
|
21 |
+
if st.button("π Clear / Retake Photo"):
|
22 |
+
st.session_state.reset = True
|
23 |
st.rerun()
|
24 |
|
25 |
# ------------------------------
|
26 |
# π· Show camera input
|
27 |
# ------------------------------
|
28 |
+
img_data = st.camera_input("π· Capture the weight display")
|
|
|
|
|
29 |
|
30 |
# ------------------------------
|
31 |
+
# β
Handle image if captured
|
32 |
# ------------------------------
|
33 |
if img_data:
|
34 |
st.success("β
Image captured successfully!")
|
|
|
40 |
image = Image.open(img_data)
|
41 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
42 |
|
43 |
+
# π Extract weight via OCR
|
44 |
with st.spinner("π Extracting weight..."):
|
45 |
weight, confidence = extract_weight_from_image(image)
|
46 |
|
47 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
48 |
|
49 |
+
# β If low confidence, ask to retake
|
50 |
if not weight or confidence < 80:
|
51 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
|
52 |
if st.button("π Retake Photo"):
|
53 |
+
st.session_state.reset = True
|
54 |
st.rerun()
|
55 |
st.stop()
|
56 |
|
57 |
# β
Success
|
58 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
59 |
|
60 |
+
# Salesforce parameters
|
61 |
device_id = "BAL-001"
|
62 |
+
image_url = "" # Optional image hosting URL
|
63 |
|
64 |
encoded_weight = urllib.parse.quote(str(weight))
|
65 |
encoded_device = urllib.parse.quote(device_id)
|