Spaces:
Build error
Build error
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.") | |