Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,33 +5,32 @@ from ocr_engine import extract_weight
|
|
5 |
|
6 |
def process_image(image):
|
7 |
if image is None:
|
8 |
-
return "No image
|
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
|
17 |
|
18 |
with gr.Row():
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
|
24 |
-
def
|
25 |
-
return
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
weight_out = gr.Textbox(label="Detected Weight")
|
32 |
time_out = gr.Textbox(label="Captured At (IST)")
|
33 |
-
|
34 |
|
35 |
-
|
36 |
|
37 |
demo.launch()
|
|
|
5 |
|
6 |
def process_image(image):
|
7 |
if image is None:
|
8 |
+
return "No image provided", None, None
|
|
|
9 |
weight = extract_weight(image)
|
10 |
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
11 |
return f"{weight} grams", now, image
|
12 |
|
13 |
with gr.Blocks() as demo:
|
14 |
gr.Markdown("## π· Auto Weight Logger β OCR-Based Smart Scale Reader")
|
15 |
+
gr.Markdown("Upload a snapshot or use webcam. The app extracts weight using OCR.")
|
16 |
|
17 |
with gr.Row():
|
18 |
+
camera_input = gr.Camera(label="πΈ Capture via Webcam", type="pil")
|
19 |
+
upload_input = gr.Image(label="π Upload Image", type="pil")
|
20 |
|
21 |
+
selected_image = gr.State()
|
22 |
|
23 |
+
def choose_latest_image(cam_img, upload_img):
|
24 |
+
return cam_img if cam_img is not None else upload_img
|
25 |
|
26 |
+
camera_input.change(fn=choose_latest_image, inputs=[camera_input, upload_input], outputs=selected_image)
|
27 |
+
upload_input.change(fn=choose_latest_image, inputs=[camera_input, upload_input], outputs=selected_image)
|
28 |
|
29 |
+
btn = gr.Button("π Detect Weight")
|
30 |
weight_out = gr.Textbox(label="Detected Weight")
|
31 |
time_out = gr.Textbox(label="Captured At (IST)")
|
32 |
+
preview = gr.Image(label="Snapshot")
|
33 |
|
34 |
+
btn.click(fn=process_image, inputs=selected_image, outputs=[weight_out, time_out, preview])
|
35 |
|
36 |
demo.launch()
|