Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import logging
|
| 6 |
+
from ocr_engine import extract_weight_from_image
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import pytz
|
| 9 |
+
import sys
|
| 10 |
+
|
| 11 |
+
# Set up logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[logging.StreamHandler(sys.stdout)])
|
| 13 |
+
|
| 14 |
+
def process_image(img):
|
| 15 |
+
try:
|
| 16 |
+
# Convert Gradio image (PIL) to process
|
| 17 |
+
if img is None:
|
| 18 |
+
return "No image provided", 0.0, "", None
|
| 19 |
+
|
| 20 |
+
# Resize if > 5MB
|
| 21 |
+
img_bytes = img.tobytes()
|
| 22 |
+
size_mb = len(img_bytes) / (1024 * 1024)
|
| 23 |
+
if size_mb > 5:
|
| 24 |
+
scale = 0.9
|
| 25 |
+
while size_mb > 5:
|
| 26 |
+
w, h = img.size
|
| 27 |
+
img = img.resize((int(w * scale), int(h * scale)), Image.Resampling.LANCZOS)
|
| 28 |
+
img_bytes = img.tobytes()
|
| 29 |
+
size_mb = len(img_bytes) / (1024 * 1024)
|
| 30 |
+
scale *= 0.9
|
| 31 |
+
logging.info(f"Resized image to {size_mb:.2f} MB")
|
| 32 |
+
|
| 33 |
+
# Extract weight
|
| 34 |
+
weight, confidence, unit = extract_weight_from_image(img)
|
| 35 |
+
|
| 36 |
+
# Return results
|
| 37 |
+
return f"{weight} {unit} (Confidence: {confidence:.2f}%)", f"Processed at {datetime.now(pytz.timezone('Asia/Kolkata')).strftime('%d-%m-%Y %I:%M:%S %p IST')}", img
|
| 38 |
+
except Exception as e:
|
| 39 |
+
logging.error(f"Error in process_image: {str(e)}")
|
| 40 |
+
return f"Error: {str(e)}", "", None
|
| 41 |
+
|
| 42 |
+
# Gradio interface
|
| 43 |
+
with gr.Blocks(title="Auto Weight Logger") as demo:
|
| 44 |
+
gr.Markdown("""
|
| 45 |
+
# π· Auto Weight Logger β OCR-Based Smart Scale Reader
|
| 46 |
+
This app detects weight from uploaded or captured images of digital balance displays. Optimized for 7-segment displays and various formats, it extracts numeric weights with high accuracy.
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column():
|
| 51 |
+
image_input = gr.Image(source="upload", tool="select", type="pil", label="Upload Weight Display Image")
|
| 52 |
+
webcam_input = gr.Image(source="webcam", type="pil", label="Or Capture with Webcam")
|
| 53 |
+
submit_btn = gr.Button("Detect Weight")
|
| 54 |
+
|
| 55 |
+
with gr.Column():
|
| 56 |
+
output_text = gr.Textbox(label="Detected Weight", interactive=False)
|
| 57 |
+
timestamp_text = gr.Textbox(label="Processed At", interactive=False)
|
| 58 |
+
output_image = gr.Image(label="Processed Image")
|
| 59 |
+
|
| 60 |
+
submit_btn.click(
|
| 61 |
+
fn=process_image,
|
| 62 |
+
inputs=[image_input],
|
| 63 |
+
outputs=[output_text, timestamp_text, output_image]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
webcam_input.change(
|
| 67 |
+
fn=process_image,
|
| 68 |
+
inputs=[webcam_input],
|
| 69 |
+
outputs=[output_text, timestamp_text, output_image]
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
demo.launch()
|