Files
buryan-infra/ops/home/ocr400.py
T

58 lines
1.8 KiB
Python

import json
import os
import re
import subprocess
PDF = '/root/buryan-infra/ops/home/plans/house-plans-2024-06-25.pdf'
BIG = '/tmp/plans/plan400.png'
OUT = '/root/buryan-infra/ops/home/renders/input'
os.makedirs('/tmp/plans', exist_ok=True)
subprocess.run(['pdftoppm', '-r', '400', '-png', '-singlefile', PDF, '/tmp/plans/plan400'],
check=False, capture_output=True)
print('400dpi:', os.path.exists(BIG), os.path.getsize(BIG) if os.path.exists(BIG) else 0, 'байт')
subprocess.run(['tesseract', BIG, '/tmp/plan400', '-l', 'rus', '--psm', '11', 'tsv'],
check=False, capture_output=True)
words = []
for i, line in enumerate(open('/tmp/plan400.tsv', encoding='utf-8')):
if i == 0:
continue
p = line.rstrip('\n').split('\t')
if len(p) < 12:
continue
try:
left, top, w, h = int(p[6]), int(p[7]), int(p[8]), int(p[9])
except ValueError:
continue
text = (p[11] or '').strip()
conf = float(p[10]) if p[10] not in ('', '-1') else 0
if text:
words.append((text, left, top, w, h, conf))
print('слов:', len(words))
# площади из экспликации → ищем те же числа на чертеже
AREAS = {
'bedroom': '11.45',
'office': '11.14',
'danina': '14.34',
'kitchen-living': '27.65',
'bathroom': '6.29',
'corridor': '6.83',
'laundry': '1.30',
'hall-way': '3.36',
}
hits = {}
for t, l, tp, w, h, c in words:
for key, area in AREAS.items():
if area in t.replace(',', '.'):
hits.setdefault(key, []).append((t, l, tp, w, h, round(c)))
print('\nнайдено по площадям:')
for k, v in hits.items():
print(' ', k, v[:3])
json.dump(hits, open('/tmp/plan_areas400.json', 'w'), ensure_ascii=False, indent=1)