Spaces:
Runtime error
Runtime error
Update app.py
Browse files5 unmodified lines67891011121314151617181920212223249101112131415161718192021222324252627272829305 unmodified lines
# Image Preprocessing Functiondef preprocess_image(image_path): # Read the image in grayscale image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # Resize the image if it is too large or too small; maintain the aspect ratio # Skip resizing if the image is already at a desirable size # Apply a mild Gaussian blur to reduce noise which can help in edge detection # Use a small kernel size to ensure details are not lost blurred_image = cv2.GaussianBlur(image, (3, 3), 0) # Apply adaptive thresholding to create a binary image # This will help in making the text/numbers crisp crisp_image = cv2.adaptiveThreshold( blurred_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2 ) # Convert the file to an OpenCV image file_bytes = np.asarray(bytearray(image_file.read()), dtype=np.uint8) image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
# Resize the image, if necessary # image = cv2.resize(image, (desired_width, desired_height))
# Convert to grayscale gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise and improve OCR accuracy blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
# Adaptive Histogram Equalization clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) equalized_image = clahe.apply(blurred_image) # Return the preprocessed image return crisp_image return image_path
# Function to encode the image to base64def encode_image(image_file):app.py+17-17