Sanjayraju30 commited on
Commit
7be0446
Β·
verified Β·
1 Parent(s): 5ba9ce7

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +66 -11
src/streamlit_app.py CHANGED
@@ -1,20 +1,75 @@
1
  import streamlit as st
2
  from PIL import Image
3
  import io
 
 
 
4
 
5
- st.set_page_config(page_title="πŸ“ Image Preview", layout="centered")
6
- st.title("πŸ–ΌοΈ Image Uploader & Viewer")
 
7
 
8
- # Upload image
9
- uploaded_file = st.file_uploader("πŸ“ Upload a JPG, JPEG, or PNG image", type=["jpg", "jpeg", "png"])
 
10
 
11
- # Show image
12
- if uploaded_file is not None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  try:
14
- image = Image.open(io.BytesIO(uploaded_file.read()))
15
- st.image(image, caption="βœ… Uploaded Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  except Exception as e:
17
- st.error("❌ Could not open image.")
18
  st.exception(e)
19
- else:
20
- st.info("πŸ“Œ Please upload an image to display it here.")
 
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 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/"
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"[βœ… 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)