BigRatz commited on
Commit
bbf3f50
·
1 Parent(s): 1dae88f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -6,25 +6,25 @@ from openai import OpenAI
6
 
7
  # Image Preprocessing Function
8
  def preprocess_image(image_path):
9
- # Read the image in grayscale
10
- image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
11
-
12
- # Resize the image if it is too large or too small; maintain the aspect ratio
13
- # Skip resizing if the image is already at a desirable size
14
-
15
- # Apply a mild Gaussian blur to reduce noise which can help in edge detection
16
- # Use a small kernel size to ensure details are not lost
17
- blurred_image = cv2.GaussianBlur(image, (3, 3), 0)
18
-
19
- # Apply adaptive thresholding to create a binary image
20
- # This will help in making the text/numbers crisp
21
- crisp_image = cv2.adaptiveThreshold(
22
- blurred_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
23
- cv2.THRESH_BINARY, 11, 2
24
- )
25
 
26
  # Return the preprocessed image
27
- return crisp_image
28
 
29
  # Function to encode the image to base64
30
  def encode_image(image_file):
 
6
 
7
  # Image Preprocessing Function
8
  def preprocess_image(image_path):
9
+ # Convert the file to an OpenCV image
10
+ file_bytes = np.asarray(bytearray(image_file.read()), dtype=np.uint8)
11
+ image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
12
+
13
+ # Resize the image, if necessary
14
+ # image = cv2.resize(image, (desired_width, desired_height))
15
+
16
+ # Convert to grayscale
17
+ gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
18
+
19
+ # Apply Gaussian blur to reduce noise and improve OCR accuracy
20
+ blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
21
+
22
+ # Adaptive Histogram Equalization
23
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
24
+ equalized_image = clahe.apply(blurred_image)
25
 
26
  # Return the preprocessed image
27
+ return image_path
28
 
29
  # Function to encode the image to base64
30
  def encode_image(image_file):