import fitz # PyMuPDF from PIL import Image import os import convert_from_path from itertools import islice from collections import namedtuple import pytesseract import argparse import imutils import cv2 import shutil import os import gradio as gr def cleanup_text(text): return "".join([c if ord(c) < 128 else "" for c in text]).strip() def detectarCatastro(pdf): if pdf.endswith(".pdf"): images = [] pdf = fitz.open("pdf",pdf) # Iterar sobre cada página del PDF for page_num in range(len(pdf)): page = pdf.load_page(page_num) pix = page.get_pixmap() images.append(pix) aligned_images = [] template = cv2.imread('alignImage1.png') aligned_image = align_images(images[0], template, debug=True) aligned_images.append(aligned_image) template = cv2.imread('alignImage2.png') aligned_image = align_images(images[1], template, debug=True) aligned_images.append(aligned_image) filtered_image = cv2.bilateralFilter(aligned_images[0], 9, 75, 75) alignedImage = filtered_image alignedImage = cv2.resize(alignedImage, None, fx=1, fy=1, interpolation=cv2.INTER_LINEAR) OCRLocation = namedtuple("OCRLocation", ["id", "bbox", "filter_keywords"]) OCR_LOCATIONS = [ OCRLocation("Numero de la parcela", (385, 33, 225, 20), ["numero", "de", "la", "parcela"]), ] mostrar = "Numero de la parcela: " for loc in OCR_LOCATIONS: (x, y, w, h) = loc.bbox roi = alignedImage[y:y + h, x:x + w] rgb = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) text = pytesseract.image_to_string(rgb) mostrar = mostrar + text + " | Cultivos: " filtered_image = cv2.bilateralFilter(aligned_images[1], 9, 75, 75) alignedImage = filtered_image alignedImage = cv2.resize(alignedImage, None, fx=1, fy=1, interpolation=cv2.INTER_LINEAR) OCR_LOCATIONS = [ OCRLocation("Cultivos", (75, 58, 180, 190), ["cultivos", "y", "aprovechamientos"]), ] for loc in OCR_LOCATIONS: (x, y, w, h) = loc.bbox roi = alignedImage[y:y + h, x:x + w] rgb = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB) text = pytesseract.image_to_string(rgb) mostrar = mostrar + text return mostrar pdf = gr.File(label="Input PDF", value="ejemplo.pdf") method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR"],value="PaddleOCR") output = gr.Textbox(label="Output") demo = gr.Interface( detectarCatastro, [pdf], output, title="DetectorCatastro", css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}", article = """
Feel free to give us your thoughts on this demo and please contact us at letstalk@pragnakalp.com
Developed by: Pragnakalp Techlabs
""" ) demo.launch()