Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +59 -26
src/streamlit_app.py
CHANGED
@@ -1,30 +1,63 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
|
|
|
|
3 |
|
4 |
-
st.
|
5 |
-
|
6 |
-
# Session state to track if image is captured
|
7 |
-
if 'captured' not in st.session_state:
|
8 |
-
st.session_state.captured = False
|
9 |
-
if 'image' not in st.session_state:
|
10 |
-
st.session_state.image = None
|
11 |
-
|
12 |
-
# Function to reset camera
|
13 |
-
def retake():
|
14 |
-
st.session_state.captured = False
|
15 |
-
st.session_state.image = None
|
16 |
-
|
17 |
-
# Only show camera if image is not captured
|
18 |
-
if not st.session_state.captured:
|
19 |
-
image = st.camera_input("Take a picture")
|
20 |
-
if image:
|
21 |
-
st.session_state.image = image
|
22 |
-
st.session_state.captured = True
|
23 |
-
|
24 |
-
# If image is captured, show options
|
25 |
-
if st.session_state.captured and st.session_state.image:
|
26 |
-
st.image(st.session_state.image, caption="Captured Image", use_column_width=True)
|
27 |
-
st.button("Retake", on_click=retake)
|
28 |
-
st.success("β
Image captured successfully.")
|
29 |
-
# You can add OCR or Salesforce upload button here
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
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 |
+
# β
Session state to control camera
|
10 |
+
if "retake" not in st.session_state:
|
11 |
+
st.session_state.retake = False
|
12 |
+
|
13 |
+
# π Retake Button
|
14 |
+
if st.button("π Clear / Retake Photo"):
|
15 |
+
st.session_state.retake = True
|
16 |
+
st.rerun()
|
17 |
+
|
18 |
+
# β
If this is the rerun after clearing, reset and skip camera once
|
19 |
+
if st.session_state.retake:
|
20 |
+
st.session_state.retake = False
|
21 |
+
st.rerun()
|
22 |
+
|
23 |
+
# β
At this point, camera will always be shown
|
24 |
+
img_data = st.camera_input("π· Capture the weight display")
|
25 |
+
|
26 |
+
if img_data:
|
27 |
+
st.success("β
Image captured successfully!")
|
28 |
+
|
29 |
+
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
30 |
+
st.error("β Image too large (>5MB). Please try again.")
|
31 |
+
st.stop()
|
32 |
+
|
33 |
+
image = Image.open(img_data)
|
34 |
+
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
35 |
+
|
36 |
+
with st.spinner("π Extracting weight..."):
|
37 |
+
weight, confidence = extract_weight_from_image(image)
|
38 |
+
|
39 |
+
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
40 |
+
|
41 |
+
if not weight or confidence < 80:
|
42 |
+
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
|
43 |
+
if st.button("π Retake Photo"):
|
44 |
+
st.session_state.retake = True
|
45 |
+
st.rerun()
|
46 |
+
st.stop()
|
47 |
+
|
48 |
+
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
49 |
+
|
50 |
+
device_id = "BAL-001"
|
51 |
+
image_url = "" # Optional
|
52 |
+
|
53 |
+
encoded_weight = urllib.parse.quote(str(weight))
|
54 |
+
encoded_device = urllib.parse.quote(device_id)
|
55 |
+
encoded_image_url = urllib.parse.quote(image_url)
|
56 |
+
|
57 |
+
salesforce_url = (
|
58 |
+
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
59 |
+
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
60 |
+
)
|
61 |
+
|
62 |
+
st.markdown("### π€ Send to Salesforce")
|
63 |
+
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|