File size: 2,616 Bytes
717b6b1 ebe3e23 717b6b1 55cb34e 75183d4 717b6b1 ebe3e23 717b6b1 ebe3e23 717b6b1 75183d4 717b6b1 75183d4 ebe3e23 75183d4 717b6b1 c4e0fe3 717b6b1 75183d4 ebe3e23 75183d4 4db401f 97b9782 55cb34e 97b9782 c4e0fe3 717b6b1 75183d4 c4e0fe3 75183d4 1b106c9 75183d4 717b6b1 | 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | import gradio as gr
import pytesseract
from pdf2image import convert_from_path
from PIL import Image
import os
gr.set_static_paths(paths=["/home/user/app"])
# Function to perform OCR
def ocr(input_file, lang='fas'): # 'fas': Persian language (Farsi)
extracted_text = ""
# Check if the input file is a PDF or an image
if isinstance(input_file, str) and input_file.endswith('.pdf'): # Check if the file is a PDF
# Convert PDF to images
images = convert_from_path(input_file)
# Loop through each image and perform OCR
for page_number, image in enumerate(images):
text = pytesseract.image_to_string(image, lang=lang)
extracted_text += text
elif isinstance(input_file, Image.Image): # If the input is an image
text = pytesseract.image_to_string(input_file, lang=lang)
extracted_text = text
return extracted_text
def gradio_interface():
# Define Gradio inputs and outputs
input_type = gr.Radio(["PDF", "Image"], label="Choose Input Type", value="PDF")
file_input = gr.File(label="Upload PDF/Image")
language_input = gr.Dropdown(
label="Select OCR Language",
choices=[
("English", "eng"),
("Mandarin Chinese", "chi_sim"),
("Hindi", "hin"),
("Spanish", "spa"),
("French", "fra"),
("Standard Arabic", "ara"),
("Bengali", "ben"),
("Portuguese", "por"),
("Russian", "rus"),
("Urdu", "urd"),
("Persian (Farsi)", "fas")
],
value="fas" # Default to Persian
)
output_text = gr.Textbox(label="Extracted Text", interactive=False)
# Function to process the inputs and return the outputs
def process(input_type, file, lang):
if input_type == "PDF":
extracted_text = ocr(file.name, lang)
else:
image = Image.open(file.name)
extracted_text = ocr(image, lang)
filename = os.path.basename(file.name)
nuul, filename = os.path.split(filename)
with open(f'{filename}.txt','w',encoding='utf-8') as f:
f.write(extracted_text)
return [f'{filename}.txt']
# Create and launch Gradio interface
gr.Interface(
fn=process,
inputs=[input_type, file_input, language_input],
outputs=[gr.File()],
title="OCR (PDF/Image)",
description="Upload a PDF or Image, select the OCR language, and extract the text."
).launch()
# Call the function to create the interface
gradio_interface()
|