Spaces:
Sleeping
Sleeping
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 (CPU only) | |
reader = easyocr.Reader(['en'], gpu=False) | |
# Image enhancement | |
def enhance_image(image): | |
image = image.convert("L") # Grayscale | |
image = ImageOps.invert(image) | |
image = ImageEnhance.Contrast(image).enhance(2.5) | |
image = ImageEnhance.Sharpness(image).enhance(2.5) | |
image = image.resize((image.width * 2, image.height * 2)) | |
return image | |
# Detect weight from image | |
def detect_weight(image): | |
try: | |
processed_image = enhance_image(image) | |
np_image = np.array(processed_image) | |
# Perform OCR | |
result = reader.readtext(np_image, detail=0) | |
full_text = " ".join(result) | |
print("OCR Output:", full_text) # DEBUGGING LINE | |
# Match both integer and decimal | |
match = re.search(r"\d{1,4}(?:\.\d{1,4})?", full_text) | |
weight = match.group(0) 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)\n\nRaw OCR: {full_text}", 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="⚖️ Accurate Auto Weight Detector (with Decimal)", | |
description="Detects weight from digital scale image using EasyOCR (no Tesseract). Shows IST timestamp." | |
) | |
interface.launch() | |