Spaces:
Running
Running
File size: 1,636 Bytes
a481416 8b5815c 27ed571 a481416 27ed571 a481416 27ed571 8b5815c 27ed571 eff70bd 8b5815c e8bd3b9 27ed571 6a56695 27ed571 eff70bd a481416 27ed571 eff70bd 8c50e18 e8bd3b9 27ed571 eff70bd a481416 27ed571 eff70bd 27ed571 eff70bd |
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 45 46 47 48 49 50 51 52 |
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()
|