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

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +27 -27
src/streamlit_app.py CHANGED
@@ -3,63 +3,63 @@ 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
 
53
- st.write(f"πŸ› οΈ DEBUG: weight = {weight}, confidence = {confidence}")
54
 
55
  if not weight or confidence < 80:
56
- st.error(f"⚠️ OCR confidence too low ({int(confidence)}%). Please try again.")
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/"
@@ -68,8 +68,8 @@ if image_bytes:
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
  except Exception as e:
74
- st.error("❌ Failed to read/process the image.")
75
  st.exception(e)
 
3
  import io
4
  import uuid
5
  import urllib.parse
6
+ from ocr_engine import extract_weight_from_image # your OCR function
7
 
8
+ # Setup
9
  st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
10
  st.title("βš–οΈ Auto Weight Logger")
11
 
12
+ # Camera refresh key
13
  if "camera_key" not in st.session_state:
14
  st.session_state.camera_key = str(uuid.uuid4())
15
 
16
+ # Input selector
17
+ input_mode = st.radio("πŸ“Έ Select Input Method", ["Camera", "Upload"], horizontal=True)
18
 
19
+ # Retake button
20
  if st.button("πŸ” Clear / Retake"):
21
  st.session_state.camera_key = str(uuid.uuid4())
22
  st.experimental_rerun()
23
 
24
+ # Variables
25
  image_bytes = None
26
  image = None
27
 
28
+ # Camera mode
29
+ if input_mode == "Camera":
30
+ cam_photo = st.camera_input("πŸ“· Take a photo of the weight display", key=st.session_state.camera_key)
31
+ if cam_photo is not None:
32
+ image_bytes = cam_photo.getvalue()
33
 
34
+ # Upload mode
35
+ elif input_mode == "Upload":
36
+ uploaded_file = st.file_uploader("πŸ“ Upload an image (JPG/PNG)", type=["jpg", "jpeg", "png"])
37
+ if uploaded_file is not None:
38
+ image_bytes = uploaded_file.read()
39
 
40
+ # Show image and process
41
  if image_bytes:
42
  try:
43
  image = Image.open(io.BytesIO(image_bytes))
44
+ st.image(image, caption="πŸ“Έ Preview", use_column_width=True)
45
 
46
  if len(image_bytes) > 5 * 1024 * 1024:
47
+ st.error("❌ Image is too large (>5MB). Please use a smaller image.")
48
  st.stop()
49
 
50
+ with st.spinner("πŸ” Extracting weight using OCR..."):
 
51
  weight, confidence = extract_weight_from_image(image)
52
 
53
+ st.write(f"πŸ› οΈ DEBUG: Weight = {weight}, Confidence = {confidence}")
54
 
55
  if not weight or confidence < 80:
56
+ st.error(f"⚠️ Low OCR confidence ({int(confidence)}%). Try again.")
57
  else:
58
  st.success(f"βœ… Detected Weight: {weight} g (Confidence: {int(confidence)}%)")
59
 
60
+ # Generate Salesforce URL
61
  device_id = "BAL-001"
62
+ image_url = "" # optional if image storage is needed
63
 
64
  salesforce_url = (
65
  "https://autoweightlogger-dev-ed.my.salesforce-sites.com/"
 
68
  )
69
 
70
  st.markdown("### πŸ“€ Send to Salesforce")
71
+ st.markdown(f"[βœ… Confirm and Log in Salesforce]({salesforce_url})", unsafe_allow_html=True)
72
 
73
  except Exception as e:
74
+ st.error("❌ Error processing the image.")
75
  st.exception(e)