Update src/streamlit_app.py
Browse files- src/streamlit_app.py +22 -20
src/streamlit_app.py
CHANGED
@@ -1,50 +1,52 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
-
from ocr_engine import extract_weight_from_image
|
4 |
-
import urllib.parse
|
5 |
-
import uuid
|
6 |
import io
|
|
|
|
|
|
|
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 |
-
# Input
|
16 |
-
input_method = st.radio("πΈ
|
17 |
|
18 |
-
# Clear
|
19 |
if st.button("π Clear / Retake"):
|
20 |
st.session_state.camera_key = str(uuid.uuid4())
|
21 |
st.experimental_rerun()
|
22 |
|
|
|
23 |
image_bytes = None
|
24 |
image = None
|
25 |
|
26 |
-
#
|
27 |
if input_method == "Camera":
|
28 |
-
|
29 |
-
if
|
30 |
-
image_bytes =
|
31 |
|
32 |
-
#
|
33 |
elif input_method == "Upload":
|
34 |
-
|
35 |
-
if
|
36 |
-
image_bytes =
|
37 |
|
38 |
-
#
|
39 |
if image_bytes:
|
40 |
try:
|
41 |
image = Image.open(io.BytesIO(image_bytes))
|
42 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
43 |
|
44 |
if len(image_bytes) > 5 * 1024 * 1024:
|
45 |
-
st.error("β Image too large (>5MB).
|
46 |
st.stop()
|
47 |
|
|
|
48 |
with st.spinner("π Extracting weight..."):
|
49 |
weight, confidence = extract_weight_from_image(image)
|
50 |
|
@@ -55,9 +57,9 @@ if image_bytes:
|
|
55 |
else:
|
56 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
57 |
|
58 |
-
# Salesforce
|
59 |
device_id = "BAL-001"
|
60 |
-
image_url = "" # Optional
|
61 |
|
62 |
salesforce_url = (
|
63 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
@@ -69,5 +71,5 @@ if image_bytes:
|
|
69 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
70 |
|
71 |
except Exception as e:
|
72 |
-
st.error("β Failed to
|
73 |
st.exception(e)
|
|
|
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 logic
|
7 |
|
8 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
9 |
st.title("βοΈ Auto Weight Logger")
|
10 |
|
11 |
+
# Unique key for camera input refresh
|
12 |
if "camera_key" not in st.session_state:
|
13 |
st.session_state.camera_key = str(uuid.uuid4())
|
14 |
|
15 |
+
# UI - Input type selector
|
16 |
+
input_method = st.radio("πΈ Choose Input Method", ["Camera", "Upload"], horizontal=True)
|
17 |
|
18 |
+
# UI - Clear/reset
|
19 |
if st.button("π Clear / Retake"):
|
20 |
st.session_state.camera_key = str(uuid.uuid4())
|
21 |
st.experimental_rerun()
|
22 |
|
23 |
+
# Initialize variables
|
24 |
image_bytes = None
|
25 |
image = None
|
26 |
|
27 |
+
# ----------------- CAMERA -----------------
|
28 |
if input_method == "Camera":
|
29 |
+
cam_image = st.camera_input("π· Capture the weight display", key=st.session_state.camera_key)
|
30 |
+
if cam_image is not None:
|
31 |
+
image_bytes = cam_image.getvalue()
|
32 |
|
33 |
+
# ----------------- UPLOAD -----------------
|
34 |
elif input_method == "Upload":
|
35 |
+
uploaded_image = st.file_uploader("π Upload image (JPG, PNG)", type=["jpg", "jpeg", "png"])
|
36 |
+
if uploaded_image is not None:
|
37 |
+
image_bytes = uploaded_image.read()
|
38 |
|
39 |
+
# ----------------- PROCESS IMAGE -----------------
|
40 |
if image_bytes:
|
41 |
try:
|
42 |
image = Image.open(io.BytesIO(image_bytes))
|
43 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
44 |
|
45 |
if len(image_bytes) > 5 * 1024 * 1024:
|
46 |
+
st.error("β Image too large (>5MB). Try a smaller file.")
|
47 |
st.stop()
|
48 |
|
49 |
+
# OCR
|
50 |
with st.spinner("π Extracting weight..."):
|
51 |
weight, confidence = extract_weight_from_image(image)
|
52 |
|
|
|
57 |
else:
|
58 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
59 |
|
60 |
+
# Salesforce link
|
61 |
device_id = "BAL-001"
|
62 |
+
image_url = "" # Optional if you host it
|
63 |
|
64 |
salesforce_url = (
|
65 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
|
71 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
72 |
|
73 |
except Exception as e:
|
74 |
+
st.error("β Failed to read/process the image.")
|
75 |
st.exception(e)
|