Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,40 @@
|
|
1 |
-
import
|
2 |
from PIL import Image, UnidentifiedImageError
|
3 |
from datetime import datetime
|
4 |
import pytz
|
5 |
from ocr_engine import extract_weight_from_image
|
6 |
|
7 |
-
|
8 |
-
st.set_page_config(page_title="βοΈ Auto Weight Logger", layout="centered")
|
9 |
-
st.title("βοΈ Auto Weight Logger")
|
10 |
-
|
11 |
-
# Debug: Confirm app starts
|
12 |
-
st.write("β
Streamlit app initialized.")
|
13 |
-
|
14 |
-
if "camera_key" not in st.session_state:
|
15 |
-
st.session_state["camera_key"] = 0
|
16 |
-
|
17 |
-
# Upload or Capture
|
18 |
-
col1, col2 = st.columns(2)
|
19 |
-
with col1:
|
20 |
-
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
21 |
-
with col2:
|
22 |
-
if st.button("πΈ Retake Photo"):
|
23 |
-
st.session_state["camera_key"] += 1
|
24 |
-
camera_image = st.camera_input("Capture Image", key=st.session_state["camera_key"])
|
25 |
-
|
26 |
-
# Load image
|
27 |
-
image = None
|
28 |
-
if uploaded_file:
|
29 |
-
try:
|
30 |
-
image = Image.open(uploaded_file)
|
31 |
-
except UnidentifiedImageError:
|
32 |
-
st.error("Invalid image format.")
|
33 |
-
elif camera_image:
|
34 |
try:
|
35 |
-
|
36 |
-
|
37 |
-
st.error("Invalid camera image.")
|
38 |
-
|
39 |
-
# OCR Process
|
40 |
-
if image:
|
41 |
-
st.markdown("### π Captured At (IST)")
|
42 |
-
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
|
43 |
-
st.info(ist_time)
|
44 |
|
45 |
-
|
46 |
-
st.image(image, width=400)
|
47 |
-
|
48 |
-
with st.spinner("π Detecting weight..."):
|
49 |
weight, confidence = extract_weight_from_image(image)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
from PIL import Image, UnidentifiedImageError
|
3 |
from datetime import datetime
|
4 |
import pytz
|
5 |
from ocr_engine import extract_weight_from_image
|
6 |
|
7 |
+
def detect_weight(image: Image.Image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
try:
|
9 |
+
# Get IST timestamp
|
10 |
+
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Perform OCR
|
|
|
|
|
|
|
13 |
weight, confidence = extract_weight_from_image(image)
|
14 |
|
15 |
+
if confidence > 0:
|
16 |
+
result = f"β
**Detected Weight:** {weight}\n\nπ **Confidence:** {confidence:.2f}\n\nπ **Captured At (IST):** {ist_time}"
|
17 |
+
else:
|
18 |
+
result = "β No weight detected. Please upload a clearer image."
|
19 |
+
return image, result
|
20 |
+
except UnidentifiedImageError:
|
21 |
+
return None, "β Invalid image format."
|
22 |
+
|
23 |
+
# Gradio UI
|
24 |
+
title = "βοΈ Auto Weight Logger"
|
25 |
+
description = "Upload or capture an image of a digital weighing scale to automatically extract the weight using OCR."
|
26 |
+
|
27 |
+
demo = gr.Interface(
|
28 |
+
fn=detect_weight,
|
29 |
+
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
30 |
+
outputs=[
|
31 |
+
gr.Image(type="pil", label="Snapshot"),
|
32 |
+
gr.Markdown(label="Result")
|
33 |
+
],
|
34 |
+
title=title,
|
35 |
+
description=description,
|
36 |
+
allow_flagging="never"
|
37 |
+
)
|
38 |
+
|
39 |
+
if __name__ == "__main__":
|
40 |
+
demo.launch()
|