Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
-
import numpy as np
|
| 4 |
from paddleocr import PaddleOCR
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
from pytz import timezone
|
| 7 |
-
import re
|
| 8 |
-
from PIL import Image
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 12 |
|
| 13 |
def detect_weight(image):
|
| 14 |
try:
|
|
@@ -22,7 +21,6 @@ def detect_weight(image):
|
|
| 22 |
enhanced = cv2.equalizeHist(gray)
|
| 23 |
rgb_image = cv2.cvtColor(enhanced, cv2.COLOR_GRAY2RGB)
|
| 24 |
|
| 25 |
-
# OCR (without cls= argument for compatibility)
|
| 26 |
result = ocr.ocr(rgb_image)
|
| 27 |
|
| 28 |
best_match = None
|
|
@@ -30,11 +28,12 @@ def detect_weight(image):
|
|
| 30 |
|
| 31 |
for line in result:
|
| 32 |
for box in line:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
if best_match:
|
| 40 |
now_ist = datetime.now(timezone('Asia/Kolkata')).strftime("%Y-%m-%d %H:%M:%S IST")
|
|
@@ -45,14 +44,16 @@ def detect_weight(image):
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error: {str(e)}", "Error", None
|
| 47 |
|
| 48 |
-
gr.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.
|
| 53 |
-
gr.
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from paddleocr import PaddleOCR
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
import re
|
| 7 |
from datetime import datetime
|
| 8 |
from pytz import timezone
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
ocr = PaddleOCR(use_angle_cls=False, lang='en') # cls removed
|
|
|
|
| 11 |
|
| 12 |
def detect_weight(image):
|
| 13 |
try:
|
|
|
|
| 21 |
enhanced = cv2.equalizeHist(gray)
|
| 22 |
rgb_image = cv2.cvtColor(enhanced, cv2.COLOR_GRAY2RGB)
|
| 23 |
|
|
|
|
| 24 |
result = ocr.ocr(rgb_image)
|
| 25 |
|
| 26 |
best_match = None
|
|
|
|
| 28 |
|
| 29 |
for line in result:
|
| 30 |
for box in line:
|
| 31 |
+
if len(box) > 1 and isinstance(box[1], tuple):
|
| 32 |
+
text, conf = box[1]
|
| 33 |
+
match = re.search(r"\d+\.\d+", text)
|
| 34 |
+
if match and conf > best_conf:
|
| 35 |
+
best_match = match.group()
|
| 36 |
+
best_conf = conf
|
| 37 |
|
| 38 |
if best_match:
|
| 39 |
now_ist = datetime.now(timezone('Asia/Kolkata')).strftime("%Y-%m-%d %H:%M:%S IST")
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
return f"Error: {str(e)}", "Error", None
|
| 46 |
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("## Auto Weight Logger\nUpload or capture a digital scale image. This app detects the weight automatically using AI.")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
image_input = gr.Image(type="pil", label="Upload or Capture Weight Image", tool="editor", sources=["upload", "camera"])
|
| 52 |
+
with gr.Column():
|
| 53 |
+
output_text = gr.Textbox(label="Detected Weight")
|
| 54 |
+
output_time = gr.Textbox(label="Captured At (IST)")
|
| 55 |
+
snapshot_output = gr.Image(label="📷 Snapshot")
|
| 56 |
+
|
| 57 |
+
image_input.change(fn=detect_weight, inputs=image_input, outputs=[output_text, output_time, snapshot_output])
|
| 58 |
+
|
| 59 |
+
demo.launch()
|