Update src/streamlit_app.py
Browse files- src/streamlit_app.py +20 -10
src/streamlit_app.py
CHANGED
@@ -10,28 +10,38 @@ st.title("βοΈ Auto Weight Logger")
|
|
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 |
-
#
|
|
|
|
|
|
|
17 |
if st.button("π Clear / Retake Photo"):
|
18 |
st.session_state.image_data = None
|
19 |
-
st.session_state.camera_key = str(uuid.uuid4()) #
|
20 |
|
21 |
-
#
|
22 |
if st.session_state.image_data is None:
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
if img_data:
|
25 |
st.session_state.image_data = img_data
|
26 |
|
27 |
-
#
|
28 |
if st.session_state.image_data:
|
29 |
-
st.success("β
Image
|
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
|
35 |
st.stop()
|
36 |
|
37 |
with st.spinner("π Extracting weight..."):
|
@@ -40,12 +50,12 @@ if st.session_state.image_data:
|
|
40 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
41 |
|
42 |
if not weight or confidence < 80:
|
43 |
-
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please
|
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/"
|
@@ -56,6 +66,6 @@ if st.session_state.image_data:
|
|
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
|
60 |
st.session_state.image_data = None
|
61 |
st.session_state.camera_key = str(uuid.uuid4())
|
|
|
10 |
# Session state
|
11 |
if "image_data" not in st.session_state:
|
12 |
st.session_state.image_data = None
|
13 |
+
if "input_mode" not in st.session_state:
|
14 |
+
st.session_state.input_mode = "Camera"
|
15 |
if "camera_key" not in st.session_state:
|
16 |
st.session_state.camera_key = str(uuid.uuid4())
|
17 |
|
18 |
+
# Choose input mode
|
19 |
+
st.radio("πΈ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
|
20 |
+
|
21 |
+
# Reset image
|
22 |
if st.button("π Clear / Retake Photo"):
|
23 |
st.session_state.image_data = None
|
24 |
+
st.session_state.camera_key = str(uuid.uuid4()) # Reload webcam
|
25 |
|
26 |
+
# Input section
|
27 |
if st.session_state.image_data is None:
|
28 |
+
if st.session_state.input_mode == "Camera":
|
29 |
+
img_data = st.camera_input("π· Capture the weight display", key=st.session_state.camera_key)
|
30 |
+
else:
|
31 |
+
img_data = st.file_uploader("π Upload an image of the weight display", type=["jpg", "jpeg", "png"])
|
32 |
+
|
33 |
if img_data:
|
34 |
st.session_state.image_data = img_data
|
35 |
|
36 |
+
# OCR and Display
|
37 |
if st.session_state.image_data:
|
38 |
+
st.success("β
Image received successfully!")
|
39 |
image = Image.open(st.session_state.image_data)
|
40 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
41 |
|
42 |
+
# Check image size
|
43 |
if len(st.session_state.image_data.getvalue()) > 5 * 1024 * 1024:
|
44 |
+
st.error("β Image too large (>5MB). Please try a smaller file.")
|
45 |
st.stop()
|
46 |
|
47 |
with st.spinner("π Extracting weight..."):
|
|
|
50 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
51 |
|
52 |
if not weight or confidence < 80:
|
53 |
+
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please try again.")
|
54 |
else:
|
55 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
56 |
|
57 |
device_id = "BAL-001"
|
58 |
+
image_url = "" # Placeholder for now
|
59 |
|
60 |
salesforce_url = (
|
61 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
|
66 |
st.markdown("### π€ Send to Salesforce")
|
67 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
68 |
|
69 |
+
if st.button("π Retake / Upload Another"):
|
70 |
st.session_state.image_data = None
|
71 |
st.session_state.camera_key = str(uuid.uuid4())
|