File size: 1,762 Bytes
4f5f0dd
 
 
 
 
812afe6
 
4f5f0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a51774c
4f5f0dd
 
 
 
 
 
 
 
 
 
 
 
a51774c
4f5f0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
from PIL import Image, UnidentifiedImageError
import io
from datetime import datetime
import pytz
from ocr_engine import extract_weight_from_image

st.set_page_config(page_title="โš–๏ธ Auto Weight Logger", layout="centered")
st.title("โš–๏ธ Auto Weight Logger")

if "camera_key" not in st.session_state:
    st.session_state["camera_key"] = 0

# UI: Upload or Capture Image
col1, col2 = st.columns(2)
with col1:
    uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
with col2:
    if st.button("๐Ÿ“ธ Retake Photo"):
        st.session_state["camera_key"] += 1
    camera_image = st.camera_input("Capture Image", key=st.session_state["camera_key"])

# Load image
image = None
if uploaded_file:
    try:
        image = Image.open(uploaded_file)
    except UnidentifiedImageError:
        st.error("Invalid image format.")
elif camera_image:
    try:
        image = Image.open(camera_image)
    except UnidentifiedImageError:
        st.error("Invalid camera image.")

# Process image
if image:
    with st.spinner("Detecting weight..."):
        weight, confidence = extract_weight_from_image(image)

    # Timestamp
    ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")

    # Output
    st.markdown("### ๐Ÿ“… Captured At (IST)")
    st.info(ist_time)

    st.markdown("### ๐Ÿ–ผ๏ธ Snapshot")
    st.image(image, width=400)

    st.markdown("### โš–๏ธ Detected Weight")
    if confidence > 0:
        st.success(f"Detected Weight: **{weight}**  \nConfidence: `{confidence:.2f}`")
    else:
        st.error("No weight detected. Try with a clearer image.")

    # (Optional) Send to Salesforce button
    st.markdown("๐Ÿ”— [Send to Salesforce](#)", unsafe_allow_html=True)