weightlogger / src /streamlit_app.py
Sanjayraju30's picture
Update src/streamlit_app.py
b0756ce verified
raw
history blame
1.19 kB
import streamlit as st
import numpy as np
import cv2
from paddleocr import PaddleOCR
from PIL import Image
import re
ocr = PaddleOCR(use_angle_cls=True, lang='en')
def preprocess_image(image):
img = np.array(image.convert("RGB"))
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
return Image.fromarray(thresh)
def extract_weight_text(image):
results = ocr.ocr(np.array(image), cls=True)
for line in results[0]:
text = line[1][0]
match = re.search(r"\d+\.\d+", text)
if match:
return match.group()
return None
st.title("πŸ“¦ Auto Weight Logger")
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
camera_image = st.camera_input("Or Capture Image")
input_image = uploaded_file or camera_image
if input_image:
image = Image.open(input_image)
st.image(image, caption="Original Image", use_column_width=True)
processed_image = preprocess_image(image)
weight = extract_weight_text(processed_image)
if weight:
st.success(f"βœ… Weight Detected: {weight} kg")
else:
st.error("❌ Weight not detected.")