Sanjayraju30 commited on
Commit
e7f3d63
Β·
verified Β·
1 Parent(s): b675cbf

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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 # Ensure this function returns (weight, confidence)
9
 
10
- # Set up page
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
- # 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 method
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 and Display (if image is available)
51
  if st.session_state.image_bytes:
52
  try:
53
  image = Image.open(io.BytesIO(st.session_state.image_bytes))
54
 
55
- # 1. Show captured time
56
- st.markdown(f"### πŸ•’ Captured At (IST): `{st.session_state.captured_time}`")
57
 
58
- # 2. Show image
59
- st.markdown("### πŸ–ΌοΈ Snapshot Image")
 
 
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.error(f"⚠️ Low OCR Confidence ({int(confidence)}%). Please retake or upload a clearer image.")
74
- st.markdown("**Detected Weight:** Not reliable")
75
  else:
76
- st.success(f"βœ… Detected Weight: `{weight} g`")
77
- st.markdown(f"**Confidence:** `{int(confidence)}%`")
78
 
79
- # 5. Send to Salesforce
80
  device_id = "BAL-001"
81
- image_url = "" # optional
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)