File size: 1,459 Bytes
4f83acf
 
 
 
 
 
 
 
 
 
 
 
 
a6be34e
 
 
2095927
07bb533
4f83acf
84a88e9
2095927
a6be34e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cd8ad7
a6be34e
 
 
 
 
 
 
 
 
 
 
 
 
c8ad4de
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os

os.system('chmod 777 /tmp')
os.system('apt-get update -y')
os.system('apt-get install tesseract-ocr -y')
os.system('pip install -q pytesseract')

if __name__ == "__main__":

    print("YES")



from PIL import Image
import pytesseract
import gradio as gr
from io import BytesIO
import os



# OCR processing function
def extract_text_from_image(image):
    try:
        # Open the uploaded image from Gradio
        image = image.resize((300, 150))  # Resize as required
        
        # Tesseract custom config
        custom_config = r'-l eng --oem 3 --psm 6'
        
        # Extracting text using pytesseract
        text = pytesseract.image_to_string(image, config=custom_config)
        
        return text
    except Exception as e:
        return f"Error occurred: {e}"

# Load example image from local directory (make sure the path is correct)
example_image_path = "./sample.png"  # Replace with the actual path
example_image = Image.open(example_image_path)

# Gradio Interface
iface = gr.Interface(
    fn=extract_text_from_image,  # Function to process OCR
    inputs=gr.Image(type="pil", label="Upload Image"),  # User uploads an image file
    outputs="text",  # Output extracted text
    title="Image to Text OCR",
    description="Upload an image file to extract text using OCR. Use the example image for testing.",
    examples=[[example_image]]  # Preload the example image
)

# Launch the interface
iface.launch(debug=True)