Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +20 -28
src/streamlit_app.py
CHANGED
@@ -6,30 +6,24 @@ 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 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
st.
|
23 |
-
|
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!")
|
35 |
|
@@ -40,26 +34,24 @@ if img_data:
|
|
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 |
-
# β
|
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.
|
54 |
-
st.
|
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
|
63 |
|
64 |
encoded_weight = urllib.parse.quote(str(weight))
|
65 |
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 |
+
# β
Flag to reset camera input
|
10 |
+
if "retake_photo" not in st.session_state:
|
11 |
+
st.session_state.retake_photo = False
|
12 |
+
|
13 |
+
# π Retake Photo or Clear button logic
|
14 |
+
def clear_photo():
|
15 |
+
st.session_state.retake_photo = True
|
16 |
+
|
17 |
+
st.button("π Clear / Retake Photo", on_click=clear_photo)
|
18 |
+
|
19 |
+
# β
Skip camera this run, and reset for next
|
20 |
+
if st.session_state.retake_photo:
|
21 |
+
st.session_state.retake_photo = False # reset the flag
|
22 |
+
st.stop() # stop here β camera will appear on next run
|
23 |
+
|
|
|
|
|
24 |
# π· Show camera input
|
|
|
25 |
img_data = st.camera_input("π· Capture the weight display")
|
26 |
|
|
|
|
|
|
|
27 |
if img_data:
|
28 |
st.success("β
Image captured successfully!")
|
29 |
|
|
|
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 |
+
# β Low confidence β offer retake
|
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.retake_photo = True
|
47 |
+
st.stop()
|
48 |
st.stop()
|
49 |
|
50 |
+
# β
Success β show Salesforce link
|
51 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
52 |
|
|
|
53 |
device_id = "BAL-001"
|
54 |
+
image_url = "" # Optional hosted image URL
|
55 |
|
56 |
encoded_weight = urllib.parse.quote(str(weight))
|
57 |
encoded_device = urllib.parse.quote(device_id)
|