File size: 1,858 Bytes
53ad608
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import streamlit as st
import numpy as np
import cv2
from paddleocr import PaddleOCR
from PIL import Image
import re
from datetime import datetime
import pytz

# Load OCR Model
ocr = PaddleOCR(use_angle_cls=True, lang='en')  # do not reinstall paddleocr here

# Preprocess image: grayscale + threshold
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)

# Extract weight using regex
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

# Streamlit UI
st.set_page_config(page_title="Auto Weight Logger", layout="centered")
st.title("πŸ“¦ Auto Weight Logger (Streamlit)")
st.write("Upload or capture an image of a digital weight display to extract weight.")

uploaded_img = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
camera_img = st.camera_input("Or click an image")

input_img = uploaded_img or camera_img

if input_img is not None:
    image = Image.open(input_img)
    st.image(image, caption="Original Image", use_column_width=True)

    # Preprocess and show preprocessed image
    pre_img = preprocess_image(image)
    st.image(pre_img, caption="Preprocessed Image", use_column_width=True)

    # Detect weight
    weight = extract_weight_text(pre_img)

    if weight:
        ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
        st.success(f"βœ… Weight Detected: **{weight} kg**")
        st.info(f"πŸ•’ Captured At (IST): {ist_time}")
    else:
        st.error("❌ Could not detect weight. Please try with a clearer image.")