Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +27 -21
src/streamlit_app.py
CHANGED
|
@@ -6,27 +6,31 @@ import urllib.parse
|
|
| 6 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
| 7 |
st.title("βοΈ Auto Weight Logger")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
st.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
if
|
| 21 |
-
|
| 22 |
-
st.
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
img_data = st.camera_input("π· Capture the weight display")
|
| 26 |
|
| 27 |
if img_data:
|
| 28 |
st.success("β
Image captured successfully!")
|
| 29 |
|
|
|
|
| 30 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
| 31 |
st.error("β Image too large (>5MB). Please try again.")
|
| 32 |
st.stop()
|
|
@@ -34,24 +38,26 @@ if img_data:
|
|
| 34 |
image = Image.open(img_data)
|
| 35 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
| 36 |
|
|
|
|
| 37 |
with st.spinner("π Extracting weight..."):
|
| 38 |
weight, confidence = extract_weight_from_image(image)
|
| 39 |
|
| 40 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
| 41 |
|
| 42 |
-
#
|
| 43 |
if not weight or confidence < 80:
|
| 44 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
|
| 45 |
if st.button("π Retake Photo"):
|
| 46 |
-
st.session_state.
|
| 47 |
-
st.
|
| 48 |
st.stop()
|
| 49 |
|
| 50 |
-
# β
|
| 51 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
| 52 |
|
|
|
|
| 53 |
device_id = "BAL-001"
|
| 54 |
-
image_url = "" # Optional
|
| 55 |
|
| 56 |
encoded_weight = urllib.parse.quote(str(weight))
|
| 57 |
encoded_device = urllib.parse.quote(device_id)
|
|
|
|
| 6 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
| 7 |
st.title("βοΈ Auto Weight Logger")
|
| 8 |
|
| 9 |
+
# --------------------------------
|
| 10 |
+
# β
Session state to manage reset
|
| 11 |
+
# --------------------------------
|
| 12 |
+
if "clear" not in st.session_state:
|
| 13 |
+
st.session_state.clear = False
|
| 14 |
+
|
| 15 |
+
# π Clear / Retake Photo Button
|
| 16 |
+
if st.button("π Clear / Retake Photo"):
|
| 17 |
+
st.session_state.clear = True
|
| 18 |
+
st.experimental_rerun() # or st.rerun() if supported
|
| 19 |
+
|
| 20 |
+
# π§Ό Skip camera input for one run if clearing
|
| 21 |
+
if st.session_state.clear:
|
| 22 |
+
st.session_state.clear = False
|
| 23 |
+
st.stop() # skip rest, rerun will show fresh input
|
| 24 |
+
|
| 25 |
+
# --------------------------------
|
| 26 |
+
# π· Camera Input
|
| 27 |
+
# --------------------------------
|
| 28 |
img_data = st.camera_input("π· Capture the weight display")
|
| 29 |
|
| 30 |
if img_data:
|
| 31 |
st.success("β
Image captured successfully!")
|
| 32 |
|
| 33 |
+
# β οΈ Optional: file size check
|
| 34 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
| 35 |
st.error("β Image too large (>5MB). Please try again.")
|
| 36 |
st.stop()
|
|
|
|
| 38 |
image = Image.open(img_data)
|
| 39 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
| 40 |
|
| 41 |
+
# π Run OCR
|
| 42 |
with st.spinner("π Extracting weight..."):
|
| 43 |
weight, confidence = extract_weight_from_image(image)
|
| 44 |
|
| 45 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
| 46 |
|
| 47 |
+
# β οΈ If confidence too low, allow retake
|
| 48 |
if not weight or confidence < 80:
|
| 49 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
|
| 50 |
if st.button("π Retake Photo"):
|
| 51 |
+
st.session_state.clear = True
|
| 52 |
+
st.experimental_rerun()
|
| 53 |
st.stop()
|
| 54 |
|
| 55 |
+
# β
Successful result
|
| 56 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
| 57 |
|
| 58 |
+
# π Salesforce redirection setup
|
| 59 |
device_id = "BAL-001"
|
| 60 |
+
image_url = "" # Optional: you can later host image here
|
| 61 |
|
| 62 |
encoded_weight = urllib.parse.quote(str(weight))
|
| 63 |
encoded_device = urllib.parse.quote(device_id)
|