Sanjayraju30 commited on
Commit
51be189
Β·
verified Β·
1 Parent(s): 17e765d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +43 -49
src/streamlit_app.py CHANGED
@@ -8,78 +8,72 @@ import io
8
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
9
  st.title("βš–οΈ Auto Weight Logger")
10
 
11
- # Session state
12
- if "image_data" not in st.session_state:
13
- st.session_state.image_data = None
14
  if "input_mode" not in st.session_state:
15
  st.session_state.input_mode = "Camera"
16
  if "camera_key" not in st.session_state:
17
  st.session_state.camera_key = str(uuid.uuid4())
18
 
19
- # Choose input method
20
  st.radio("πŸ“Έ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
21
 
22
- # Clear/reset image
23
  if st.button("πŸ” Clear / Retake Photo"):
24
- st.session_state.image_data = None
25
  st.session_state.camera_key = str(uuid.uuid4())
26
 
27
  # Get image input
28
- uploaded_image = None
 
29
 
30
- if st.session_state.image_data is None:
31
  if st.session_state.input_mode == "Camera":
32
  uploaded_image = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
33
- else:
34
- uploaded_image = st.file_uploader("πŸ“ Upload an image of the weight display", type=["jpg", "jpeg", "png"])
35
 
36
- if uploaded_image:
37
- st.session_state.image_data = uploaded_image
 
 
38
 
39
- # Process image
40
- if st.session_state.image_data:
41
- st.success("βœ… Image received successfully!")
42
-
43
- # Convert to PIL image
44
  try:
45
- image_bytes = st.session_state.image_data.read() if hasattr(st.session_state.image_data, 'read') else st.session_state.image_data.getvalue()
46
- image = Image.open(io.BytesIO(image_bytes))
47
- except Exception as e:
48
- st.error("❌ Failed to load image.")
49
- st.stop()
50
 
51
- st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
 
 
52
 
53
- # Size check
54
- if len(image_bytes) > 5 * 1024 * 1024:
55
- st.error("❌ Image too large (>5MB). Please upload a smaller image.")
56
- st.stop()
57
 
58
- # OCR Extraction
59
- with st.spinner("πŸ” Extracting weight..."):
60
- weight, confidence = extract_weight_from_image(image)
61
 
62
- st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
 
 
 
63
 
64
- if not weight or confidence < 80:
65
- st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please try again.")
66
- else:
67
- st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
68
 
69
- # Generate Salesforce URL
70
- device_id = "BAL-001"
71
- image_url = "" # You can upload to S3 or Salesforce Files if needed
 
 
72
 
73
- salesforce_url = (
74
- "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
75
- f"weight_logger_page?WeightInput={urllib.parse.quote(str(weight))}"
76
- f"&DeviceID={urllib.parse.quote(device_id)}&ImageURL={urllib.parse.quote(image_url)}"
77
- )
78
 
79
- st.markdown("### πŸ“€ Send to Salesforce")
80
- st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
 
81
 
82
- # Retake or upload another
83
- if st.button("πŸ” Retake / Upload Another"):
84
- st.session_state.image_data = None
85
- st.session_state.camera_key = str(uuid.uuid4())
 
8
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
9
  st.title("βš–οΈ Auto Weight Logger")
10
 
11
+ # State initialization
12
+ if "image_bytes" not in st.session_state:
13
+ st.session_state.image_bytes = None
14
  if "input_mode" not in st.session_state:
15
  st.session_state.input_mode = "Camera"
16
  if "camera_key" not in st.session_state:
17
  st.session_state.camera_key = str(uuid.uuid4())
18
 
19
+ # Input mode selector
20
  st.radio("πŸ“Έ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
21
 
22
+ # Clear/reset
23
  if st.button("πŸ” Clear / Retake Photo"):
24
+ st.session_state.image_bytes = None
25
  st.session_state.camera_key = str(uuid.uuid4())
26
 
27
  # Get image input
28
+ if st.session_state.image_bytes is None:
29
+ uploaded_image = None
30
 
 
31
  if st.session_state.input_mode == "Camera":
32
  uploaded_image = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
33
+ if uploaded_image is not None:
34
+ st.session_state.image_bytes = uploaded_image.getvalue()
35
 
36
+ elif st.session_state.input_mode == "Upload":
37
+ uploaded_image = st.file_uploader("πŸ“ Upload image of weight display", type=["jpg", "jpeg", "png"])
38
+ if uploaded_image is not None:
39
+ st.session_state.image_bytes = uploaded_image.read()
40
 
41
+ # Process and OCR
42
+ if st.session_state.image_bytes:
 
 
 
43
  try:
44
+ image = Image.open(io.BytesIO(st.session_state.image_bytes))
45
+ st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
 
 
 
46
 
47
+ if len(st.session_state.image_bytes) > 5 * 1024 * 1024:
48
+ st.error("❌ Image too large (>5MB). Please try again.")
49
+ st.stop()
50
 
51
+ with st.spinner("πŸ” Extracting weight..."):
52
+ weight, confidence = extract_weight_from_image(image)
 
 
53
 
54
+ st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
 
 
55
 
56
+ if not weight or confidence < 80:
57
+ st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please try again.")
58
+ else:
59
+ st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
60
 
61
+ device_id = "BAL-001"
62
+ image_url = ""
 
 
63
 
64
+ salesforce_url = (
65
+ "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
66
+ f"weight_logger_page?WeightInput={urllib.parse.quote(str(weight))}"
67
+ f"&DeviceID={urllib.parse.quote(device_id)}&ImageURL={urllib.parse.quote(image_url)}"
68
+ )
69
 
70
+ st.markdown("### πŸ“€ Send to Salesforce")
71
+ st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
 
 
 
72
 
73
+ if st.button("πŸ” Retake / Upload Another"):
74
+ st.session_state.image_bytes = None
75
+ st.session_state.camera_key = str(uuid.uuid4())
76
 
77
+ except Exception as e:
78
+ st.error("❌ Failed to load or process image.")
79
+ st.exception(e)