Autoweight / app.py
Sanjayraju30's picture
Update app.py
9bbe87e verified
raw
history blame
1.29 kB
import gradio as gr
from PIL import Image, UnidentifiedImageError
from datetime import datetime
import pytz
from ocr_engine import extract_weight_from_image
def detect_weight(image: Image.Image):
try:
# Get IST timestamp
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
# Perform OCR
weight, confidence = extract_weight_from_image(image)
if confidence > 0:
result = f"βœ… **Detected Weight:** {weight}\n\nπŸ“Š **Confidence:** {confidence:.2f}\n\nπŸ•’ **Captured At (IST):** {ist_time}"
else:
result = "❌ No weight detected. Please upload a clearer image."
return image, result
except UnidentifiedImageError:
return None, "❌ Invalid image format."
# Gradio UI
title = "βš–οΈ Auto Weight Logger"
description = "Upload or capture an image of a digital weighing scale to automatically extract the weight using OCR."
demo = gr.Interface(
fn=detect_weight,
inputs=gr.Image(type="pil", label="Upload or Capture Image"),
outputs=[
gr.Image(type="pil", label="Snapshot"),
gr.Markdown(label="Result")
],
title=title,
description=description,
allow_flagging="never"
)
if __name__ == "__main__":
demo.launch()