Sahar7888's picture
Update app.py
4f83acf verified
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)