Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,61 +4,60 @@ import cv2
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
|
| 7 |
-
# Initialize
|
| 8 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
def preprocess(img):
|
| 12 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
sharpen_kernel = np.array([[-1, -1, -1],
|
| 16 |
-
[-1,
|
| 17 |
[-1, -1, -1]])
|
| 18 |
sharp = cv2.filter2D(gray, -1, sharpen_kernel)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
thresh = cv2.adaptiveThreshold(
|
| 22 |
-
|
| 23 |
-
cv2.THRESH_BINARY_INV, 11, 4)
|
| 24 |
|
| 25 |
-
#
|
| 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
|
| 34 |
-
|
| 35 |
-
if
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
return None
|
| 38 |
|
| 39 |
-
#
|
| 40 |
def get_weight(img):
|
| 41 |
-
# Convert RGB to BGR
|
| 42 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 43 |
|
| 44 |
-
# Resize small
|
| 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 |
-
|
| 50 |
-
results = reader.readtext(
|
| 51 |
weight = extract_weight(results)
|
| 52 |
|
| 53 |
return f"Detected Weight: {weight if weight else 'Not found'}"
|
| 54 |
|
| 55 |
-
# Gradio
|
| 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
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import re
|
| 6 |
|
| 7 |
+
# Initialize EasyOCR reader
|
| 8 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 9 |
|
| 10 |
+
# Step 1: Preprocessing Function
|
| 11 |
def preprocess(img):
|
| 12 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 13 |
|
| 14 |
+
# Apply sharpening for deblurring
|
| 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 |
+
# Apply adaptive thresholding
|
| 21 |
+
thresh = cv2.adaptiveThreshold(
|
| 22 |
+
sharp, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
|
|
|
|
| 23 |
|
| 24 |
+
# Strengthen characters using dilation
|
| 25 |
kernel = np.ones((2, 2), np.uint8)
|
| 26 |
dilated = cv2.dilate(thresh, kernel, iterations=1)
|
| 27 |
|
| 28 |
return dilated
|
| 29 |
|
| 30 |
+
# Step 2: Extract weight using regex
|
| 31 |
def extract_weight(results):
|
| 32 |
+
for result in results:
|
| 33 |
+
text = result[0] if isinstance(result, (list, tuple)) else ""
|
| 34 |
+
if isinstance(text, str):
|
| 35 |
+
match = re.search(r'\b\d{2,4}(\.\d{1,2})?\b', text)
|
| 36 |
+
if match:
|
| 37 |
+
return match.group()
|
| 38 |
return None
|
| 39 |
|
| 40 |
+
# Step 3: Main OCR Function
|
| 41 |
def get_weight(img):
|
|
|
|
| 42 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 43 |
|
| 44 |
+
# Resize small (distant) images
|
| 45 |
if img.shape[0] < 500:
|
| 46 |
+
img = cv2.resize(img, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)
|
|
|
|
| 47 |
|
| 48 |
+
processed = preprocess(img)
|
| 49 |
+
results = reader.readtext(processed)
|
| 50 |
weight = extract_weight(results)
|
| 51 |
|
| 52 |
return f"Detected Weight: {weight if weight else 'Not found'}"
|
| 53 |
|
| 54 |
+
# Step 4: Gradio Interface
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=get_weight,
|
| 57 |
inputs=gr.Image(type="numpy", label="Upload Image"),
|
| 58 |
outputs=gr.Textbox(label="Detected Weight"),
|
| 59 |
title="Auto Weight Logger (Blur & Distance Aware)",
|
| 60 |
+
description="Extracts weight from distant or blurry display images using OCR + preprocessing."
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|