Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,30 +2,25 @@ import gradio as gr
|
|
| 2 |
from datetime import datetime
|
| 3 |
from PIL import ImageFilter, ImageEnhance
|
| 4 |
import pytz
|
| 5 |
-
import numpy as np
|
| 6 |
-
import cv2
|
| 7 |
from ocr_engine import extract_weight_from_image
|
| 8 |
|
| 9 |
-
def preprocess_image(img):
|
| 10 |
-
img = img.filter(ImageFilter.SHARPEN)
|
| 11 |
-
enhancer = ImageEnhance.Contrast(img)
|
| 12 |
-
img = enhancer.enhance(2.0)
|
| 13 |
-
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 14 |
-
return img_cv
|
| 15 |
-
|
| 16 |
def process_image(img):
|
| 17 |
if img is None:
|
| 18 |
return "No image uploaded", None, None, "No OCR output"
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
| 24 |
-
return f"{weight} kg (Confidence: {confidence}%)", ist_time,
|
| 25 |
|
| 26 |
with gr.Blocks(title="โ๏ธ Auto Weight Logger") as demo:
|
| 27 |
gr.Markdown("## โ๏ธ Auto Weight Logger")
|
| 28 |
-
gr.Markdown("๐ท **Upload or capture an image of a digital weight scale.**
|
| 29 |
|
| 30 |
with gr.Row():
|
| 31 |
image_input = gr.Image(type="pil", label="Upload / Capture Image")
|
|
@@ -33,12 +28,16 @@ with gr.Blocks(title="โ๏ธ Auto Weight Logger") as demo:
|
|
| 33 |
|
| 34 |
with gr.Row():
|
| 35 |
timestamp = gr.Textbox(label="๐ Captured At (IST)")
|
| 36 |
-
snapshot = gr.Image(label="๐ธ Snapshot
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
debug_output = gr.Textbox(label="๐ชต Raw OCR Output")
|
| 40 |
|
| 41 |
submit = gr.Button("๐ Detect Weight")
|
| 42 |
-
submit.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
demo.launch()
|
|
|
|
| 2 |
from datetime import datetime
|
| 3 |
from PIL import ImageFilter, ImageEnhance
|
| 4 |
import pytz
|
|
|
|
|
|
|
| 5 |
from ocr_engine import extract_weight_from_image
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def process_image(img):
|
| 8 |
if img is None:
|
| 9 |
return "No image uploaded", None, None, "No OCR output"
|
| 10 |
|
| 11 |
+
# Enhance image before deblurring
|
| 12 |
+
img = img.filter(ImageFilter.SHARPEN)
|
| 13 |
+
enhancer = ImageEnhance.Contrast(img)
|
| 14 |
+
img = enhancer.enhance(2.0)
|
| 15 |
+
|
| 16 |
+
weight, confidence, raw_text, deblurred_img = extract_weight_from_image(img)
|
| 17 |
|
| 18 |
ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%d-%m-%Y %I:%M:%S %p")
|
| 19 |
+
return f"{weight} kg (Confidence: {confidence}%)", ist_time, deblurred_img, raw_text
|
| 20 |
|
| 21 |
with gr.Blocks(title="โ๏ธ Auto Weight Logger") as demo:
|
| 22 |
gr.Markdown("## โ๏ธ Auto Weight Logger")
|
| 23 |
+
gr.Markdown("๐ท **Upload or capture an image of a digital weight scale.** This version uses Restormer to deblur before OCR.")
|
| 24 |
|
| 25 |
with gr.Row():
|
| 26 |
image_input = gr.Image(type="pil", label="Upload / Capture Image")
|
|
|
|
| 28 |
|
| 29 |
with gr.Row():
|
| 30 |
timestamp = gr.Textbox(label="๐ Captured At (IST)")
|
| 31 |
+
snapshot = gr.Image(label="๐ธ Deblurred Snapshot")
|
| 32 |
|
| 33 |
with gr.Row():
|
| 34 |
debug_output = gr.Textbox(label="๐ชต Raw OCR Output")
|
| 35 |
|
| 36 |
submit = gr.Button("๐ Detect Weight")
|
| 37 |
+
submit.click(
|
| 38 |
+
process_image,
|
| 39 |
+
inputs=image_input,
|
| 40 |
+
outputs=[output_weight, timestamp, snapshot, debug_output]
|
| 41 |
+
)
|
| 42 |
|
| 43 |
demo.launch()
|