Spaces:
Runtime error
Runtime error
Update app.py
Browse files5 unmodified lines67891011121314151617181920212223242526272829305 unmodified lines# Image Preprocessing Functiondef preprocess_image(image_path):# Read the image in grayscaleimage = 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 lostblurred_image = cv2.GaussianBlur(image, (3, 3), 0)# Apply adaptive thresholding to create a binary image# This will help in making the text/numbers crispcrisp_image = cv2.adaptiveThreshold(blurred_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 11, 2)# Return the preprocessed imagereturn crisp_image# Function to encode the image to base64def encode_image(image_file):5 unmodified lines67891011121314151617181920212223242526272829305 unmodified lines# Image Preprocessing Functiondef preprocess_image(image_path):# Convert the file to an OpenCV imagefile_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 grayscalegray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# Apply Gaussian blur to reduce noise and improve OCR accuracyblurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)# Adaptive Histogram Equalizationclahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))equalized_image = clahe.apply(blurred_image)# Return the preprocessed imagereturn image_path# Function to encode the image to base64def encode_image(image_file):
app.py+17-17