Abhisesh7 commited on
Commit
78732d8
·
verified ·
1 Parent(s): 49f76f1

Update image_ocr.py

Browse files
Files changed (1) hide show
  1. image_ocr.py +15 -3
image_ocr.py CHANGED
@@ -1,17 +1,29 @@
1
  import easyocr
2
  import logging
 
 
3
 
4
  # Set up logging to suppress unnecessary warnings
5
  logging.getLogger("easyocr").setLevel(logging.ERROR)
6
 
7
- def extract_text_from_image(image_path):
8
  """Extract text from an image using EasyOCR."""
9
  try:
10
  # Initialize EasyOCR reader (English language, CPU mode)
11
  reader = easyocr.Reader(['en'], gpu=False)
12
 
13
- # Read text from the image
14
- results = reader.readtext(image_path, detail=0, paragraph=True)
 
 
 
 
 
 
 
 
 
 
15
 
16
  # Combine the extracted text into a single string
17
  text = "\n".join(results)
 
1
  import easyocr
2
  import logging
3
+ import numpy as np
4
+ from PIL import Image
5
 
6
  # Set up logging to suppress unnecessary warnings
7
  logging.getLogger("easyocr").setLevel(logging.ERROR)
8
 
9
+ def extract_text_from_image(image_input):
10
  """Extract text from an image using EasyOCR."""
11
  try:
12
  # Initialize EasyOCR reader (English language, CPU mode)
13
  reader = easyocr.Reader(['en'], gpu=False)
14
 
15
+ # Handle different input types
16
+ if isinstance(image_input, str):
17
+ # Input is a file path
18
+ results = reader.readtext(image_input, detail=0, paragraph=True)
19
+ elif isinstance(image_input, (bytes, np.ndarray)):
20
+ # Input is bytes or numpy array
21
+ results = reader.readtext(image_input, detail=0, paragraph=True)
22
+ else:
23
+ # Try to open as an image using PIL and convert to numpy array
24
+ image = Image.open(image_input).convert('RGB')
25
+ image_np = np.array(image)
26
+ results = reader.readtext(image_np, detail=0, paragraph=True)
27
 
28
  # Combine the extracted text into a single string
29
  text = "\n".join(results)