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()