File size: 1,343 Bytes
37bea37
 
 
 
 
 
 
 
 
 
 
 
 
 
c4904a4
 
 
37bea37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess

# Force install mmocr at runtime if not available
try:
    from mmocr.utils.ocr import MMOCR
    print("✅ MMOCR module loaded successfully.")
except ImportError:
    print("⚠️ MMOCR not found, installing...")
    subprocess.run(["pip", "install", "mmocr==0.5.0", "--no-deps"])
    from mmocr.utils.ocr import MMOCR
    print("✅ MMOCR installed and loaded.")

from ocr_engine import extract_weight_from_image
import gradio as gr
from datetime import datetime
import pytz
from PIL import Image

def process_image(image):
    if image is None:
        return "No image provided", "", None, None

    weight, debug_text = extract_weight_from_image(image)

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

    return f"{weight} kg" if weight else "Not detected", captured_at, image, debug_text

gr.Interface(
    fn=process_image,
    inputs=gr.Image(type="pil", label="Upload Weight Image"),
    outputs=[
        gr.Textbox(label="Detected Weight"),
        gr.Textbox(label="Captured At (IST)"),
        gr.Image(label="Snapshot"),
        gr.Textbox(label="OCR Debug Output")
    ],
    title="Auto Weight Logger using MMOCR",
    description="Upload a weight scale image. This app uses MMOCR to detect the weight."
).launch()