Sanjayraju30 commited on
Commit
a26d1e2
Β·
verified Β·
1 Parent(s): e789af9

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +16 -17
src/streamlit_app.py CHANGED
@@ -7,30 +7,28 @@ st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
  # ------------------------------
10
- # βœ… Session state management
11
  # ------------------------------
12
- if "show_camera" not in st.session_state:
13
- st.session_state.show_camera = True
14
 
15
- # πŸ”˜ Clear or Retake Button pressed
16
- if st.button("πŸ” Clear / Retake Photo"):
17
- st.session_state.show_camera = False # hide camera temporarily
18
  st.rerun()
19
 
20
- # πŸ”„ Reset camera on next run
21
- if not st.session_state.show_camera:
22
- st.session_state.show_camera = True
23
  st.rerun()
24
 
25
  # ------------------------------
26
  # πŸ“· Show camera input
27
  # ------------------------------
28
- img_data = None
29
- if st.session_state.show_camera:
30
- img_data = st.camera_input("πŸ“· Capture the weight display")
31
 
32
  # ------------------------------
33
- # βœ… Process if image is captured
34
  # ------------------------------
35
  if img_data:
36
  st.success("βœ… Image captured successfully!")
@@ -42,25 +40,26 @@ if img_data:
42
  image = Image.open(img_data)
43
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
44
 
 
45
  with st.spinner("πŸ” Extracting weight..."):
46
  weight, confidence = extract_weight_from_image(image)
47
 
48
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
49
 
50
- # ❌ Handle low confidence
51
  if not weight or confidence < 80:
52
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
53
  if st.button("πŸ” Retake Photo"):
54
- st.session_state.show_camera = False
55
  st.rerun()
56
  st.stop()
57
 
58
  # βœ… Success
59
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
60
 
61
- # Salesforce URL setup
62
  device_id = "BAL-001"
63
- image_url = "" # Optional: add image hosting logic later
64
 
65
  encoded_weight = urllib.parse.quote(str(weight))
66
  encoded_device = urllib.parse.quote(device_id)
 
7
  st.title("βš–οΈ Auto Weight Logger")
8
 
9
  # ------------------------------
10
+ # βœ… Setup session state for reset
11
  # ------------------------------
12
+ if "reset" not in st.session_state:
13
+ st.session_state.reset = False
14
 
15
+ # πŸ” Handle reset if needed (immediately rerun and then show camera again)
16
+ if st.session_state.reset:
17
+ st.session_state.reset = False # Clear flag
18
  st.rerun()
19
 
20
+ # πŸ”˜ Clear / Retake button
21
+ if st.button("πŸ” Clear / Retake Photo"):
22
+ st.session_state.reset = True
23
  st.rerun()
24
 
25
  # ------------------------------
26
  # πŸ“· Show camera input
27
  # ------------------------------
28
+ img_data = st.camera_input("πŸ“· Capture the weight display")
 
 
29
 
30
  # ------------------------------
31
+ # βœ… Handle image if captured
32
  # ------------------------------
33
  if img_data:
34
  st.success("βœ… Image captured successfully!")
 
40
  image = Image.open(img_data)
41
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
42
 
43
+ # πŸ” Extract weight via OCR
44
  with st.spinner("πŸ” Extracting weight..."):
45
  weight, confidence = extract_weight_from_image(image)
46
 
47
  st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
48
 
49
+ # ❌ If low confidence, ask to retake
50
  if not weight or confidence < 80:
51
  st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please retake the photo.")
52
  if st.button("πŸ” Retake Photo"):
53
+ st.session_state.reset = True
54
  st.rerun()
55
  st.stop()
56
 
57
  # βœ… Success
58
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
59
 
60
+ # Salesforce parameters
61
  device_id = "BAL-001"
62
+ image_url = "" # Optional image hosting URL
63
 
64
  encoded_weight = urllib.parse.quote(str(weight))
65
  encoded_device = urllib.parse.quote(device_id)