Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,7 @@ def enhance_image(image):
|
|
| 28 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
| 29 |
sharpened = cv2.filter2D(image, -1, kernel)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
thresholded = cv2.adaptiveThreshold(sharpened, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 33 |
cv2.THRESH_BINARY, 11, 2)
|
| 34 |
|
|
@@ -40,7 +40,7 @@ def convert_to_rgb(image):
|
|
| 40 |
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 41 |
return image
|
| 42 |
|
| 43 |
-
# Detect Digital Meter Using YOLOv5
|
| 44 |
def detect_meter(image):
|
| 45 |
results = yolo_model(image)
|
| 46 |
detected_meters = []
|
|
@@ -58,7 +58,7 @@ def extract_text_easyocr(image):
|
|
| 58 |
text = " ".join(ocr_reader.readtext(image, detail=0))
|
| 59 |
return text
|
| 60 |
|
| 61 |
-
# Extract Text Using TrOCR
|
| 62 |
def extract_text_trocr(image):
|
| 63 |
image = convert_to_rgb(image) # Convert grayscale to RGB
|
| 64 |
image = Image.fromarray(image)
|
|
@@ -70,17 +70,17 @@ def extract_text_trocr(image):
|
|
| 70 |
# Extract Weight Using Regex
|
| 71 |
def extract_weight(text):
|
| 72 |
matches = re.findall(r'\d+\.\d+|\d+', text) # Extract numeric weight
|
| 73 |
-
return matches[0] if matches else
|
| 74 |
|
| 75 |
-
# Apply Statistical Filtering
|
| 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
|
| 82 |
|
| 83 |
-
# Full Processing Pipeline (
|
| 84 |
def process_image(image):
|
| 85 |
enhanced = enhance_image(image)
|
| 86 |
detected_meters = detect_meter(image)
|
|
@@ -93,12 +93,12 @@ def process_image(image):
|
|
| 93 |
weight_easyocr = extract_weight(text_easyocr)
|
| 94 |
weight_trocr = extract_weight(text_trocr)
|
| 95 |
|
| 96 |
-
final_weights = [weight_easyocr, weight_trocr]
|
| 97 |
-
final_weight = filter_weight_values(
|
| 98 |
-
|
| 99 |
-
# Handle failed detection cases
|
| 100 |
-
if
|
| 101 |
-
return "Try adjusting image clarity or detection thresholds"
|
| 102 |
|
| 103 |
return final_weight
|
| 104 |
|
|
|
|
| 28 |
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
| 29 |
sharpened = cv2.filter2D(image, -1, kernel)
|
| 30 |
|
| 31 |
+
# Adaptive thresholding
|
| 32 |
thresholded = cv2.adaptiveThreshold(sharpened, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 33 |
cv2.THRESH_BINARY, 11, 2)
|
| 34 |
|
|
|
|
| 40 |
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 41 |
return image
|
| 42 |
|
| 43 |
+
# Detect Digital Meter Using YOLOv5
|
| 44 |
def detect_meter(image):
|
| 45 |
results = yolo_model(image)
|
| 46 |
detected_meters = []
|
|
|
|
| 58 |
text = " ".join(ocr_reader.readtext(image, detail=0))
|
| 59 |
return text
|
| 60 |
|
| 61 |
+
# Extract Text Using TrOCR
|
| 62 |
def extract_text_trocr(image):
|
| 63 |
image = convert_to_rgb(image) # Convert grayscale to RGB
|
| 64 |
image = Image.fromarray(image)
|
|
|
|
| 70 |
# Extract Weight Using Regex
|
| 71 |
def extract_weight(text):
|
| 72 |
matches = re.findall(r'\d+\.\d+|\d+', text) # Extract numeric weight
|
| 73 |
+
return matches[0] if matches else None # Returns None if no weight detected
|
| 74 |
|
| 75 |
+
# Apply Statistical Filtering for Stability
|
| 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 None
|
| 82 |
|
| 83 |
+
# Full Processing Pipeline (Dynamic Feedback)
|
| 84 |
def process_image(image):
|
| 85 |
enhanced = enhance_image(image)
|
| 86 |
detected_meters = detect_meter(image)
|
|
|
|
| 93 |
weight_easyocr = extract_weight(text_easyocr)
|
| 94 |
weight_trocr = extract_weight(text_trocr)
|
| 95 |
|
| 96 |
+
final_weights = [w for w in [weight_easyocr, weight_trocr] if w]
|
| 97 |
+
final_weight = filter_weight_values(final_weights)
|
| 98 |
+
|
| 99 |
+
# Handle failed detection cases dynamically
|
| 100 |
+
if not final_weight:
|
| 101 |
+
return "Try adjusting image clarity or detection thresholds."
|
| 102 |
|
| 103 |
return final_weight
|
| 104 |
|