Update src/streamlit_app.py
Browse files- src/streamlit_app.py +27 -27
src/streamlit_app.py
CHANGED
@@ -3,63 +3,63 @@ from PIL import Image
|
|
3 |
import io
|
4 |
import uuid
|
5 |
import urllib.parse
|
6 |
-
from ocr_engine import extract_weight_from_image #
|
7 |
|
|
|
8 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
9 |
st.title("βοΈ Auto Weight Logger")
|
10 |
|
11 |
-
#
|
12 |
if "camera_key" not in st.session_state:
|
13 |
st.session_state.camera_key = str(uuid.uuid4())
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
|
18 |
-
#
|
19 |
if st.button("π Clear / Retake"):
|
20 |
st.session_state.camera_key = str(uuid.uuid4())
|
21 |
st.experimental_rerun()
|
22 |
|
23 |
-
#
|
24 |
image_bytes = None
|
25 |
image = None
|
26 |
|
27 |
-
#
|
28 |
-
if
|
29 |
-
|
30 |
-
if
|
31 |
-
image_bytes =
|
32 |
|
33 |
-
#
|
34 |
-
elif
|
35 |
-
|
36 |
-
if
|
37 |
-
image_bytes =
|
38 |
|
39 |
-
#
|
40 |
if image_bytes:
|
41 |
try:
|
42 |
image = Image.open(io.BytesIO(image_bytes))
|
43 |
-
st.image(image, caption="πΈ
|
44 |
|
45 |
if len(image_bytes) > 5 * 1024 * 1024:
|
46 |
-
st.error("β Image too large (>5MB).
|
47 |
st.stop()
|
48 |
|
49 |
-
|
50 |
-
with st.spinner("π Extracting weight..."):
|
51 |
weight, confidence = extract_weight_from_image(image)
|
52 |
|
53 |
-
st.write(f"π οΈ DEBUG:
|
54 |
|
55 |
if not weight or confidence < 80:
|
56 |
-
st.error(f"β οΈ OCR confidence
|
57 |
else:
|
58 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
59 |
|
60 |
-
# Salesforce
|
61 |
device_id = "BAL-001"
|
62 |
-
image_url = "" #
|
63 |
|
64 |
salesforce_url = (
|
65 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
@@ -68,8 +68,8 @@ if image_bytes:
|
|
68 |
)
|
69 |
|
70 |
st.markdown("### π€ Send to Salesforce")
|
71 |
-
st.markdown(f"[β
|
72 |
|
73 |
except Exception as e:
|
74 |
-
st.error("β
|
75 |
st.exception(e)
|
|
|
3 |
import io
|
4 |
import uuid
|
5 |
import urllib.parse
|
6 |
+
from ocr_engine import extract_weight_from_image # your OCR function
|
7 |
|
8 |
+
# Setup
|
9 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
10 |
st.title("βοΈ Auto Weight Logger")
|
11 |
|
12 |
+
# Camera refresh key
|
13 |
if "camera_key" not in st.session_state:
|
14 |
st.session_state.camera_key = str(uuid.uuid4())
|
15 |
|
16 |
+
# Input selector
|
17 |
+
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
18 |
|
19 |
+
# Retake button
|
20 |
if st.button("π Clear / Retake"):
|
21 |
st.session_state.camera_key = str(uuid.uuid4())
|
22 |
st.experimental_rerun()
|
23 |
|
24 |
+
# Variables
|
25 |
image_bytes = None
|
26 |
image = None
|
27 |
|
28 |
+
# Camera mode
|
29 |
+
if input_mode == "Camera":
|
30 |
+
cam_photo = st.camera_input("π· Take a photo of the weight display", key=st.session_state.camera_key)
|
31 |
+
if cam_photo is not None:
|
32 |
+
image_bytes = cam_photo.getvalue()
|
33 |
|
34 |
+
# Upload mode
|
35 |
+
elif input_mode == "Upload":
|
36 |
+
uploaded_file = st.file_uploader("π Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
37 |
+
if uploaded_file is not None:
|
38 |
+
image_bytes = uploaded_file.read()
|
39 |
|
40 |
+
# Show image and process
|
41 |
if image_bytes:
|
42 |
try:
|
43 |
image = Image.open(io.BytesIO(image_bytes))
|
44 |
+
st.image(image, caption="πΈ Preview", use_column_width=True)
|
45 |
|
46 |
if len(image_bytes) > 5 * 1024 * 1024:
|
47 |
+
st.error("β Image is too large (>5MB). Please use a smaller image.")
|
48 |
st.stop()
|
49 |
|
50 |
+
with st.spinner("π Extracting weight using OCR..."):
|
|
|
51 |
weight, confidence = extract_weight_from_image(image)
|
52 |
|
53 |
+
st.write(f"π οΈ DEBUG: Weight = {weight}, Confidence = {confidence}")
|
54 |
|
55 |
if not weight or confidence < 80:
|
56 |
+
st.error(f"β οΈ Low OCR confidence ({int(confidence)}%). Try again.")
|
57 |
else:
|
58 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
59 |
|
60 |
+
# Generate Salesforce URL
|
61 |
device_id = "BAL-001"
|
62 |
+
image_url = "" # optional if image storage is needed
|
63 |
|
64 |
salesforce_url = (
|
65 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
|
68 |
)
|
69 |
|
70 |
st.markdown("### π€ Send to Salesforce")
|
71 |
+
st.markdown(f"[β
Confirm and Log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
72 |
|
73 |
except Exception as e:
|
74 |
+
st.error("β Error processing the image.")
|
75 |
st.exception(e)
|