Autoweight / app.py
Sanjayraju30's picture
Update app.py
788bf64 verified
raw
history blame
1.72 kB
import streamlit as st
from PIL import Image, UnidentifiedImageError
from datetime import datetime
import pytz
from ocr_engine import extract_weight_from_image
# Streamlit page config
st.set_page_config(page_title="βš–οΈ Auto Weight Logger", layout="centered")
st.title("βš–οΈ Auto Weight Logger")
# Debug: Confirm app starts
st.write("βœ… Streamlit app initialized.")
if "camera_key" not in st.session_state:
st.session_state["camera_key"] = 0
# Upload or Capture
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.")
# OCR Process
if image:
st.markdown("### πŸ•’ Captured At (IST)")
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
st.info(ist_time)
st.markdown("### πŸ–ΌοΈ Snapshot Image")
st.image(image, width=400)
with st.spinner("πŸ” Detecting weight..."):
weight, confidence = extract_weight_from_image(image)
st.markdown("### βš–οΈ Captured Weight & Confidence")
if confidence > 0:
st.success(f"Detected Weight: **{weight}** \nConfidence: `{confidence:.2f}`")
else:
st.error("No weight detected. Please upload a clearer image.")