Sanjayraju30 commited on
Commit
21a3f78
Β·
verified Β·
1 Parent(s): f970466

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +22 -20
src/streamlit_app.py CHANGED
@@ -1,50 +1,52 @@
1
  import streamlit as st
2
  from PIL import Image
3
- from ocr_engine import extract_weight_from_image
4
- import urllib.parse
5
- import uuid
6
  import io
 
 
 
7
 
8
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
9
  st.title("βš–οΈ Auto Weight Logger")
10
 
11
- # Session state for camera reload
12
  if "camera_key" not in st.session_state:
13
  st.session_state.camera_key = str(uuid.uuid4())
14
 
15
- # Input method selector
16
- input_method = st.radio("πŸ“Έ Select Input Method", ["Camera", "Upload"], horizontal=True)
17
 
18
- # Clear button
19
  if st.button("πŸ” Clear / Retake"):
20
  st.session_state.camera_key = str(uuid.uuid4())
21
  st.experimental_rerun()
22
 
 
23
  image_bytes = None
24
  image = None
25
 
26
- # ----- CAMERA INPUT -----
27
  if input_method == "Camera":
28
- camera_image = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
29
- if camera_image is not None:
30
- image_bytes = camera_image.getvalue()
31
 
32
- # ----- FILE UPLOAD INPUT -----
33
  elif input_method == "Upload":
34
- uploaded_file = st.file_uploader("πŸ“ Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
35
- if uploaded_file is not None:
36
- image_bytes = uploaded_file.read()
37
 
38
- # ----- IMAGE PROCESSING -----
39
  if image_bytes:
40
  try:
41
  image = Image.open(io.BytesIO(image_bytes))
42
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
43
 
44
  if len(image_bytes) > 5 * 1024 * 1024:
45
- st.error("❌ Image too large (>5MB). Please use a smaller one.")
46
  st.stop()
47
 
 
48
  with st.spinner("πŸ” Extracting weight..."):
49
  weight, confidence = extract_weight_from_image(image)
50
 
@@ -55,9 +57,9 @@ if image_bytes:
55
  else:
56
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
57
 
58
- # Salesforce redirect link
59
  device_id = "BAL-001"
60
- image_url = "" # Optional
61
 
62
  salesforce_url = (
63
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
@@ -69,5 +71,5 @@ if image_bytes:
69
  st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
70
 
71
  except Exception as e:
72
- st.error("❌ Failed to load or process the image.")
73
  st.exception(e)
 
1
  import streamlit as st
2
  from PIL import Image
 
 
 
3
  import io
4
+ import uuid
5
+ import urllib.parse
6
+ from ocr_engine import extract_weight_from_image # Your OCR logic
7
 
8
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
9
  st.title("βš–οΈ Auto Weight Logger")
10
 
11
+ # Unique key for camera input refresh
12
  if "camera_key" not in st.session_state:
13
  st.session_state.camera_key = str(uuid.uuid4())
14
 
15
+ # UI - Input type selector
16
+ input_method = st.radio("πŸ“Έ Choose Input Method", ["Camera", "Upload"], horizontal=True)
17
 
18
+ # UI - Clear/reset
19
  if st.button("πŸ” Clear / Retake"):
20
  st.session_state.camera_key = str(uuid.uuid4())
21
  st.experimental_rerun()
22
 
23
+ # Initialize variables
24
  image_bytes = None
25
  image = None
26
 
27
+ # ----------------- CAMERA -----------------
28
  if input_method == "Camera":
29
+ cam_image = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
30
+ if cam_image is not None:
31
+ image_bytes = cam_image.getvalue()
32
 
33
+ # ----------------- UPLOAD -----------------
34
  elif input_method == "Upload":
35
+ uploaded_image = st.file_uploader("πŸ“ Upload image (JPG, PNG)", type=["jpg", "jpeg", "png"])
36
+ if uploaded_image is not None:
37
+ image_bytes = uploaded_image.read()
38
 
39
+ # ----------------- PROCESS IMAGE -----------------
40
  if image_bytes:
41
  try:
42
  image = Image.open(io.BytesIO(image_bytes))
43
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
44
 
45
  if len(image_bytes) > 5 * 1024 * 1024:
46
+ st.error("❌ Image too large (>5MB). Try a smaller file.")
47
  st.stop()
48
 
49
+ # OCR
50
  with st.spinner("πŸ” Extracting weight..."):
51
  weight, confidence = extract_weight_from_image(image)
52
 
 
57
  else:
58
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
59
 
60
+ # Salesforce link
61
  device_id = "BAL-001"
62
+ image_url = "" # Optional if you host it
63
 
64
  salesforce_url = (
65
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
 
71
  st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
72
 
73
  except Exception as e:
74
+ st.error("❌ Failed to read/process the image.")
75
  st.exception(e)