Spaces:
Sleeping
Sleeping
Rename src/streamlit_app.py to app.py
Browse files- app.py +56 -0
- src/streamlit_app.py +0 -40
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from paddleocr import PaddleOCR
|
5 |
+
from PIL import Image
|
6 |
+
import re
|
7 |
+
from datetime import datetime
|
8 |
+
import pytz
|
9 |
+
|
10 |
+
# Load OCR Model
|
11 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en') # do not reinstall paddleocr here
|
12 |
+
|
13 |
+
# Preprocess image: grayscale + threshold
|
14 |
+
def preprocess_image(image):
|
15 |
+
img = np.array(image.convert("RGB"))
|
16 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
17 |
+
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
|
18 |
+
return Image.fromarray(thresh)
|
19 |
+
|
20 |
+
# Extract weight using regex
|
21 |
+
def extract_weight_text(image):
|
22 |
+
results = ocr.ocr(np.array(image), cls=True)
|
23 |
+
for line in results[0]:
|
24 |
+
text = line[1][0]
|
25 |
+
match = re.search(r"\d+\.\d+", text)
|
26 |
+
if match:
|
27 |
+
return match.group()
|
28 |
+
return None
|
29 |
+
|
30 |
+
# Streamlit UI
|
31 |
+
st.set_page_config(page_title="Auto Weight Logger", layout="centered")
|
32 |
+
st.title("π¦ Auto Weight Logger (Streamlit)")
|
33 |
+
st.write("Upload or capture an image of a digital weight display to extract weight.")
|
34 |
+
|
35 |
+
uploaded_img = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
36 |
+
camera_img = st.camera_input("Or click an image")
|
37 |
+
|
38 |
+
input_img = uploaded_img or camera_img
|
39 |
+
|
40 |
+
if input_img is not None:
|
41 |
+
image = Image.open(input_img)
|
42 |
+
st.image(image, caption="Original Image", use_column_width=True)
|
43 |
+
|
44 |
+
# Preprocess and show preprocessed image
|
45 |
+
pre_img = preprocess_image(image)
|
46 |
+
st.image(pre_img, caption="Preprocessed Image", use_column_width=True)
|
47 |
+
|
48 |
+
# Detect weight
|
49 |
+
weight = extract_weight_text(pre_img)
|
50 |
+
|
51 |
+
if weight:
|
52 |
+
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
|
53 |
+
st.success(f"β
Weight Detected: **{weight} kg**")
|
54 |
+
st.info(f"π Captured At (IST): {ist_time}")
|
55 |
+
else:
|
56 |
+
st.error("β Could not detect weight. Please try with a clearer image.")
|
src/streamlit_app.py
DELETED
@@ -1,40 +0,0 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
-
import streamlit as st
|
5 |
-
|
6 |
-
"""
|
7 |
-
# Welcome to Streamlit!
|
8 |
-
|
9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
11 |
-
forums](https://discuss.streamlit.io).
|
12 |
-
|
13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
14 |
-
"""
|
15 |
-
|
16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
18 |
-
|
19 |
-
indices = np.linspace(0, 1, num_points)
|
20 |
-
theta = 2 * np.pi * num_turns * indices
|
21 |
-
radius = indices
|
22 |
-
|
23 |
-
x = radius * np.cos(theta)
|
24 |
-
y = radius * np.sin(theta)
|
25 |
-
|
26 |
-
df = pd.DataFrame({
|
27 |
-
"x": x,
|
28 |
-
"y": y,
|
29 |
-
"idx": indices,
|
30 |
-
"rand": np.random.randn(num_points),
|
31 |
-
})
|
32 |
-
|
33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
34 |
-
.mark_point(filled=True)
|
35 |
-
.encode(
|
36 |
-
x=alt.X("x", axis=None),
|
37 |
-
y=alt.Y("y", axis=None),
|
38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|