Update src/streamlit_app.py
Browse files- src/streamlit_app.py +33 -23
src/streamlit_app.py
CHANGED
@@ -5,67 +5,77 @@ import uuid
|
|
5 |
import urllib.parse
|
6 |
import pytz
|
7 |
from datetime import datetime
|
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 |
|
14 |
-
#
|
15 |
if "camera_key" not in st.session_state:
|
16 |
st.session_state.camera_key = str(uuid.uuid4())
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
20 |
|
21 |
-
# Clear
|
22 |
if st.button("π Clear / Retake"):
|
23 |
st.session_state.camera_key = str(uuid.uuid4())
|
|
|
24 |
st.rerun()
|
25 |
|
26 |
-
#
|
27 |
image_bytes = None
|
28 |
image = None
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
if input_mode == "Camera":
|
31 |
-
cam_photo = st.camera_input("π·
|
32 |
if cam_photo:
|
33 |
image_bytes = cam_photo.getvalue()
|
|
|
34 |
|
|
|
35 |
elif input_mode == "Upload":
|
36 |
-
uploaded_file = st.file_uploader("π Upload
|
37 |
if uploaded_file:
|
38 |
image_bytes = uploaded_file.read()
|
|
|
39 |
|
40 |
-
# Process image
|
41 |
if image_bytes:
|
42 |
try:
|
43 |
image = Image.open(io.BytesIO(image_bytes))
|
44 |
|
45 |
-
# Timestamp
|
46 |
-
ist = pytz.timezone("Asia/Kolkata")
|
47 |
-
captured_time = datetime.now(ist).strftime("%Y-%m-%d %I:%M:%S %p")
|
48 |
-
|
49 |
st.subheader("πΌοΈ Snapshot Image")
|
50 |
st.image(image, use_column_width=True)
|
51 |
|
52 |
if len(image_bytes) > 5 * 1024 * 1024:
|
53 |
-
st.error("β Image too large (>5MB).
|
54 |
st.stop()
|
55 |
|
56 |
-
with st.spinner("π
|
57 |
weight, confidence = extract_weight_from_image(image)
|
58 |
|
59 |
-
#
|
60 |
-
st.markdown(f"###
|
61 |
-
st.markdown(f"### π Captured At (IST): `{captured_time}`")
|
62 |
|
|
|
63 |
if not weight or confidence < 80:
|
64 |
-
st.
|
|
|
65 |
else:
|
66 |
-
st.
|
|
|
67 |
|
68 |
-
# Salesforce
|
69 |
device_id = "BAL-001"
|
70 |
image_url = "" # optional
|
71 |
|
@@ -79,7 +89,7 @@ if image_bytes:
|
|
79 |
st.markdown(f"[π€ Click to Log Weight in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
80 |
|
81 |
except UnidentifiedImageError:
|
82 |
-
st.error("β Invalid image
|
83 |
except Exception as e:
|
84 |
-
st.error("β
|
85 |
st.exception(e)
|
|
|
5 |
import urllib.parse
|
6 |
import pytz
|
7 |
from datetime import datetime
|
8 |
+
from ocr_engine import extract_weight_from_image # This must return (weight, confidence)
|
9 |
|
10 |
+
# Page setup
|
11 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
12 |
st.title("βοΈ Auto Weight Logger")
|
13 |
|
14 |
+
# Store keys/state
|
15 |
if "camera_key" not in st.session_state:
|
16 |
st.session_state.camera_key = str(uuid.uuid4())
|
17 |
+
if "captured_time" not in st.session_state:
|
18 |
+
st.session_state.captured_time = ""
|
19 |
|
20 |
+
# Select input type
|
21 |
input_mode = st.radio("πΈ Select Input Method", ["Camera", "Upload"], horizontal=True)
|
22 |
|
23 |
+
# Clear/reset
|
24 |
if st.button("π Clear / Retake"):
|
25 |
st.session_state.camera_key = str(uuid.uuid4())
|
26 |
+
st.session_state.captured_time = ""
|
27 |
st.rerun()
|
28 |
|
29 |
+
# Init
|
30 |
image_bytes = None
|
31 |
image = None
|
32 |
|
33 |
+
# Capture time (IST)
|
34 |
+
def get_current_ist_time():
|
35 |
+
ist = pytz.timezone("Asia/Kolkata")
|
36 |
+
return datetime.now(ist).strftime("%Y-%m-%d %I:%M:%S %p")
|
37 |
+
|
38 |
+
# Handle camera
|
39 |
if input_mode == "Camera":
|
40 |
+
cam_photo = st.camera_input("π· Take a photo", key=st.session_state.camera_key)
|
41 |
if cam_photo:
|
42 |
image_bytes = cam_photo.getvalue()
|
43 |
+
st.session_state.captured_time = get_current_ist_time()
|
44 |
|
45 |
+
# Handle upload
|
46 |
elif input_mode == "Upload":
|
47 |
+
uploaded_file = st.file_uploader("π Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
48 |
if uploaded_file:
|
49 |
image_bytes = uploaded_file.read()
|
50 |
+
st.session_state.captured_time = get_current_ist_time()
|
51 |
|
52 |
+
# Process if image exists
|
53 |
if image_bytes:
|
54 |
try:
|
55 |
image = Image.open(io.BytesIO(image_bytes))
|
56 |
|
|
|
|
|
|
|
|
|
57 |
st.subheader("πΌοΈ Snapshot Image")
|
58 |
st.image(image, use_column_width=True)
|
59 |
|
60 |
if len(image_bytes) > 5 * 1024 * 1024:
|
61 |
+
st.error("β Image is too large (>5MB).")
|
62 |
st.stop()
|
63 |
|
64 |
+
with st.spinner("π Running OCR..."):
|
65 |
weight, confidence = extract_weight_from_image(image)
|
66 |
|
67 |
+
# Show captured time
|
68 |
+
st.markdown(f"### π Captured At (IST): `{st.session_state.captured_time}`")
|
|
|
69 |
|
70 |
+
# Show weight
|
71 |
if not weight or confidence < 80:
|
72 |
+
st.markdown(f"### π¦ Captured Weight: `Not Detected`")
|
73 |
+
st.warning(f"β οΈ OCR Confidence too low ({int(confidence)}%)")
|
74 |
else:
|
75 |
+
st.markdown(f"### π¦ Captured Weight: `{weight} g`")
|
76 |
+
st.success(f"β
OCR Confidence: {int(confidence)}%")
|
77 |
|
78 |
+
# Generate Salesforce URL
|
79 |
device_id = "BAL-001"
|
80 |
image_url = "" # optional
|
81 |
|
|
|
89 |
st.markdown(f"[π€ Click to Log Weight in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
90 |
|
91 |
except UnidentifiedImageError:
|
92 |
+
st.error("β Invalid image file.")
|
93 |
except Exception as e:
|
94 |
+
st.error("β Unexpected error.")
|
95 |
st.exception(e)
|