Update src/streamlit_app.py
Browse files- src/streamlit_app.py +11 -66
src/streamlit_app.py
CHANGED
@@ -1,75 +1,20 @@
|
|
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 |
-
|
9 |
-
st.
|
10 |
-
st.title("βοΈ Auto Weight Logger")
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
st.session_state.camera_key = str(uuid.uuid4())
|
15 |
|
16 |
-
#
|
17 |
-
|
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(
|
44 |
-
st.image(image, caption="
|
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("β
|
75 |
st.exception(e)
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
import io
|
|
|
|
|
|
|
4 |
|
5 |
+
st.set_page_config(page_title="π Image Preview", layout="centered")
|
6 |
+
st.title("πΌοΈ Image Uploader & Viewer")
|
|
|
7 |
|
8 |
+
# Upload image
|
9 |
+
uploaded_file = st.file_uploader("π Upload a JPG, JPEG, or PNG image", type=["jpg", "jpeg", "png"])
|
|
|
10 |
|
11 |
+
# Show image
|
12 |
+
if uploaded_file is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
try:
|
14 |
+
image = Image.open(io.BytesIO(uploaded_file.read()))
|
15 |
+
st.image(image, caption="β
Uploaded Image", use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
except Exception as e:
|
17 |
+
st.error("β Could not open image.")
|
18 |
st.exception(e)
|
19 |
+
else:
|
20 |
+
st.info("π Please upload an image to display it here.")
|