Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
from PIL import Image
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
enhancer = ImageEnhance.Contrast(pil_img)
|
| 11 |
-
return enhancer.enhance(2.0)
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
image_input = gr.Image(type="pil", label="Upload / Capture Image")
|
| 30 |
-
output_weight = gr.Textbox(label="⚖️ Detected Weight (in kg)")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import easyocr
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
import re
|
|
|
|
| 6 |
|
| 7 |
+
# Initialize OCR reader
|
| 8 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Preprocessing function to enhance OCR accuracy
|
| 11 |
+
def preprocess(img):
|
| 12 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 13 |
|
| 14 |
+
# Sharpening kernel (helps deblur weak edges)
|
| 15 |
+
sharpen_kernel = np.array([[-1, -1, -1],
|
| 16 |
+
[-1, 9, -1],
|
| 17 |
+
[-1, -1, -1]])
|
| 18 |
+
sharp = cv2.filter2D(gray, -1, sharpen_kernel)
|
| 19 |
|
| 20 |
+
# Adaptive thresholding for binarization
|
| 21 |
+
thresh = cv2.adaptiveThreshold(sharp, 255,
|
| 22 |
+
cv2.ADAPTIVE_THRESH_MEAN_C,
|
| 23 |
+
cv2.THRESH_BINARY_INV, 11, 4)
|
| 24 |
|
| 25 |
+
# Dilation to strengthen character boundaries
|
| 26 |
+
kernel = np.ones((2, 2), np.uint8)
|
| 27 |
+
dilated = cv2.dilate(thresh, kernel, iterations=1)
|
| 28 |
|
| 29 |
+
return dilated
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Extract weight using regex
|
| 32 |
+
def extract_weight(results):
|
| 33 |
+
for text, _, _ in results:
|
| 34 |
+
match = re.search(r'\b\d{2,4}(\.\d{1,2})?\b', text)
|
| 35 |
+
if match:
|
| 36 |
+
return match.group()
|
| 37 |
+
return None
|
| 38 |
|
| 39 |
+
# Complete OCR pipeline
|
| 40 |
+
def get_weight(img):
|
| 41 |
+
# Convert RGB to BGR
|
| 42 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 43 |
|
| 44 |
+
# Resize small or distant images (heuristic)
|
| 45 |
+
if img.shape[0] < 500:
|
| 46 |
+
img = cv2.resize(img, None, fx=2.0, fy=2.0,
|
| 47 |
+
interpolation=cv2.INTER_CUBIC)
|
| 48 |
|
| 49 |
+
preprocessed = preprocess(img)
|
| 50 |
+
results = reader.readtext(preprocessed)
|
| 51 |
+
weight = extract_weight(results)
|
| 52 |
+
|
| 53 |
+
return f"Detected Weight: {weight if weight else 'Not found'}"
|
| 54 |
+
|
| 55 |
+
# Gradio UI
|
| 56 |
+
demo = gr.Interface(
|
| 57 |
+
fn=get_weight,
|
| 58 |
+
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 59 |
+
outputs=gr.Textbox(label="Detected Weight"),
|
| 60 |
+
title="Auto Weight Logger (Blur & Distance Aware)",
|
| 61 |
+
description="Extracts weight from digital display images using OCR & preprocessing."
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
demo.launch()
|