Update src/streamlit_app.py
Browse files- src/streamlit_app.py +66 -11
src/streamlit_app.py
CHANGED
@@ -1,20 +1,75 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import io
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
st.
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
|
|
10 |
|
11 |
-
#
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
-
image = Image.open(io.BytesIO(
|
15 |
-
st.image(image, caption="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
except Exception as e:
|
17 |
-
st.error("β
|
18 |
st.exception(e)
|
19 |
-
else:
|
20 |
-
st.info("π Please upload an image to display it here.")
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
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/"
|
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"[β
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)
|
|
|
|