Sanjayraju30 commited on
Commit
4f5f0dd
·
verified ·
1 Parent(s): 34234c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -33
app.py CHANGED
@@ -1,37 +1,58 @@
1
- import gradio as gr
 
 
 
 
2
  from ocr_engine import extract_weight_from_image
3
- from utils import get_ist_time
4
 
5
- def process_image(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  try:
7
- if image is None:
8
- return "No image provided", "-", None, "-"
9
-
 
 
 
 
 
 
 
 
 
10
  weight, confidence = extract_weight_from_image(image)
11
- timestamp = get_ist_time()
12
-
13
- if not weight:
14
- return "No weight detected", timestamp, image, "-"
15
-
16
- sf_url = f"https://your-salesforce-site.com/log-weight?weight={weight}&time={timestamp}"
17
- return f"{weight} g (Confidence: {confidence}%)", timestamp, image, sf_url
18
-
19
- except Exception as e:
20
- return f"Error: {str(e)}", "-", None, "-"
21
-
22
- iface = gr.Interface(
23
- fn=process_image,
24
- inputs=gr.Image(type="pil", label="Upload or Capture Snapshot"),
25
- outputs=[
26
- gr.Textbox(label="Detected Weight"),
27
- gr.Textbox(label="Captured At (IST)"),
28
- gr.Image(label="Snapshot Image"),
29
- gr.Textbox(label="Salesforce Log URL"),
30
- ],
31
- title="⚖️ Auto Weight Logger",
32
- description="Upload or capture a digital scale image to extract the weight.",
33
- live=True,
34
- )
35
-
36
- if __name__ == "__main__":
37
- iface.launch()
 
1
+ import streamlit as st
2
+ from PIL import Image, UnidentifiedImageError
3
+ import io
4
+ from datetime import datetime
5
+ import pytz
6
  from ocr_engine import extract_weight_from_image
 
7
 
8
+ st.set_page_config(page_title="⚖️ Auto Weight Logger", layout="centered")
9
+ st.title("⚖️ Auto Weight Logger")
10
+
11
+ if "camera_key" not in st.session_state:
12
+ st.session_state["camera_key"] = 0
13
+
14
+ # UI: Upload or Capture Image
15
+ col1, col2 = st.columns(2)
16
+ with col1:
17
+ uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
18
+ with col2:
19
+ if st.button("📸 Retake Photo"):
20
+ st.session_state["camera_key"] += 1
21
+ camera_image = st.camera_input("Capture Image", key=st.session_state["camera_key"])
22
+
23
+ # Load image
24
+ image = None
25
+ if uploaded_file:
26
  try:
27
+ image = Image.open(uploaded_file)
28
+ except UnidentifiedImageError:
29
+ st.error("Invalid image format.")
30
+ elif camera_image:
31
+ try:
32
+ image = Image.open(camera_image)
33
+ except UnidentifiedImageError:
34
+ st.error("Invalid camera image.")
35
+
36
+ # Process image
37
+ if image:
38
+ with st.spinner("Detecting weight..."):
39
  weight, confidence = extract_weight_from_image(image)
40
+
41
+ # Timestamp
42
+ ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
43
+
44
+ # Output
45
+ st.markdown("### 📅 Captured At (IST)")
46
+ st.info(ist_time)
47
+
48
+ st.markdown("### 🖼️ Snapshot")
49
+ st.image(image, width=400)
50
+
51
+ st.markdown("### ⚖️ Detected Weight")
52
+ if confidence > 0:
53
+ st.success(f"Detected Weight: **{weight}** \nConfidence: `{confidence:.2f}`")
54
+ else:
55
+ st.error("No weight detected. Try with a clearer image.")
56
+
57
+ # (Optional) Send to Salesforce button
58
+ st.markdown("🔗 [Send to Salesforce](#)", unsafe_allow_html=True)