Sanjayraju30 commited on
Commit
3c80166
Β·
verified Β·
1 Parent(s): 45d41b6

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +11 -29
src/streamlit_app.py CHANGED
@@ -7,81 +7,63 @@ st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
  # ------------------------------
10
- # βœ… Setup session state for clearing
11
  # ------------------------------
12
  if "reset" not in st.session_state:
13
  st.session_state.reset = False
14
 
15
- # ------------------------------
16
- # πŸ” If reset is active, clear and rerun once
17
- # ------------------------------
18
  if st.session_state.reset:
19
- st.session_state.reset = False # clear the flag
20
  st.rerun()
21
 
22
- # ------------------------------
23
- # πŸ”˜ Clear Photo Button
24
- # ------------------------------
25
  if st.button("πŸ” Clear Photo"):
26
  st.session_state.reset = True
27
  st.rerun()
28
 
29
- # ------------------------------
30
- # πŸ“· Capture Image from Webcam
31
- # ------------------------------
32
  img_data = st.camera_input("πŸ“· Capture the weight display")
33
 
 
34
  if img_data:
35
  st.success("βœ… Image captured successfully!")
36
 
37
- # βœ… Optional: check file size
38
  if len(img_data.getvalue()) > 5 * 1024 * 1024:
39
  st.error("❌ Image too large (>5MB). Please try again.")
40
  st.stop()
41
 
42
- # Open and display image
43
  image = Image.open(img_data)
44
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
45
 
46
- # --------------------------
47
- # 🧠 Extract weight via OCR
48
- # --------------------------
49
  with st.spinner("πŸ” Extracting weight..."):
50
  weight, confidence = extract_weight_from_image(image)
51
 
52
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
53
 
54
- # --------------------------
55
- # ❌ Retry if confidence low
56
- # --------------------------
57
  if not weight or confidence < 80:
58
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the image.")
59
- if st.button("πŸ” Retry"):
60
  st.session_state.reset = True
61
  st.rerun()
62
  st.stop()
63
 
64
- # --------------------------
65
- # βœ… Success β€” show result
66
- # --------------------------
67
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
68
 
69
- # Salesforce parameters
70
  device_id = "BAL-001"
71
- image_url = "" # Optional: set if you host the image
72
 
73
  encoded_weight = urllib.parse.quote(str(weight))
74
  encoded_device = urllib.parse.quote(device_id)
75
  encoded_image_url = urllib.parse.quote(image_url)
76
 
77
- # βœ… Salesforce VF Page URL
78
  salesforce_url = (
79
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
80
  f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
81
  )
82
 
83
- # --------------------------
84
- # πŸ”— Display Salesforce link
85
- # --------------------------
86
  st.markdown("### πŸ“€ Send to Salesforce")
87
  st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
 
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
  # ------------------------------
10
+ # βœ… Session state for clearing input
11
  # ------------------------------
12
  if "reset" not in st.session_state:
13
  st.session_state.reset = False
14
 
15
+ # πŸ” Auto rerun if reset is active
 
 
16
  if st.session_state.reset:
17
+ st.session_state.reset = False
18
  st.rerun()
19
 
20
+ # πŸ”˜ Manual clear/reset button
 
 
21
  if st.button("πŸ” Clear Photo"):
22
  st.session_state.reset = True
23
  st.rerun()
24
 
25
+ # πŸ“· Capture webcam image
 
 
26
  img_data = st.camera_input("πŸ“· Capture the weight display")
27
 
28
+ # πŸ” Process image only if captured
29
  if img_data:
30
  st.success("βœ… Image captured successfully!")
31
 
 
32
  if len(img_data.getvalue()) > 5 * 1024 * 1024:
33
  st.error("❌ Image too large (>5MB). Please try again.")
34
  st.stop()
35
 
 
36
  image = Image.open(img_data)
37
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
38
 
 
 
 
39
  with st.spinner("πŸ” Extracting weight..."):
40
  weight, confidence = extract_weight_from_image(image)
41
 
42
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
43
 
44
+ # ❌ If confidence is low or result missing β€” show Retake
 
 
45
  if not weight or confidence < 80:
46
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the image.")
47
+ if st.button("πŸ” Retake Photo"):
48
  st.session_state.reset = True
49
  st.rerun()
50
  st.stop()
51
 
52
+ # βœ… Valid result
 
 
53
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
54
 
55
+ # πŸ”— Salesforce URL
56
  device_id = "BAL-001"
57
+ image_url = "" # Optional
58
 
59
  encoded_weight = urllib.parse.quote(str(weight))
60
  encoded_device = urllib.parse.quote(device_id)
61
  encoded_image_url = urllib.parse.quote(image_url)
62
 
 
63
  salesforce_url = (
64
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
65
  f"weight_logger_page?WeightInput={encoded_weight}&DeviceID={encoded_device}&ImageURL={encoded_image_url}"
66
  )
67
 
 
 
 
68
  st.markdown("### πŸ“€ Send to Salesforce")
69
  st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)