File size: 3,735 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
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
"""Test tiled Gemini detection on one tile from a residential floor plan."""
import os, io, json
from pathlib import Path
from PIL import Image
from dotenv import load_dotenv
import fitz, warnings
warnings.filterwarnings('ignore')
Image.MAX_IMAGE_PIXELS = None

load_dotenv(Path(__file__).parent.parent / '.env')
api_key = os.environ.get('GEMINI_API_KEY', '').strip().strip('"')

from google import genai as google_genai
from google.genai import types as genai_types

client = google_genai.Client(api_key=api_key)

# Render page 7 (typical 4th-5th floor) at 300 DPI
pdf = fitz.open(r'..\raw_pdfs\355 E 86th St Mechanical 100% CD Set_flattened.pdf')
page = pdf[6]  # page 7, 0-indexed
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()
W, H = img.size
print(f'Full image: {W}x{H}')

# Take a 3000x3000 tile from the center of the drawing area
# Drawing area is roughly left 85% of the page
tile_x = W // 5       # start around 20% from left
tile_y = H // 3       # start around 33% from top
tile_size = 3000

tile = img.crop((tile_x, tile_y, tile_x + tile_size, tile_y + tile_size))
tile.save('output/test_tile.png')
print(f'Tile: ({tile_x},{tile_y}) to ({tile_x+tile_size},{tile_y+tile_size}), size={tile.size}')

# Scale tile for API (2048px max)
factor = 2048 / tile_size
scaled = tile.resize((int(tile_size * factor), int(tile_size * factor)), Image.LANCZOS)
print(f'Scaled tile for API: {scaled.size}, text ~{17 * factor:.1f}px')

buf = io.BytesIO()
scaled.save(buf, format='PNG')
img_bytes = buf.getvalue()

prompt = (
    "You are analyzing a SECTION (tile/crop) of a mechanical HVAC floor plan blueprint.\n\n"
    "This is NOT the full page — it is a zoomed-in portion. Look carefully for small HVAC indoor unit symbols.\n\n"
    "WHAT TO LOOK FOR:\n"
    "- Small rectangular or square symbols in rooms (bedrooms, living areas)\n"
    "- Each has a tag label like 'AC-D', 'AC-F', 'AC-S' printed in small text next to or below it\n"
    "- The label may be written as 'AC-D', 'AC D', 'ACD' or similar\n"
    "- Units are typically small (few cm on the drawing) and scattered in rooms\n\n"
    "WHAT TO IGNORE:\n"
    "- Ductwork labels (like '16X8', 'SG-3', 'VD')\n"
    "- Any schedule table or equipment list\n"
    "- Labels that are clearly part of duct/pipe sizes\n\n"
    "Known tags to find: AC-C, AC-D, AC-E, AC-F, AC-H, AC-I, AC-O, AC-P, AC-Q, AC-S, AC-T, AC-U\n\n"
    "Return ONLY valid JSON:\n"
    '{"detections": [{"tag": "AC-D", "ymin": 120, "xmin": 340, "ymax": 180, "xmax": 400}, ...]}\n\n'
    "Coordinates are integers 0-1000 normalized to this tile's size.\n"
    'Return {"detections": []} if no AC tags found.'
)

response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents=[prompt, genai_types.Part.from_bytes(data=img_bytes, mime_type='image/png')],
    config=genai_types.GenerateContentConfig(max_output_tokens=8192, temperature=0.0),
)

raw = response.text.strip()
print('\nGemini response:')
print(raw[:2000])

# Parse and convert back to full-image coordinates
import re
raw_clean = re.sub(r'^```[a-z]*\n?', '', raw, flags=re.MULTILINE)
raw_clean = re.sub(r'```$', '', raw_clean.strip())
try:
    data = json.loads(raw_clean)
    dets = data.get('detections', [])
except:
    dets = []

print(f'\n{len(dets)} detections:')
for d in dets:
    # Convert 0-1000 normalized coords back to full-image pixels
    x = tile_x + int(d['xmin'] / 1000 * tile_size)
    y = tile_y + int(d['ymin'] / 1000 * tile_size)
    x2 = tile_x + int(d['xmax'] / 1000 * tile_size)
    y2 = tile_y + int(d['ymax'] / 1000 * tile_size)
    print(f"  {d['tag']}: ({x},{y}) to ({x2},{y2})")