import os os.environ['PADDLEX_HOME'] = '/tmp/.paddlex' # Fix for Hugging Face permission error from paddleocr import PaddleOCR from PIL import Image import streamlit as st import numpy as np import re # Initialize PaddleOCR model ocr = PaddleOCR(use_angle_cls=True, lang='en') # Enable angle classification # Streamlit page setup st.set_page_config(page_title="Auto Weight Logger", layout="centered") st.title("📷 Auto Weight Logger") # File uploader uploaded_file = st.file_uploader("Upload an image of the weight display", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: # Display uploaded image image = Image.open(uploaded_file).convert("RGB") st.image(image, caption="Uploaded Image", use_column_width=True) # Convert image to numpy array for OCR img_array = np.array(image) # OCR Detection with st.spinner("🔍 Detecting weight..."): result = ocr.ocr(img_array, cls=True) weight = "Not detected" confidence = 0.0 for line in result[0]: for word_info in line: text, conf = word_info[1] match = re.search(r'\d+\.\d+|\d+', text) if match: weight = match.group() confidence = conf break if weight != "Not detected": break st.markdown(f"### ✅ Weight: **{weight} kg** (Confidence: {confidence:.2%})")