import gradio as gr from PIL import Image, ImageEnhance, ImageOps import easyocr import re from datetime import datetime import pytz # Initialize EasyOCR reader reader = easyocr.Reader(['en']) # Preprocessing to enhance image for OCR def enhance_image(image): image = image.convert("L") # Grayscale image = ImageOps.invert(image) # Invert 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 # Weight detection function def detect_weight(image): try: processed_image = enhance_image(image) # OCR using EasyOCR result = reader.readtext(np.array(processed_image), detail=0) full_text = " ".join(result) # Extract number with decimal point match = re.search(r"(\d{1,4}\.\d{1,4})", full_text) weight = match.group(1) 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)", 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="⚖️ Accurate Auto Weight Detector", description="Detects full decimal weight (e.g., 52.75 kg) using EasyOCR and shows timestamp (IST)" ) interface.launch()