Update src/streamlit_app.py
Browse files- src/streamlit_app.py +18 -19
src/streamlit_app.py
CHANGED
@@ -5,9 +5,9 @@ import uuid
|
|
5 |
import urllib.parse
|
6 |
from datetime import datetime
|
7 |
import pytz
|
8 |
-
from ocr_engine import extract_weight_from_image #
|
9 |
|
10 |
-
#
|
11 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
12 |
st.title("βοΈ Auto Weight Logger")
|
13 |
|
@@ -19,12 +19,12 @@ if "captured_time" not in st.session_state:
|
|
19 |
if "image_bytes" not in st.session_state:
|
20 |
st.session_state.image_bytes = None
|
21 |
|
22 |
-
#
|
23 |
def get_current_ist_time():
|
24 |
ist = pytz.timezone("Asia/Kolkata")
|
25 |
return datetime.now(ist).strftime("%Y-%m-%d %I:%M:%S %p")
|
26 |
|
27 |
-
# Input
|
28 |
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
29 |
|
30 |
# Retake button
|
@@ -47,19 +47,20 @@ elif input_mode == "Upload":
|
|
47 |
st.session_state.image_bytes = uploaded_file.read()
|
48 |
st.session_state.captured_time = get_current_ist_time()
|
49 |
|
50 |
-
# Process
|
51 |
if st.session_state.image_bytes:
|
52 |
try:
|
53 |
image = Image.open(io.BytesIO(st.session_state.image_bytes))
|
54 |
|
55 |
-
#
|
56 |
-
st.markdown(
|
57 |
|
58 |
-
|
59 |
-
|
|
|
|
|
60 |
st.image(image, use_column_width=True)
|
61 |
|
62 |
-
# 3. OCR processing
|
63 |
if len(st.session_state.image_bytes) > 5 * 1024 * 1024:
|
64 |
st.error("β Image too large (>5MB). Please upload a smaller image.")
|
65 |
st.stop()
|
@@ -67,18 +68,16 @@ if st.session_state.image_bytes:
|
|
67 |
with st.spinner("π Extracting weight using OCR..."):
|
68 |
weight, confidence = extract_weight_from_image(image)
|
69 |
|
70 |
-
# 4. Display OCR result
|
71 |
-
st.markdown("### βοΈ Captured Weight & OCR Confidence")
|
72 |
if not weight or confidence < 80:
|
73 |
-
st.
|
74 |
-
st.markdown("**Detected Weight:** Not reliable")
|
75 |
else:
|
76 |
-
st.
|
77 |
-
st.markdown(f"**Confidence:** `{int(confidence)}%`")
|
78 |
|
79 |
-
#
|
80 |
device_id = "BAL-001"
|
81 |
-
image_url = "" #
|
82 |
|
83 |
salesforce_url = (
|
84 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
@@ -109,5 +108,5 @@ if st.session_state.image_bytes:
|
|
109 |
except UnidentifiedImageError:
|
110 |
st.error("β Unable to process image. Please upload a valid JPG or PNG.")
|
111 |
except Exception as e:
|
112 |
-
st.error("β Unexpected error.")
|
113 |
st.exception(e)
|
|
|
5 |
import urllib.parse
|
6 |
from datetime import datetime
|
7 |
import pytz
|
8 |
+
from ocr_engine import extract_weight_from_image # This must return (weight, confidence)
|
9 |
|
10 |
+
# Setup
|
11 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
12 |
st.title("βοΈ Auto Weight Logger")
|
13 |
|
|
|
19 |
if "image_bytes" not in st.session_state:
|
20 |
st.session_state.image_bytes = None
|
21 |
|
22 |
+
# Function to get IST time
|
23 |
def get_current_ist_time():
|
24 |
ist = pytz.timezone("Asia/Kolkata")
|
25 |
return datetime.now(ist).strftime("%Y-%m-%d %I:%M:%S %p")
|
26 |
|
27 |
+
# Input selection
|
28 |
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
29 |
|
30 |
# Retake button
|
|
|
47 |
st.session_state.image_bytes = uploaded_file.read()
|
48 |
st.session_state.captured_time = get_current_ist_time()
|
49 |
|
50 |
+
# Process image
|
51 |
if st.session_state.image_bytes:
|
52 |
try:
|
53 |
image = Image.open(io.BytesIO(st.session_state.image_bytes))
|
54 |
|
55 |
+
# Display bullet-point summary
|
56 |
+
st.markdown("### π Captured Information")
|
57 |
|
58 |
+
st.markdown(f"""
|
59 |
+
- π **Captured At (IST):** `{st.session_state.captured_time}`
|
60 |
+
- πΌοΈ **Snapshot Image:**
|
61 |
+
""")
|
62 |
st.image(image, use_column_width=True)
|
63 |
|
|
|
64 |
if len(st.session_state.image_bytes) > 5 * 1024 * 1024:
|
65 |
st.error("β Image too large (>5MB). Please upload a smaller image.")
|
66 |
st.stop()
|
|
|
68 |
with st.spinner("π Extracting weight using OCR..."):
|
69 |
weight, confidence = extract_weight_from_image(image)
|
70 |
|
|
|
|
|
71 |
if not weight or confidence < 80:
|
72 |
+
st.markdown(f"- β οΈ **Low OCR Confidence:** `{int(confidence)}%` β Please retake or upload a clearer image.")
|
73 |
+
st.markdown("- βοΈ **Detected Weight:** Not reliable")
|
74 |
else:
|
75 |
+
st.markdown(f"- βοΈ **Detected Weight:** `{weight} g`")
|
76 |
+
st.markdown(f"- π **OCR Confidence:** `{int(confidence)}%`")
|
77 |
|
78 |
+
# Salesforce link
|
79 |
device_id = "BAL-001"
|
80 |
+
image_url = "" # Optional
|
81 |
|
82 |
salesforce_url = (
|
83 |
"https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
|
|
|
108 |
except UnidentifiedImageError:
|
109 |
st.error("β Unable to process image. Please upload a valid JPG or PNG.")
|
110 |
except Exception as e:
|
111 |
+
st.error("β Unexpected error occurred.")
|
112 |
st.exception(e)
|