Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -20,22 +20,22 @@ try:
|
|
20 |
except Exception as e:
|
21 |
logging.error(f"Tesseract not found or misconfigured: {str(e)}")
|
22 |
|
23 |
-
# Image Preprocessing function
|
24 |
def preprocess_image(img_cv):
|
25 |
"""Enhance the image to improve OCR performance."""
|
26 |
try:
|
|
|
27 |
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
# Applying Gaussian blur
|
34 |
blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
|
35 |
|
36 |
-
# Adaptive thresholding
|
37 |
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
|
38 |
-
|
39 |
# Sharpening the image
|
40 |
sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
|
41 |
|
@@ -54,7 +54,11 @@ def extract_weight(img):
|
|
54 |
|
55 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
56 |
processed_img = preprocess_image(img_cv)
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
# Tesseract configuration to extract digits and decimals
|
59 |
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
|
60 |
|
|
|
20 |
except Exception as e:
|
21 |
logging.error(f"Tesseract not found or misconfigured: {str(e)}")
|
22 |
|
23 |
+
# Improved Image Preprocessing function
|
24 |
def preprocess_image(img_cv):
|
25 |
"""Enhance the image to improve OCR performance."""
|
26 |
try:
|
27 |
+
# Convert to grayscale
|
28 |
gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
|
29 |
|
30 |
+
# Increase contrast
|
31 |
+
contrast = cv2.equalizeHist(gray)
|
32 |
+
|
33 |
+
# Apply Gaussian blur to reduce noise
|
|
|
34 |
blurred = cv2.GaussianBlur(contrast, (5, 5), 0)
|
35 |
|
36 |
+
# Adaptive thresholding for binarization
|
37 |
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
|
38 |
+
|
39 |
# Sharpening the image
|
40 |
sharpened = cv2.filter2D(thresh, -1, np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]))
|
41 |
|
|
|
54 |
|
55 |
img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
56 |
processed_img = preprocess_image(img_cv)
|
57 |
+
|
58 |
+
# Show processed image for debugging
|
59 |
+
debug_img = Image.fromarray(processed_img)
|
60 |
+
debug_img.show()
|
61 |
+
|
62 |
# Tesseract configuration to extract digits and decimals
|
63 |
custom_config = r'--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789.'
|
64 |
|