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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +37 -21
src/streamlit_app.py CHANGED
@@ -11,64 +11,63 @@ from ocr_engine import extract_weight_from_image # Ensure this function returns
11
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
12
  st.title("βš–οΈ Auto Weight Logger")
13
 
14
- # Session state keys
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
  # Get IST time
21
  def get_current_ist_time():
22
  ist = pytz.timezone("Asia/Kolkata")
23
  return datetime.now(ist).strftime("%Y-%m-%d %I:%M:%S %p")
24
 
25
- # Input selection
26
  input_mode = st.radio("πŸ“Έ Select Input Method", ["Camera", "Upload"], horizontal=True)
27
 
28
  # Retake button
29
  if st.button("πŸ” Clear / Retake"):
30
  st.session_state.camera_key = str(uuid.uuid4())
31
  st.session_state.captured_time = ""
 
32
  st.rerun()
33
 
34
- # Image handling
35
- image_bytes = None
36
- image = None
37
-
38
- # Webcam input
39
  if input_mode == "Camera":
40
  cam_photo = st.camera_input("πŸ“· Capture Weight Display", key=st.session_state.camera_key)
41
- if cam_photo is not None:
42
- image_bytes = cam_photo.getvalue()
43
  st.session_state.captured_time = get_current_ist_time()
44
 
45
- # File 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 is not None:
49
- image_bytes = uploaded_file.read()
50
  st.session_state.captured_time = get_current_ist_time()
51
 
52
- # Process and display
53
- if image_bytes:
54
  try:
55
- image = Image.open(io.BytesIO(image_bytes))
56
 
57
- # 1. Show Captured At
58
  st.markdown(f"### πŸ•’ Captured At (IST): `{st.session_state.captured_time}`")
59
 
60
- # 2. Show Snapshot Image
61
  st.markdown("### πŸ–ΌοΈ Snapshot Image")
62
  st.image(image, use_column_width=True)
63
 
64
- # 3. Extract OCR and show result
65
- if len(image_bytes) > 5 * 1024 * 1024:
66
  st.error("❌ Image too large (>5MB). Please upload a smaller image.")
67
  st.stop()
68
 
69
  with st.spinner("πŸ” Extracting weight using OCR..."):
70
  weight, confidence = extract_weight_from_image(image)
71
 
 
72
  st.markdown("### βš–οΈ Captured Weight & OCR Confidence")
73
  if not weight or confidence < 80:
74
  st.error(f"⚠️ Low OCR Confidence ({int(confidence)}%). Please retake or upload a clearer image.")
@@ -77,7 +76,7 @@ if image_bytes:
77
  st.success(f"βœ… Detected Weight: `{weight} g`")
78
  st.markdown(f"**Confidence:** `{int(confidence)}%`")
79
 
80
- # 4. Send to Salesforce
81
  device_id = "BAL-001"
82
  image_url = "" # optional
83
 
@@ -88,7 +87,24 @@ if image_bytes:
88
  )
89
 
90
  st.markdown("### βœ… Send to Salesforce")
91
- st.markdown(f"[πŸ“€ Click to Log Weight in Salesforce]({salesforce_url})", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  except UnidentifiedImageError:
94
  st.error("❌ Unable to process image. Please upload a valid JPG or PNG.")
 
11
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
12
  st.title("βš–οΈ Auto Weight Logger")
13
 
14
+ # Session 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
+ 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
31
  if st.button("πŸ” Clear / Retake"):
32
  st.session_state.camera_key = str(uuid.uuid4())
33
  st.session_state.captured_time = ""
34
+ st.session_state.image_bytes = None
35
  st.rerun()
36
 
37
+ # Capture or Upload
 
 
 
 
38
  if input_mode == "Camera":
39
  cam_photo = st.camera_input("πŸ“· Capture Weight Display", key=st.session_state.camera_key)
40
+ if cam_photo:
41
+ st.session_state.image_bytes = cam_photo.getvalue()
42
  st.session_state.captured_time = get_current_ist_time()
43
 
 
44
  elif input_mode == "Upload":
45
  uploaded_file = st.file_uploader("πŸ“ Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
46
+ if uploaded_file:
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()
66
 
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.")
 
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
 
 
87
  )
88
 
89
  st.markdown("### βœ… Send to Salesforce")
90
+ st.markdown(
91
+ f"""
92
+ <a href="{salesforce_url}" target="_blank">
93
+ <button style="
94
+ background-color: #4CAF50;
95
+ border: none;
96
+ color: white;
97
+ padding: 12px 24px;
98
+ text-align: center;
99
+ text-decoration: none;
100
+ display: inline-block;
101
+ font-size: 16px;
102
+ border-radius: 8px;
103
+ cursor: pointer;
104
+ ">πŸ“€ Log to Salesforce</button>
105
+ </a>
106
+ """, unsafe_allow_html=True
107
+ )
108
 
109
  except UnidentifiedImageError:
110
  st.error("❌ Unable to process image. Please upload a valid JPG or PNG.")