DeepDiveDev commited on
Commit
13a649f
·
verified ·
1 Parent(s): c504dbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -14,7 +14,13 @@ import numpy as np
14
  def preprocess_image(img):
15
  """Preprocess image to improve OCR accuracy for handwritten text"""
16
  # Convert to grayscale
17
- gray = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)
 
 
 
 
 
 
18
 
19
  # Apply thresholding
20
  _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
@@ -78,7 +84,7 @@ with gr.Blocks(title="Handwritten Text OCR Extractor") as app:
78
 
79
  with gr.Row():
80
  with gr.Column():
81
- file_input = gr.File(label="Upload Image or PDF", file_types=["image", "pdf"])
82
  extract_button = gr.Button("Extract Text")
83
 
84
  with gr.Column():
 
14
  def preprocess_image(img):
15
  """Preprocess image to improve OCR accuracy for handwritten text"""
16
  # Convert to grayscale
17
+ img_array = np.array(img)
18
+
19
+ # Check if the image is already grayscale
20
+ if len(img_array.shape) == 2:
21
+ gray = img_array
22
+ else:
23
+ gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
24
 
25
  # Apply thresholding
26
  _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 
84
 
85
  with gr.Row():
86
  with gr.Column():
87
+ file_input = gr.File(label="Upload Image or PDF")
88
  extract_button = gr.Button("Extract Text")
89
 
90
  with gr.Column():