Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +11 -2
src/streamlit_app.py
CHANGED
@@ -3,35 +3,43 @@ from PIL import Image
|
|
3 |
from ocr_engine import extract_weight_from_image
|
4 |
import urllib.parse
|
5 |
|
|
|
6 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
7 |
st.title("βοΈ Auto Weight Logger")
|
8 |
|
|
|
9 |
img_data = st.camera_input("π· Capture the weight display")
|
10 |
|
11 |
if img_data is not None:
|
|
|
12 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
13 |
st.error("β Image too large (>5MB). Please try again.")
|
14 |
st.stop()
|
15 |
|
|
|
16 |
image = Image.open(img_data)
|
17 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
18 |
|
|
|
19 |
with st.spinner("π Extracting weight..."):
|
20 |
weight, confidence = extract_weight_from_image(image)
|
21 |
|
22 |
-
#
|
23 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
24 |
|
|
|
25 |
if not weight or confidence < 80:
|
26 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the image.")
|
27 |
if st.button("π Try Again"):
|
28 |
st.experimental_rerun()
|
29 |
st.stop()
|
30 |
|
|
|
31 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
32 |
|
|
|
33 |
device_id = "BAL-001"
|
34 |
-
image_url = ""
|
35 |
|
36 |
encoded_weight = urllib.parse.quote(str(weight))
|
37 |
encoded_device = urllib.parse.quote(device_id)
|
@@ -42,5 +50,6 @@ if img_data is not None:
|
|
42 |
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
43 |
)
|
44 |
|
|
|
45 |
st.markdown("### π€ Send to Salesforce")
|
46 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|
|
|
3 |
from ocr_engine import extract_weight_from_image
|
4 |
import urllib.parse
|
5 |
|
6 |
+
# β
App configuration
|
7 |
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
8 |
st.title("βοΈ Auto Weight Logger")
|
9 |
|
10 |
+
# β
Capture image from camera
|
11 |
img_data = st.camera_input("π· Capture the weight display")
|
12 |
|
13 |
if img_data is not None:
|
14 |
+
# β
Check for file size
|
15 |
if len(img_data.getvalue()) > 5 * 1024 * 1024:
|
16 |
st.error("β Image too large (>5MB). Please try again.")
|
17 |
st.stop()
|
18 |
|
19 |
+
# β
Show captured image
|
20 |
image = Image.open(img_data)
|
21 |
st.image(image, caption="πΈ Snapshot", use_column_width=True)
|
22 |
|
23 |
+
# β
Run OCR
|
24 |
with st.spinner("π Extracting weight..."):
|
25 |
weight, confidence = extract_weight_from_image(image)
|
26 |
|
27 |
+
# β
Show debug info (can be removed later)
|
28 |
st.write(f"π οΈ DEBUG: weight = {weight}, confidence = {confidence}")
|
29 |
|
30 |
+
# β
Retry if OCR failed or confidence too low
|
31 |
if not weight or confidence < 80:
|
32 |
st.error(f"β οΈ OCR confidence too low ({int(confidence)}%). Please retake the image.")
|
33 |
if st.button("π Try Again"):
|
34 |
st.experimental_rerun()
|
35 |
st.stop()
|
36 |
|
37 |
+
# β
Show success
|
38 |
st.success(f"β
Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
|
39 |
|
40 |
+
# β
Prepare Salesforce URL
|
41 |
device_id = "BAL-001"
|
42 |
+
image_url = "" # Leave blank unless you host image
|
43 |
|
44 |
encoded_weight = urllib.parse.quote(str(weight))
|
45 |
encoded_device = urllib.parse.quote(device_id)
|
|
|
50 |
f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
|
51 |
)
|
52 |
|
53 |
+
# β
Send to Salesforce
|
54 |
st.markdown("### π€ Send to Salesforce")
|
55 |
st.markdown(f"[β
Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
|