Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +28 -14
src/streamlit_app.py
CHANGED
|
@@ -3,6 +3,7 @@ from PIL import Image
|
|
| 3 |
from ocr_engine import extract_weight_from_image
|
| 4 |
import urllib.parse
|
| 5 |
import uuid
|
|
|
|
| 6 |
|
| 7 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
| 8 |
st.title("βοΈ Auto Weight Logger")
|
|
@@ -15,35 +16,46 @@ if "input_mode" not in st.session_state:
|
|
| 15 |
if "camera_key" not in st.session_state:
|
| 16 |
st.session_state.camera_key = str(uuid.uuid4())
|
| 17 |
|
| 18 |
-
# Choose input
|
| 19 |
st.radio("πΈ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
if st.button("π Clear / Retake Photo"):
|
| 23 |
st.session_state.image_data = None
|
| 24 |
-
st.session_state.camera_key = str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# Input section
|
| 27 |
if st.session_state.image_data is None:
|
| 28 |
if st.session_state.input_mode == "Camera":
|
| 29 |
-
|
| 30 |
else:
|
| 31 |
-
|
| 32 |
|
| 33 |
-
if
|
| 34 |
-
st.session_state.image_data =
|
| 35 |
|
| 36 |
-
#
|
| 37 |
if st.session_state.image_data:
|
| 38 |
st.success("β
Image received successfully!")
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
if len(
|
| 44 |
-
st.error("β Image too large (>5MB). Please
|
| 45 |
st.stop()
|
| 46 |
|
|
|
|
| 47 |
with st.spinner("π Extracting weight..."):
|
| 48 |
weight, confidence = extract_weight_from_image(image)
|
| 49 |
|
|
@@ -54,8 +66,9 @@ if st.session_state.image_data:
|
|
| 54 |
else:
|
| 55 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
| 56 |
|
|
|
|
| 57 |
device_id = "BAL-001"
|
| 58 |
-
image_url = "" #
|
| 59 |
|
| 60 |
salesforce_url = (
|
| 61 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
@@ -66,6 +79,7 @@ if st.session_state.image_data:
|
|
| 66 |
st.markdown("### π€ Send to Salesforce")
|
| 67 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
| 68 |
|
|
|
|
| 69 |
if st.button("π Retake / Upload Another"):
|
| 70 |
st.session_state.image_data = None
|
| 71 |
st.session_state.camera_key = str(uuid.uuid4())
|
|
|
|
| 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")
|
|
|
|
| 16 |
if "camera_key" not in st.session_state:
|
| 17 |
st.session_state.camera_key = str(uuid.uuid4())
|
| 18 |
|
| 19 |
+
# Choose input method
|
| 20 |
st.radio("πΈ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
|
| 21 |
|
| 22 |
+
# Clear/reset image
|
| 23 |
if st.button("π Clear / Retake Photo"):
|
| 24 |
st.session_state.image_data = None
|
| 25 |
+
st.session_state.camera_key = str(uuid.uuid4())
|
| 26 |
+
|
| 27 |
+
# Get image input
|
| 28 |
+
uploaded_image = None
|
| 29 |
|
|
|
|
| 30 |
if st.session_state.image_data is None:
|
| 31 |
if st.session_state.input_mode == "Camera":
|
| 32 |
+
uploaded_image = st.camera_input("π· Capture the weight display", key=st.session_state.camera_key)
|
| 33 |
else:
|
| 34 |
+
uploaded_image = st.file_uploader("π Upload an image of the weight display", type=["jpg", "jpeg", "png"])
|
| 35 |
|
| 36 |
+
if uploaded_image:
|
| 37 |
+
st.session_state.image_data = uploaded_image
|
| 38 |
|
| 39 |
+
# Process image
|
| 40 |
if st.session_state.image_data:
|
| 41 |
st.success("β
Image received successfully!")
|
| 42 |
+
|
| 43 |
+
# Convert to PIL image
|
| 44 |
+
try:
|
| 45 |
+
image_bytes = st.session_state.image_data.read() if hasattr(st.session_state.image_data, 'read') else st.session_state.image_data.getvalue()
|
| 46 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error("β Failed to load image.")
|
| 49 |
+
st.stop()
|
| 50 |
+
|
| 51 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
| 52 |
|
| 53 |
+
# Size check
|
| 54 |
+
if len(image_bytes) > 5 * 1024 * 1024:
|
| 55 |
+
st.error("β Image too large (>5MB). Please upload a smaller image.")
|
| 56 |
st.stop()
|
| 57 |
|
| 58 |
+
# OCR Extraction
|
| 59 |
with st.spinner("π Extracting weight..."):
|
| 60 |
weight, confidence = extract_weight_from_image(image)
|
| 61 |
|
|
|
|
| 66 |
else:
|
| 67 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
| 68 |
|
| 69 |
+
# Generate Salesforce URL
|
| 70 |
device_id = "BAL-001"
|
| 71 |
+
image_url = "" # You can upload to S3 or Salesforce Files if needed
|
| 72 |
|
| 73 |
salesforce_url = (
|
| 74 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
|
|
| 79 |
st.markdown("### π€ Send to Salesforce")
|
| 80 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
| 81 |
|
| 82 |
+
# Retake or upload another
|
| 83 |
if st.button("π Retake / Upload Another"):
|
| 84 |
st.session_state.image_data = None
|
| 85 |
st.session_state.camera_key = str(uuid.uuid4())
|