Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,53 @@
|
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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")
|
| 40 |
-
return f"Weight: {best_match} kg (Confidence: {round(best_conf * 100, 2)}%)", now_ist, image
|
| 41 |
-
else:
|
| 42 |
-
return "No weight detected kg (Confidence: 0.0%)", "N/A", image
|
| 43 |
-
|
| 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", 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()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
import cv2
|
| 5 |
import re
|
| 6 |
+
from paddleocr import PaddleOCR
|
| 7 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Initialize OCR model once
|
| 10 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 11 |
+
|
| 12 |
+
# Preprocessing: Convert to grayscale and threshold
|
| 13 |
+
def preprocess_image(image):
|
| 14 |
+
img = np.array(image.convert("RGB"))
|
| 15 |
+
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
| 16 |
+
_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
|
| 17 |
+
return Image.fromarray(thresh)
|
| 18 |
+
|
| 19 |
+
# OCR detection + regex filtering
|
| 20 |
+
def extract_weight_text(image):
|
| 21 |
+
results = ocr.ocr(np.array(image), cls=True)
|
| 22 |
+
for line in results[0]:
|
| 23 |
+
text = line[1][0]
|
| 24 |
+
match = re.search(r"\d+\.\d+", text)
|
| 25 |
+
if match:
|
| 26 |
+
return match.group()
|
| 27 |
+
return None
|
| 28 |
+
|
| 29 |
+
# Main function
|
| 30 |
def detect_weight(image):
|
| 31 |
+
if image is None:
|
| 32 |
+
return "No image uploaded.", "", None
|
| 33 |
+
pre_img = preprocess_image(image)
|
| 34 |
+
weight = extract_weight_text(pre_img)
|
| 35 |
+
if weight:
|
| 36 |
+
return f"Detected Weight: {weight} kg", datetime.now().strftime("Captured At: %Y-%m-%d %H:%M:%S"), pre_img
|
| 37 |
+
else:
|
| 38 |
+
return "Weight Not Detected", "", pre_img
|
| 39 |
+
|
| 40 |
+
# Gradio UI
|
| 41 |
+
interface = gr.Interface(
|
| 42 |
+
fn=detect_weight,
|
| 43 |
+
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
|
| 44 |
+
outputs=[
|
| 45 |
+
gr.Textbox(label="Weight"),
|
| 46 |
+
gr.Textbox(label="Timestamp"),
|
| 47 |
+
gr.Image(label="Preprocessed Image")
|
| 48 |
+
],
|
| 49 |
+
title="Auto Weight Logger",
|
| 50 |
+
description="Upload or click image of digital scale. It will detect and show the weight (kg).",
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|