File size: 2,077 Bytes
a481416
7f861bc
12d76b5
 
 
 
a481416
57ffefc
a481416
12d76b5
a481416
eff70bd
0263481
 
 
 
 
 
 
 
 
 
 
57ffefc
0263481
 
 
 
 
 
12d76b5
 
 
 
 
 
0263481
 
57ffefc
 
0263481
 
 
 
 
4e7dfd4
12d76b5
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
from paddleocr import PaddleOCR
from PIL import Image
import numpy as np
import cv2
import re
from datetime import datetime
from pytz import timezone

ocr = PaddleOCR(use_angle_cls=False, lang='en')  # cls removed

def detect_weight(image):
    try:
        if image is None:
            return "No image uploaded", "N/A", None

        image = image.convert("RGB")
        image_np = np.array(image)

        gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
        enhanced = cv2.equalizeHist(gray)
        rgb_image = cv2.cvtColor(enhanced, cv2.COLOR_GRAY2RGB)

        result = ocr.ocr(rgb_image)

        best_match = None
        best_conf = 0

        for line in result:
            for box in line:
                if len(box) > 1 and isinstance(box[1], tuple):
                    text, conf = box[1]
                    match = re.search(r"\d+\.\d+", text)
                    if match and conf > best_conf:
                        best_match = match.group()
                        best_conf = conf

        if best_match:
            now_ist = datetime.now(timezone('Asia/Kolkata')).strftime("%Y-%m-%d %H:%M:%S IST")
            return f"Weight: {best_match} kg (Confidence: {round(best_conf * 100, 2)}%)", now_ist, image
        else:
            return "No weight detected kg (Confidence: 0.0%)", "N/A", image

    except Exception as e:
        return f"Error: {str(e)}", "Error", None

with gr.Blocks() as demo:
    gr.Markdown("## Auto Weight Logger\nUpload or capture a digital scale image. This app detects the weight automatically using AI.")

    with gr.Row():
        image_input = gr.Image(type="pil", label="Upload or Capture Weight Image", tool="editor", sources=["upload", "camera"])
        with gr.Column():
            output_text = gr.Textbox(label="Detected Weight")
            output_time = gr.Textbox(label="Captured At (IST)")
            snapshot_output = gr.Image(label="📷 Snapshot")

    image_input.change(fn=detect_weight, inputs=image_input, outputs=[output_text, output_time, snapshot_output])

demo.launch()