Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,58 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
2 |
from ocr_engine import extract_weight_from_image
|
3 |
-
from utils import get_ist_time
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
try:
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
weight, confidence = extract_weight_from_image(image)
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|