File size: 2,412 Bytes
ca75569
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cd8c51e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fae65d
 
cd8c51e
04cb7d1
 
 
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
import json
import logging
from utils.key_manager import KeyManager

logger = logging.getLogger(__name__)

# Prompt strictly focused on extracting height levels from Elevations and Sections
ELEVATION_PROMPT = """This is an Architectural Elevation or Section drawing of a UAE villa. Your ONLY task is to extract the vertical heights (levels) of the floors and the building. DO NOT attempt to read room dimensions or count items.

Look for level markers, elevation tags, or vertical dimension lines (e.g. FFL, SSL, Top of Parapet).
Extract the following heights in METRES. Do not guess. If you cannot clearly see the dimension for a specific floor, return null for that field.

Return ONLY this JSON (no markdown, no explanations):
{
  "gf_height_m": 4.0,           // Floor-to-floor height of Ground Floor (e.g. difference between 1st Floor FFL and Ground FFL)
  "f1_height_m": 4.0,           // Floor-to-floor height of First Floor
  "total_villa_height_m": 9.5,  // Total height from Ground Level to the absolute highest point (e.g. Top of Parapet)
  "parapet_height_m": 1.2,      // Height of the roof parapet wall
  "confidence": "high|medium|low",
  "notes": ""
}"""

async def extract_elevation_data(image_path: str, key_manager: KeyManager = None) -> dict:
    from workflow.step3_extract import _ask_ai_with_retry
    from pdf_engine.ocr_engine import extract_all_text, extract_dimensions
    import cv2
    
    # 1. OCR Extraction of physical dimensions
    img_cv = cv2.imread(image_path)
    if img_cv is not None:
        extracted_text = extract_all_text(img_cv, preprocess=True)
        dims = extract_dimensions(extracted_text)
        # Get unique dimension values
        dim_values = list(set([str(d['value_meters']) for d in dims]))
    else:
        dim_values = []
        
    prompt = ELEVATION_PROMPT
    if dim_values:
        ocr_string = ", ".join(dim_values)
        prompt += f"\n\nCRITICAL INSTRUCTION: I have mathematically extracted the following numeric dimensions (in meters) from the image using OCR: [{ocr_string}]. You MUST select the floor heights ONLY from these exact numbers. DO NOT hallucinate or invent any numbers that are not in this list."

    with open(image_path, "rb") as f:
        img_bytes = f.read()
    raw = await _ask_ai_with_retry(img_bytes, prompt, key_manager)
    import re
    raw = re.sub(r"```json|```", "", raw).strip()
    return json.loads(raw)