ocr / app.py
GAS17's picture
Update app.py
01dfe28 verified
raw
history blame contribute delete
922 Bytes
import gradio as gr
from PIL import Image
import pytesseract
import fitz # PyMuPDF
def ocr_pdf(file):
# Abre el PDF
doc = fitz.open(file.name)
text = ""
for page_number in range(len(doc)):
# Extrae la página como imagen
page = doc.load_page(page_number)
pix = page.get_pixmap()
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
# Extrae texto de la imagen
text += pytesseract.image_to_string(img) + "\n"
return text
# Crea la interfaz de Gradio
iface = gr.Interface(
fn=ocr_pdf, # Función que procesa el PDF
inputs=gr.File(label="Sube tu archivo PDF"), # Tipo de entrada: archivo
outputs=gr.Textbox(label="Texto extraído"), # Tipo de salida: texto
title="OCR con Python Tesseract para PDF",
description="Sube un archivo PDF para extraer el texto usando Tesseract OCR."
)
# Ejecuta la interfaz
iface.launch()