Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from datetime import datetime
|
3 |
import pytz
|
4 |
-
from
|
5 |
-
|
6 |
-
def process_image(
|
7 |
-
if
|
8 |
-
return "No image
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
submit.click(process_image, inputs=image_input, outputs=[output_weight, timestamp, snapshot, debug_output])
|
31 |
-
|
32 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
|
4 |
+
# Force install mmocr at runtime if not available
|
5 |
+
try:
|
6 |
+
from mmocr.utils.ocr import MMOCR
|
7 |
+
print("β
MMOCR module loaded successfully.")
|
8 |
+
except ImportError:
|
9 |
+
print("β οΈ MMOCR not found, installing...")
|
10 |
+
subprocess.run(["pip", "install", "mmocr==0.5.0", "--no-deps"])
|
11 |
+
from mmocr.utils.ocr import MMOCR
|
12 |
+
print("β
MMOCR installed and loaded.")
|
13 |
+
|
14 |
+
from ocr_engine import extract_weight_from_image
|
15 |
import gradio as gr
|
16 |
from datetime import datetime
|
17 |
import pytz
|
18 |
+
from PIL import Image
|
19 |
+
|
20 |
+
def process_image(image):
|
21 |
+
if image is None:
|
22 |
+
return "No image provided", "", None, None
|
23 |
+
|
24 |
+
weight, debug_text = extract_weight_from_image(image)
|
25 |
+
|
26 |
+
# Get IST time
|
27 |
+
ist = pytz.timezone('Asia/Kolkata')
|
28 |
+
captured_at = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S %Z")
|
29 |
+
|
30 |
+
return f"{weight} kg" if weight else "Not detected", captured_at, image, debug_text
|
31 |
+
|
32 |
+
gr.Interface(
|
33 |
+
fn=process_image,
|
34 |
+
inputs=gr.Image(type="pil", label="Upload Weight Image"),
|
35 |
+
outputs=[
|
36 |
+
gr.Textbox(label="Detected Weight"),
|
37 |
+
gr.Textbox(label="Captured At (IST)"),
|
38 |
+
gr.Image(label="Snapshot"),
|
39 |
+
gr.Textbox(label="OCR Debug Output")
|
40 |
+
],
|
41 |
+
title="Auto Weight Logger using MMOCR",
|
42 |
+
description="Upload a weight scale image. This app uses MMOCR to detect the weight."
|
43 |
+
).launch()
|
|
|
|
|
|