Spaces:
Paused
Paused
Download python_files/find_ac_ocr.py from Rachel7/hvac_fable: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/Rachel7/hvac_fable/resolve/main/python_files/find_ac_ocr.py
- Command line
-
hf download hf://spaces/Rachel7/hvac_fable/python_files/find_ac_ocr.py
-
curl -L -o find_ac_ocr.py https://huggingface.co/spaces/Rachel7/hvac_fable/resolve/main/python_files/find_ac_ocr.py
1.05 kB
| 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"]})') | |