Sanjayraju30 commited on
Commit
9bbe87e
Β·
verified Β·
1 Parent(s): 65aeab5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -46
app.py CHANGED
@@ -1,55 +1,40 @@
1
- import streamlit as st
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
- # Streamlit page config
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
- image = Image.open(camera_image)
36
- except UnidentifiedImageError:
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
- st.markdown("### πŸ–ΌοΈ Snapshot Image")
46
- st.image(image, width=400)
47
-
48
- with st.spinner("πŸ” Detecting weight..."):
49
  weight, confidence = extract_weight_from_image(image)
50
 
51
- st.markdown("### βš–οΈ Captured Weight & Confidence")
52
- if confidence > 0:
53
- st.success(f"Detected Weight: **{weight}** \nConfidence: `{confidence:.2f}`")
54
- else:
55
- st.error("No weight detected. Please upload a clearer image.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()