Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,21 +6,37 @@ from PIL import Image
|
|
| 6 |
from gtts import gTTS
|
| 7 |
import os
|
| 8 |
|
|
|
|
| 9 |
def preprocess(image):
|
| 10 |
"""
|
| 11 |
-
Preprocess the image for OCR:
|
| 12 |
- Convert to grayscale
|
| 13 |
-
-
|
| 14 |
-
-
|
|
|
|
| 15 |
"""
|
| 16 |
img = np.array(image)
|
|
|
|
|
|
|
| 17 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return thresh
|
|
|
|
| 24 |
|
| 25 |
def extract_and_speak(image):
|
| 26 |
"""
|
|
|
|
| 6 |
from gtts import gTTS
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# --- REPLACE your old preprocess function with this ---
|
| 10 |
def preprocess(image):
|
| 11 |
"""
|
| 12 |
+
Preprocess the image for better OCR results:
|
| 13 |
- Convert to grayscale
|
| 14 |
+
- Sharpen edges
|
| 15 |
+
- Denoise
|
| 16 |
+
- Adaptive thresholding for better contrast
|
| 17 |
"""
|
| 18 |
img = np.array(image)
|
| 19 |
+
|
| 20 |
+
# Convert to grayscale
|
| 21 |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 22 |
+
|
| 23 |
+
# Sharpen image
|
| 24 |
+
kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
|
| 25 |
+
gray = cv2.filter2D(gray, -1, kernel)
|
| 26 |
+
|
| 27 |
+
# Denoise
|
| 28 |
+
gray = cv2.fastNlMeansDenoising(gray, None, 30, 7, 21)
|
| 29 |
+
|
| 30 |
+
# Adaptive threshold
|
| 31 |
+
thresh = cv2.adaptiveThreshold(
|
| 32 |
+
gray, 255,
|
| 33 |
+
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 34 |
+
cv2.THRESH_BINARY,
|
| 35 |
+
31, 10
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
return thresh
|
| 39 |
+
# --- End of preprocess function ---
|
| 40 |
|
| 41 |
def extract_and_speak(image):
|
| 42 |
"""
|