File size: 1,761 Bytes
a481416
8b5815c
7ec4a88
27ed571
 
a481416
 
 
7ec4a88
 
a481416
7ec4a88
8b5815c
27ed571
7ec4a88
27ed571
 
 
 
 
7ec4a88
eff70bd
 
7ec4a88
8b5815c
e8bd3b9
7ec4a88
 
6a56695
7ec4a88
 
 
a481416
7ec4a88
 
 
8c50e18
7ec4a88
 
 
27ed571
7ec4a88
eff70bd
 
a481416
7ec4a88
eff70bd
 
 
7ec4a88
 
 
 
 
 
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
55
56
57
58
59
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 (no Tesseract needed)
reader = easyocr.Reader(['en'], gpu=False)

# Enhance image before OCR
def enhance_image(image):
    image = image.convert("L")  # Grayscale
    image = ImageOps.invert(image)  # Invert black ↔ white
    image = ImageEnhance.Contrast(image).enhance(2.0)
    image = ImageEnhance.Sharpness(image).enhance(2.5)
    image = image.resize((image.width * 2, image.height * 2))  # Enlarge
    return image

# Main function to extract weight
def detect_weight(image):
    try:
        # Enhance
        processed_image = enhance_image(image)

        # Convert to array
        np_image = np.array(processed_image)

        # OCR
        result = reader.readtext(np_image, detail=0)
        text = " ".join(result)

        # Extract decimal weight like 52.75
        match = re.search(r"\d{1,4}\.\d{1,4}", text)
        weight = match.group(0) if match else "Not detected"

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

        return f"Weight: {weight} kg\nCaptured At: {timestamp} (IST)", image
    except Exception as e:
        return f"Error: {str(e)}", image

# Gradio Interface
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="⚖️ Auto Weight Detector (Decimal Accurate)",
    description="Detects exact weight including decimals (e.g., 52.75 kg) using EasyOCR. Shows IST time."
)

interface.launch()