Sanjayraju30's picture
Update app.py
4e7dfd4 verified
raw
history blame
1.41 kB
import gradio as gr
import easyocr
import re
import cv2
import numpy as np
from PIL import Image
from datetime import datetime
reader = easyocr.Reader(['en'], gpu=False)
def detect_weight(image):
if image is None:
return "No image uploaded", "N/A", None
# Convert PIL to OpenCV
image_np = np.array(image)
gray = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY)
# Optional: Enhance contrast
gray = cv2.equalizeHist(gray)
# OCR
results = reader.readtext(gray, detail=1)
weight = None
max_conf = 0
for (bbox, text, conf) in results:
text = text.strip()
match = re.search(r'\d+\.\d+', text)
if match and conf > max_conf:
weight = match.group()
max_conf = conf
if weight:
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return f"Weight: {weight} kg (Confidence: {round(max_conf*100, 2)}%)", now, image
else:
return "No weight detected kg (Confidence: 0.0%)", "N/A", image
gr.Interface(
fn=detect_weight,
inputs=gr.Image(type="pil", label="Upload or Capture Weight Image"),
outputs=[
gr.Text(label="Detected Weight"),
gr.Text(label="Captured At (IST)"),
gr.Image(label="Snapshot")
],
title="Auto Weight Logger",
description="Upload or capture a weight scale image. The app will detect and log the weight automatically."
).launch()