Spaces:
Paused
Paused
File size: 1,054 Bytes
dac3297 | 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 | import sys, warnings
warnings.filterwarnings('ignore')
sys.path.insert(0, '.')
import fitz, pytesseract, numpy as np, cv2
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
pdf = fitz.open(r'..\raw_pdfs\355 E 86th St Mechanical 100% CD Set_flattened.pdf')
page = pdf[2] # 1st floor
mat = fitz.Matrix(300/72, 300/72)
pix = page.get_pixmap(matrix=mat, alpha=False)
img = Image.frombytes('RGB', [pix.width, pix.height], pix.samples)
pdf.close()
print('Image size:', img.size)
gray = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
data = pytesseract.image_to_data(Image.fromarray(gray),
output_type=pytesseract.Output.DATAFRAME,
config='--oem 3 --psm 11')
data = data[data['text'].notna() & (data['conf'] > 30)]
import re
ac_hits = data[data['text'].str.contains(r'AC[-\s]?[A-U]', case=False, regex=True, na=False)]
print(f'Found {len(ac_hits)} AC-tag OCR hits:')
for _, row in ac_hits.iterrows():
print(f' "{row["text"]}" conf={row["conf"]:.0f} at ({row["left"]},{row["top"]})')
|