Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import re
|
|
7 |
from ultralytics import YOLO
|
8 |
import easyocr
|
9 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
|
10 |
|
11 |
# Load models
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -19,12 +20,18 @@ ocr_reader = easyocr.Reader(["en"]) # EasyOCR
|
|
19 |
trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
20 |
trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1").to(device)
|
21 |
|
22 |
-
# Image Preprocessing (Sharpen & Threshold)
|
23 |
def enhance_image(image):
|
24 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
25 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
26 |
sharpened = cv2.filter2D(image, -1, kernel)
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
return thresholded
|
29 |
|
30 |
# Convert Grayscale to RGB (Fix for TrOCR)
|
@@ -41,7 +48,7 @@ def detect_meter(image):
|
|
41 |
for result in results:
|
42 |
if hasattr(result, "boxes"): # Ensure correct format
|
43 |
for box in result.boxes:
|
44 |
-
if box.conf > 0.
|
45 |
detected_meters.append(box.xyxy.tolist())
|
46 |
|
47 |
return detected_meters
|
@@ -65,6 +72,14 @@ def extract_weight(text):
|
|
65 |
matches = re.findall(r'\d+\.\d+|\d+', text) # Extract numeric weight
|
66 |
return matches[0] if matches else "Weight not detected"
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
# Full Processing Pipeline
|
69 |
def process_image(image):
|
70 |
enhanced = enhance_image(image)
|
@@ -76,7 +91,9 @@ def process_image(image):
|
|
76 |
weight_easyocr = extract_weight(text_easyocr)
|
77 |
weight_trocr = extract_weight(text_trocr)
|
78 |
|
79 |
-
|
|
|
|
|
80 |
return final_weight or "Weight not detected"
|
81 |
|
82 |
# Gradio Interface
|
|
|
7 |
from ultralytics import YOLO
|
8 |
import easyocr
|
9 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
10 |
+
from scipy.signal import medfilt
|
11 |
|
12 |
# Load models
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
20 |
trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-stage1")
|
21 |
trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-stage1").to(device)
|
22 |
|
23 |
+
# Image Preprocessing (Sharpen & Adaptive Threshold)
|
24 |
def enhance_image(image):
|
25 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
26 |
+
|
27 |
+
# Apply sharpening
|
28 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
29 |
sharpened = cv2.filter2D(image, -1, kernel)
|
30 |
+
|
31 |
+
# Apply adaptive thresholding for digital meter reading
|
32 |
+
thresholded = cv2.adaptiveThreshold(sharpened, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
33 |
+
cv2.THRESH_BINARY, 11, 2)
|
34 |
+
|
35 |
return thresholded
|
36 |
|
37 |
# Convert Grayscale to RGB (Fix for TrOCR)
|
|
|
48 |
for result in results:
|
49 |
if hasattr(result, "boxes"): # Ensure correct format
|
50 |
for box in result.boxes:
|
51 |
+
if box.conf > 0.3: # Lower confidence threshold for better detection
|
52 |
detected_meters.append(box.xyxy.tolist())
|
53 |
|
54 |
return detected_meters
|
|
|
72 |
matches = re.findall(r'\d+\.\d+|\d+', text) # Extract numeric weight
|
73 |
return matches[0] if matches else "Weight not detected"
|
74 |
|
75 |
+
# Apply Statistical Filtering (Median Filtering for Stable Readings)
|
76 |
+
def filter_weight_values(weights):
|
77 |
+
if len(weights) > 1:
|
78 |
+
weights = [float(w) for w in weights]
|
79 |
+
filtered_weight = medfilt(weights, kernel_size=3)[-1] # Smooth out variations
|
80 |
+
return str(round(filtered_weight, 2))
|
81 |
+
return weights[0] if weights else "Weight not detected"
|
82 |
+
|
83 |
# Full Processing Pipeline
|
84 |
def process_image(image):
|
85 |
enhanced = enhance_image(image)
|
|
|
91 |
weight_easyocr = extract_weight(text_easyocr)
|
92 |
weight_trocr = extract_weight(text_trocr)
|
93 |
|
94 |
+
final_weights = [weight_easyocr, weight_trocr]
|
95 |
+
final_weight = filter_weight_values([w for w in final_weights if w != "Weight not detected"])
|
96 |
+
|
97 |
return final_weight or "Weight not detected"
|
98 |
|
99 |
# Gradio Interface
|