Spaces:
Sleeping
Sleeping
File size: 5,354 Bytes
8801ece 0f22bc9 adc8bc0 8801ece 33f6dad 8801ece cc5baab 3c68373 7eb2ab6 8801ece 6d6aaec 8801ece 6d6aaec 8801ece f01a1cf 8801ece 4a23cb8 |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import fitz # PyMuPDF
from PIL import Image
import os
from itertools import islice
from collections import namedtuple
import pytesseract
import argparse
import imutils
import cv2
import shutil
import os
import numpy as np
import gradio as gr
def align_images(image, template, maxFeatures=500, keepPercent=0.2,debug=False):
# convert both the input image and template to grayscale
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
orb = cv2.ORB_create(maxFeatures)
(kpsA, descsA) = orb.detectAndCompute(imageGray, None)
(kpsB, descsB) = orb.detectAndCompute(templateGray, None)
# match the features
method = cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING
matcher = cv2.DescriptorMatcher_create(method)
matches = matcher.match(descsA, descsB, None)
matches = sorted(matches, key=lambda x:x.distance)
# keep only the top matches
keep = int(len(matches) * keepPercent)
matches = matches[:keep]
# check to see if we should visualize the matched keypoints
if debug:
matchedVis = cv2.drawMatches(image, kpsA, template, kpsB,matches, None)
matchedVis = imutils.resize(matchedVis, width=1000)
cv2.waitKey(0)
# allocate memory for the keypoints (x, y)-coordinates from the
# top matches -- we'll use these coordinates to compute our
# homography matrix
ptsA = np.zeros((len(matches), 2), dtype="float")
ptsB = np.zeros((len(matches), 2), dtype="float")
# loop over the top matches
for (i, m) in enumerate(matches):
# indicate that the two keypoints in the respective images
# map to each other
ptsA[i] = kpsA[m.queryIdx].pt
ptsB[i] = kpsB[m.trainIdx].pt
# compute the homography matrix between the two sets of matched
# points
(H, mask) = cv2.findHomography(ptsA, ptsB, method=cv2.RANSAC)
# use the homography matrix to align the images
(h, w) = template.shape[:2]
aligned = cv2.warpPerspective(image, H, (w, h))
# return the aligned image
return aligned
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)
# Iterar sobre cada página del PDF
for page_num in range(len(pdf)):
page = pdf.load_page(page_num)
pix = page.get_pixmap()
img_array = np.frombuffer(pix.samples, dtype=np.uint8)
if pix.alpha:
img_array = img_array.reshape((pix.height, pix.width, 4))
else:
img_array = img_array.reshape((pix.height, pix.width, 3))
images.append(img_array)
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 = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com/" target="_blank">Pragnakalp Techlabs</a></p>"""
)
demo.launch() |