DeepDiveDev commited on
Commit
d010bf6
·
verified ·
1 Parent(s): a3df3f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -5
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import gradio as gr
2
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
  from PIL import Image
4
  import numpy as np
@@ -15,13 +15,13 @@ model2 = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwri
15
  # Function to extract text from handwritten images
16
  def extract_text(image):
17
  try:
18
- # Convert NumPy array to PIL Image if needed
19
  if isinstance(image, np.ndarray):
20
  if len(image.shape) == 2: # Grayscale (H, W) -> Convert to RGB
21
  image = np.stack([image] * 3, axis=-1)
22
  image = Image.fromarray(image)
23
- else:
24
- image = Image.open(image).convert("RGB") # Ensure RGB format
25
 
26
  # Maintain aspect ratio while resizing (better for OCR)
27
  image.thumbnail((800, 800))
@@ -45,7 +45,7 @@ def extract_text(image):
45
  # Gradio UI for OCR Extraction
46
  iface = gr.Interface(
47
  fn=extract_text,
48
- inputs=gr.Image(type="pil"),
49
  outputs="text",
50
  title="Handwritten OCR Extraction",
51
  description="Upload a handwritten image to extract text using AI OCR.",
 
1
+ import gradio as gr
2
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
  from PIL import Image
4
  import numpy as np
 
15
  # Function to extract text from handwritten images
16
  def extract_text(image):
17
  try:
18
+ # Ensure input is a PIL Image
19
  if isinstance(image, np.ndarray):
20
  if len(image.shape) == 2: # Grayscale (H, W) -> Convert to RGB
21
  image = np.stack([image] * 3, axis=-1)
22
  image = Image.fromarray(image)
23
+ elif isinstance(image, str): # If file path is given, open the image
24
+ image = Image.open(image).convert("RGB")
25
 
26
  # Maintain aspect ratio while resizing (better for OCR)
27
  image.thumbnail((800, 800))
 
45
  # Gradio UI for OCR Extraction
46
  iface = gr.Interface(
47
  fn=extract_text,
48
+ inputs=gr.Image(type="pil"), # Ensures input is a PIL image
49
  outputs="text",
50
  title="Handwritten OCR Extraction",
51
  description="Upload a handwritten image to extract text using AI OCR.",