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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +28 -14
src/streamlit_app.py CHANGED
@@ -3,6 +3,7 @@ from PIL import Image
3
  from ocr_engine import extract_weight_from_image
4
  import urllib.parse
5
  import uuid
 
6
 
7
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
8
  st.title("βš–οΈ Auto Weight Logger")
@@ -15,35 +16,46 @@ if "input_mode" not in st.session_state:
15
  if "camera_key" not in st.session_state:
16
  st.session_state.camera_key = str(uuid.uuid4())
17
 
18
- # Choose input mode
19
  st.radio("πŸ“Έ Select Image Input Method:", ["Camera", "Upload"], key="input_mode", horizontal=True)
20
 
21
- # Reset image
22
  if st.button("πŸ” Clear / Retake Photo"):
23
  st.session_state.image_data = None
24
- st.session_state.camera_key = str(uuid.uuid4()) # Reload webcam
 
 
 
25
 
26
- # Input section
27
  if st.session_state.image_data is None:
28
  if st.session_state.input_mode == "Camera":
29
- img_data = st.camera_input("πŸ“· Capture the weight display", key=st.session_state.camera_key)
30
  else:
31
- img_data = st.file_uploader("πŸ“ Upload an image of the weight display", type=["jpg", "jpeg", "png"])
32
 
33
- if img_data:
34
- st.session_state.image_data = img_data
35
 
36
- # OCR and Display
37
  if st.session_state.image_data:
38
  st.success("βœ… Image received successfully!")
39
- image = Image.open(st.session_state.image_data)
 
 
 
 
 
 
 
 
40
  st.image(image, caption="πŸ“Έ Snapshot", use_column_width=True)
41
 
42
- # Check image size
43
- if len(st.session_state.image_data.getvalue()) > 5 * 1024 * 1024:
44
- st.error("❌ Image too large (>5MB). Please try a smaller file.")
45
  st.stop()
46
 
 
47
  with st.spinner("πŸ” Extracting weight..."):
48
  weight, confidence = extract_weight_from_image(image)
49
 
@@ -54,8 +66,9 @@ if st.session_state.image_data:
54
  else:
55
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
56
 
 
57
  device_id = "BAL-001"
58
- image_url = "" # Placeholder for now
59
 
60
  salesforce_url = (
61
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
@@ -66,6 +79,7 @@ if st.session_state.image_data:
66
  st.markdown("### πŸ“€ Send to Salesforce")
67
  st.markdown(f"[βœ… Click here to confirm and log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
68
 
 
69
  if st.button("πŸ” Retake / Upload Another"):
70
  st.session_state.image_data = None
71
  st.session_state.camera_key = str(uuid.uuid4())
 
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")
 
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
 
 
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/"
 
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())