File size: 1,328 Bytes
c104c8e
 
78732d8
 
c104c8e
 
 
 
78732d8
c104c8e
 
 
 
 
78732d8
 
 
 
 
 
 
 
 
 
 
 
c104c8e
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import easyocr
import logging
import numpy as np
from PIL import Image

# Set up logging to suppress unnecessary warnings
logging.getLogger("easyocr").setLevel(logging.ERROR)

def extract_text_from_image(image_input):
    """Extract text from an image using EasyOCR."""
    try:
        # Initialize EasyOCR reader (English language, CPU mode)
        reader = easyocr.Reader(['en'], gpu=False)
        
        # Handle different input types
        if isinstance(image_input, str):
            # Input is a file path
            results = reader.readtext(image_input, detail=0, paragraph=True)
        elif isinstance(image_input, (bytes, np.ndarray)):
            # Input is bytes or numpy array
            results = reader.readtext(image_input, detail=0, paragraph=True)
        else:
            # Try to open as an image using PIL and convert to numpy array
            image = Image.open(image_input).convert('RGB')
            image_np = np.array(image)
            results = reader.readtext(image_np, detail=0, paragraph=True)
        
        # Combine the extracted text into a single string
        text = "\n".join(results)
        print("Extracted text from image:\n", text)  # Debug: Print extracted text
        return text
    except Exception as e:
        return f"Error extracting text from image: {str(e)}"