import gradio as gr from PIL import Image, ImageEnhance, ImageOps import torch from transformers import TrOCRProcessor, VisionEncoderDecoderModel from datetime import datetime import pytz import re # Load model and processor processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1") model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1") # Enhance image before OCR def enhance_image(image): # Convert to grayscale image = image.convert("L") # Invert (light text on dark bg works better) image = ImageOps.invert(image) # Increase contrast and sharpness image = ImageEnhance.Contrast(image).enhance(2.0) image = ImageEnhance.Sharpness(image).enhance(2.0) # Resize (bigger = easier for OCR) image = image.resize((image.width * 2, image.height * 2)) # Convert back to RGB for model compatibility image = image.convert("RGB") return image # Extract weight def detect_weight(image): try: processed_image = enhance_image(image) # Send to Hugging Face OCR model pixel_values = processor(images=processed_image, return_tensors="pt").pixel_values generated_ids = model.generate(pixel_values) generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] # Extract number using regex match = re.search(r"(\d{1,4}(?:\.\d{1,2})?)", generated_text) weight = match.group(1) if match else "Not detected" # Get current 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 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="⚖️ Auto Weight Detector (No Tesseract)", description="Detects weight from digital scale image using Hugging Face TrOCR. Shows weight and capture time (IST)." ) interface.launch()