Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +26 -30
src/streamlit_app.py
CHANGED
|
@@ -2,38 +2,38 @@ import streamlit as st
|
|
| 2 |
from PIL import Image
|
| 3 |
from ocr_engine import extract_weight_from_image
|
| 4 |
import urllib.parse
|
|
|
|
| 5 |
|
| 6 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
| 7 |
st.title("βοΈ Auto Weight Logger")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
if "image_data" not in st.session_state:
|
| 11 |
st.session_state.image_data = None
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
# Clear
|
| 14 |
if st.button("π Clear / Retake Photo"):
|
| 15 |
st.session_state.image_data = None
|
|
|
|
| 16 |
|
| 17 |
-
# Show camera
|
| 18 |
if st.session_state.image_data is None:
|
| 19 |
-
img_data = st.camera_input("π· Capture the weight display")
|
| 20 |
if img_data:
|
| 21 |
st.session_state.image_data = img_data
|
| 22 |
-
st.experimental_rerun() # NOTE: Works fine in most Streamlit cloud spaces
|
| 23 |
|
| 24 |
-
#
|
| 25 |
if st.session_state.image_data:
|
| 26 |
st.success("β
Image captured successfully!")
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
| 31 |
st.error("β Image too large (>5MB). Please try again.")
|
| 32 |
st.stop()
|
| 33 |
|
| 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 |
|
|
@@ -41,25 +41,21 @@ if st.session_state.image_data:
|
|
| 41 |
|
| 42 |
if not weight or confidence < 80:
|
| 43 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
st.experimental_rerun()
|
| 47 |
-
st.stop()
|
| 48 |
-
|
| 49 |
-
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
image_url = ""
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
| 62 |
-
)
|
| 63 |
|
| 64 |
-
st.
|
| 65 |
-
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
from ocr_engine import extract_weight_from_image
|
| 4 |
import urllib.parse
|
| 5 |
+
import uuid
|
| 6 |
|
| 7 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
| 8 |
st.title("βοΈ Auto Weight Logger")
|
| 9 |
|
| 10 |
+
# Session state
|
| 11 |
if "image_data" not in st.session_state:
|
| 12 |
st.session_state.image_data = None
|
| 13 |
+
if "camera_key" not in st.session_state:
|
| 14 |
+
st.session_state.camera_key = str(uuid.uuid4())
|
| 15 |
|
| 16 |
+
# Clear / Retake
|
| 17 |
if st.button("π Clear / Retake Photo"):
|
| 18 |
st.session_state.image_data = None
|
| 19 |
+
st.session_state.camera_key = str(uuid.uuid4()) # new key to force reload
|
| 20 |
|
| 21 |
+
# Show camera
|
| 22 |
if st.session_state.image_data is None:
|
| 23 |
+
img_data = st.camera_input("π· Capture the weight display", key=st.session_state.camera_key)
|
| 24 |
if img_data:
|
| 25 |
st.session_state.image_data = img_data
|
|
|
|
| 26 |
|
| 27 |
+
# Process image
|
| 28 |
if st.session_state.image_data:
|
| 29 |
st.success("β
Image captured successfully!")
|
| 30 |
+
image = Image.open(st.session_state.image_data)
|
| 31 |
+
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
| 32 |
|
| 33 |
+
if len(st.session_state.image_data.getvalue()) > 5 * 1024 * 1024:
|
|
|
|
|
|
|
| 34 |
st.error("β Image too large (>5MB). Please try again.")
|
| 35 |
st.stop()
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
with st.spinner("π Extracting weight..."):
|
| 38 |
weight, confidence = extract_weight_from_image(image)
|
| 39 |
|
|
|
|
| 41 |
|
| 42 |
if not weight or confidence < 80:
|
| 43 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
|
| 44 |
+
else:
|
| 45 |
+
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
device_id = "BAL-001"
|
| 48 |
+
image_url = ""
|
|
|
|
| 49 |
|
| 50 |
+
salesforce_url = (
|
| 51 |
+
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
| 52 |
+
f"weight_logger_page?WeightInput={urllib.parse.quote(str(weight))}"
|
| 53 |
+
f"&DeviceID={urllib.parse.quote(device_id)}&ImageURL={urllib.parse.quote(image_url)}"
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
st.markdown("### π€ Send to Salesforce")
|
| 57 |
+
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
if st.button("π Retake Photo"):
|
| 60 |
+
st.session_state.image_data = None
|
| 61 |
+
st.session_state.camera_key = str(uuid.uuid4())
|