Update src/streamlit_app.py
Browse files- src/streamlit_app.py +43 -49
src/streamlit_app.py
CHANGED
@@ -8,78 +8,72 @@ import io
|
|
8 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
9 |
st.title("βοΈ Auto Weight Logger")
|
10 |
|
11 |
-
#
|
12 |
-
if "
|
13 |
-
st.session_state.
|
14 |
if "input_mode" not in st.session_state:
|
15 |
st.session_state.input_mode = "Camera"
|
16 |
if "camera_key" not in st.session_state:
|
17 |
st.session_state.camera_key = str(uuid.uuid4())
|
18 |
|
19 |
-
#
|
20 |
st.radio("πΈ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
|
21 |
|
22 |
-
# Clear/reset
|
23 |
if st.button("π Clear / Retake Photo"):
|
24 |
-
st.session_state.
|
25 |
st.session_state.camera_key = str(uuid.uuid4())
|
26 |
|
27 |
# Get image input
|
28 |
-
|
|
|
29 |
|
30 |
-
if st.session_state.image_data is None:
|
31 |
if st.session_state.input_mode == "Camera":
|
32 |
uploaded_image = st.camera_input("π· Capture the weight display", key=st.session_state.camera_key)
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
st.
|
|
|
|
|
38 |
|
39 |
-
# Process
|
40 |
-
if st.session_state.
|
41 |
-
st.success("β
Image received successfully!")
|
42 |
-
|
43 |
-
# Convert to PIL image
|
44 |
try:
|
45 |
-
|
46 |
-
image =
|
47 |
-
except Exception as e:
|
48 |
-
st.error("β Failed to load image.")
|
49 |
-
st.stop()
|
50 |
|
51 |
-
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
st.error("β Image too large (>5MB). Please upload a smaller image.")
|
56 |
-
st.stop()
|
57 |
|
58 |
-
|
59 |
-
with st.spinner("π Extracting weight..."):
|
60 |
-
weight, confidence = extract_weight_from_image(image)
|
61 |
|
62 |
-
|
|
|
|
|
|
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
else:
|
67 |
-
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
f"weight_logger_page?WeightInput={urllib.parse.quote(str(weight))}"
|
76 |
-
f"&DeviceID={urllib.parse.quote(device_id)}&ImageURL={urllib.parse.quote(image_url)}"
|
77 |
-
)
|
78 |
|
79 |
-
st.
|
80 |
-
|
|
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
st.
|
85 |
-
st.session_state.camera_key = str(uuid.uuid4())
|
|
|
8 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
9 |
st.title("βοΈ Auto Weight Logger")
|
10 |
|
11 |
+
# State initialization
|
12 |
+
if "image_bytes" not in st.session_state:
|
13 |
+
st.session_state.image_bytes = None
|
14 |
if "input_mode" not in st.session_state:
|
15 |
st.session_state.input_mode = "Camera"
|
16 |
if "camera_key" not in st.session_state:
|
17 |
st.session_state.camera_key = str(uuid.uuid4())
|
18 |
|
19 |
+
# Input mode selector
|
20 |
st.radio("πΈ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
|
21 |
|
22 |
+
# Clear/reset
|
23 |
if st.button("π Clear / Retake Photo"):
|
24 |
+
st.session_state.image_bytes = None
|
25 |
st.session_state.camera_key = str(uuid.uuid4())
|
26 |
|
27 |
# Get image input
|
28 |
+
if st.session_state.image_bytes is None:
|
29 |
+
uploaded_image = None
|
30 |
|
|
|
31 |
if st.session_state.input_mode == "Camera":
|
32 |
uploaded_image = st.camera_input("π· Capture the weight display", key=st.session_state.camera_key)
|
33 |
+
if uploaded_image is not None:
|
34 |
+
st.session_state.image_bytes = uploaded_image.getvalue()
|
35 |
|
36 |
+
elif st.session_state.input_mode == "Upload":
|
37 |
+
uploaded_image = st.file_uploader("π Upload image of weight display", type=["jpg", "jpeg", "png"])
|
38 |
+
if uploaded_image is not None:
|
39 |
+
st.session_state.image_bytes = uploaded_image.read()
|
40 |
|
41 |
+
# Process and OCR
|
42 |
+
if st.session_state.image_bytes:
|
|
|
|
|
|
|
43 |
try:
|
44 |
+
image = Image.open(io.BytesIO(st.session_state.image_bytes))
|
45 |
+
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
|
|
|
|
|
|
46 |
|
47 |
+
if len(st.session_state.image_bytes) > 5 * 1024 * 1024:
|
48 |
+
st.error("β Image too large (>5MB). Please try again.")
|
49 |
+
st.stop()
|
50 |
|
51 |
+
with st.spinner("π Extracting weight..."):
|
52 |
+
weight, confidence = extract_weight_from_image(image)
|
|
|
|
|
53 |
|
54 |
+
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
|
|
|
|
55 |
|
56 |
+
if not weight or confidence < 80:
|
57 |
+
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please try again.")
|
58 |
+
else:
|
59 |
+
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
60 |
|
61 |
+
device_id = "BAL-001"
|
62 |
+
image_url = ""
|
|
|
|
|
63 |
|
64 |
+
salesforce_url = (
|
65 |
+
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
66 |
+
f"weight_logger_page?WeightInput={urllib.parse.quote(str(weight))}"
|
67 |
+
f"&DeviceID={urllib.parse.quote(device_id)}&ImageURL={urllib.parse.quote(image_url)}"
|
68 |
+
)
|
69 |
|
70 |
+
st.markdown("### π€ Send to Salesforce")
|
71 |
+
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
|
|
|
|
|
|
72 |
|
73 |
+
if st.button("π Retake / Upload Another"):
|
74 |
+
st.session_state.image_bytes = None
|
75 |
+
st.session_state.camera_key = str(uuid.uuid4())
|
76 |
|
77 |
+
except Exception as e:
|
78 |
+
st.error("β Failed to load or process image.")
|
79 |
+
st.exception(e)
|
|