File size: 1,744 Bytes
a481416
8b5815c
7ec4a88
27ed571
 
a481416
 
 
94a9b1b
7ec4a88
a481416
94a9b1b
8b5815c
27ed571
94a9b1b
 
27ed571
94a9b1b
27ed571
 
94a9b1b
eff70bd
 
8b5815c
7ec4a88
6a56695
94a9b1b
7ec4a88
94a9b1b
 
a481416
94a9b1b
 
7ec4a88
8c50e18
94a9b1b
7ec4a88
94a9b1b
27ed571
94a9b1b
eff70bd
 
a481416
94a9b1b
eff70bd
 
 
94a9b1b
 
 
eff70bd
 
 
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
import gradio as gr
from PIL import Image, ImageEnhance, ImageOps
import numpy as np
import easyocr
import re
from datetime import datetime
import pytz

# Load EasyOCR model (CPU only)
reader = easyocr.Reader(['en'], gpu=False)

# Image enhancement
def enhance_image(image):
    image = image.convert("L")  # Grayscale
    image = ImageOps.invert(image)
    image = ImageEnhance.Contrast(image).enhance(2.5)
    image = ImageEnhance.Sharpness(image).enhance(2.5)
    image = image.resize((image.width * 2, image.height * 2))
    return image

# Detect weight from image
def detect_weight(image):
    try:
        processed_image = enhance_image(image)
        np_image = np.array(processed_image)

        # Perform OCR
        result = reader.readtext(np_image, detail=0)
        full_text = " ".join(result)
        print("OCR Output:", full_text)  # DEBUGGING LINE

        # Match both integer and decimal
        match = re.search(r"\d{1,4}(?:\.\d{1,4})?", full_text)
        weight = match.group(0) if match else "Not detected"

        # Get IST time
        ist = pytz.timezone("Asia/Kolkata")
        current_time = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S")

        return f"Weight: {weight} kg\nCaptured At: {current_time} (IST)\n\nRaw OCR: {full_text}", image
    except Exception as e:
        return f"Error: {str(e)}", image

# Gradio UI
interface = gr.Interface(
    fn=detect_weight,
    inputs=gr.Image(type="pil", label="Upload or Capture Image"),
    outputs=[gr.Textbox(label="Weight Info"), gr.Image(label="Snapshot")],
    title="⚖️ Accurate Auto Weight Detector (with Decimal)",
    description="Detects weight from digital scale image using EasyOCR (no Tesseract). Shows IST timestamp."
)

interface.launch()