Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,119 +1,37 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
-
import
|
5 |
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
elif image_source == "upload" and image_upload is not None:
|
16 |
-
img = image_upload
|
17 |
-
elif image_source == "url" and image_url:
|
18 |
-
import requests
|
19 |
-
from io import BytesIO
|
20 |
-
response = requests.get(image_url)
|
21 |
-
img = Image.open(BytesIO(response.content))
|
22 |
-
else:
|
23 |
-
return {
|
24 |
-
"weight": None,
|
25 |
-
"message": "β οΈ No image provided!",
|
26 |
-
"image": None,
|
27 |
-
"time": detector.get_current_ist()
|
28 |
-
}
|
29 |
-
|
30 |
-
# Save to temp file
|
31 |
-
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
|
32 |
-
temp_img_path = f.name
|
33 |
-
img.save(f.name)
|
34 |
-
|
35 |
-
# Detect weight
|
36 |
-
return detector.detect_weight(temp_img_path)
|
37 |
-
|
38 |
-
except Exception as e:
|
39 |
-
return {
|
40 |
-
"weight": None,
|
41 |
-
"message": f"β οΈ Error: {str(e)}",
|
42 |
-
"image": None,
|
43 |
-
"time": detector.get_current_ist()
|
44 |
-
}
|
45 |
-
finally:
|
46 |
-
if temp_img_path and os.path.exists(temp_img_path):
|
47 |
-
os.remove(temp_img_path)
|
48 |
|
49 |
-
# Gradio UI
|
50 |
-
with gr.Blocks(title="Auto Weight Logger") as demo:
|
51 |
-
gr.Markdown("""
|
52 |
-
# **βοΈ Auto Weight Logger (7-Segment OCR)**
|
53 |
-
**Capture weight from digital balances using a webcam or image upload.**
|
54 |
-
- β
Optimized for **7-segment displays** (e.g., lab balances)
|
55 |
-
- π
Logs **IST time** automatically
|
56 |
-
- π« Detects **blurry/glare** images
|
57 |
-
""")
|
58 |
-
|
59 |
with gr.Row():
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
)
|
78 |
-
|
79 |
-
submit_btn = gr.Button("Detect Weight", variant="primary")
|
80 |
-
|
81 |
-
with gr.Column():
|
82 |
-
weight_value = gr.Number(
|
83 |
-
label="Detected Weight (g)",
|
84 |
-
interactive=False
|
85 |
-
)
|
86 |
-
|
87 |
-
detection_time = gr.Textbox(
|
88 |
-
label="Detection Time (IST)",
|
89 |
-
interactive=False
|
90 |
-
)
|
91 |
-
|
92 |
-
result_message = gr.Textbox(
|
93 |
-
label="Result",
|
94 |
-
interactive=False
|
95 |
-
)
|
96 |
-
|
97 |
-
annotated_image = gr.Image(
|
98 |
-
label="Annotated Image",
|
99 |
-
interactive=False
|
100 |
-
)
|
101 |
-
|
102 |
-
# Show/hide URL input
|
103 |
-
def toggle_url_visibility(source):
|
104 |
-
return gr.Textbox(visible=source == "url")
|
105 |
-
|
106 |
-
image_source.change(
|
107 |
-
toggle_url_visibility,
|
108 |
-
inputs=image_source,
|
109 |
-
outputs=image_url
|
110 |
-
)
|
111 |
-
|
112 |
-
# Process input
|
113 |
-
submit_btn.click(
|
114 |
-
process_input,
|
115 |
-
inputs=[image_source, image_upload, image_url],
|
116 |
-
outputs=[weight_value, detection_time, result_message, annotated_image]
|
117 |
-
)
|
118 |
|
119 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
from datetime import datetime
|
4 |
+
from ocr_engine import extract_weight
|
5 |
|
6 |
+
def process_image(image):
|
7 |
+
if image is None:
|
8 |
+
return "No image received", None, None
|
9 |
|
10 |
+
weight = extract_weight(image)
|
11 |
+
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
12 |
+
return f"{weight} grams", now, image
|
13 |
+
|
14 |
+
with gr.Blocks() as demo:
|
15 |
+
gr.Markdown("## π· Auto Weight Logger β OCR-Based Smart Scale Reader")
|
16 |
+
gr.Markdown("Upload a snapshot of a digital weight display or use webcam. The app extracts weight using OCR.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
with gr.Row():
|
19 |
+
webcam = gr.Image(source="webcam", type="pil", label="πΈ Capture from Webcam")
|
20 |
+
upload = gr.Image(source="upload", type="pil", label="π Or Upload Image")
|
21 |
+
|
22 |
+
image_input = gr.State()
|
23 |
+
|
24 |
+
def choose_image(w, u):
|
25 |
+
return w if w else u
|
26 |
+
|
27 |
+
webcam.change(fn=choose_image, inputs=[webcam, upload], outputs=image_input)
|
28 |
+
upload.change(fn=choose_image, inputs=[webcam, upload], outputs=image_input)
|
29 |
+
|
30 |
+
submit_btn = gr.Button("π Detect Weight")
|
31 |
+
weight_out = gr.Textbox(label="Detected Weight")
|
32 |
+
time_out = gr.Textbox(label="Captured At (IST)")
|
33 |
+
snapshot_out = gr.Image(label="Snapshot")
|
34 |
+
|
35 |
+
submit_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
demo.launch()
|