File size: 3,486 Bytes
8a50c3f
 
13b9afe
de8ea7d
 
 
 
e918e64
de8ea7d
e918e64
 
de8ea7d
8a50c3f
8219ccf
 
 
e918e64
8219ccf
 
 
 
 
 
 
8a50c3f
e918e64
 
 
 
8a50c3f
13b9afe
e918e64
de8ea7d
e918e64
 
 
 
 
 
 
 
 
 
 
de8ea7d
 
e918e64
de8ea7d
 
e918e64
 
de8ea7d
e918e64
 
 
 
de8ea7d
e918e64
 
 
 
de8ea7d
e918e64
65ba25a
de8ea7d
 
 
65ba25a
de8ea7d
 
e918e64
 
 
 
de8ea7d
 
 
e918e64
13b9afe
e918e64
8a50c3f
 
de8ea7d
e918e64
de8ea7d
 
e918e64
de8ea7d
e918e64
 
 
8a50c3f
 
 
e918e64
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
95
96
import cv2
import numpy as np
import gradio as gr
from pdf2image import convert_from_path
import os
import tempfile
import logging
from PIL import Image

# Set up logging
logging.basicConfig(filename='debug.log', level=logging.DEBUG)
logger = logging.getLogger(__name__)

def validate_poppler_path(poppler_path):
    if not poppler_path:
        return None
    poppler_path = poppler_path.replace('\\', '/')
    if not os.path.isdir(poppler_path):
        return None
    pdftoppm_path = os.path.join(poppler_path, "pdftoppm" + (".exe" if os.name == "nt" else ""))
    if not os.path.isfile(pdftoppm_path):
        return None
    return poppler_path

def calculate_materials_from_dimensions(wall_area, foundation_area):
    return {
        "cement": round(wall_area * 10 + foundation_area * 20, 2),
        "bricks": int(wall_area * 500 + foundation_area * 750),
        "steel": round(wall_area * 2 + foundation_area * 5, 2)
    }

def process_blueprint(uploaded_file, blueprint_width_m=27, blueprint_height_m=9.78, poppler_path=None):
    try:
        filename = uploaded_file.name
        poppler_path = validate_poppler_path(poppler_path)
        
        # Convert input to image
        if filename.endswith(".pdf"):
            images = convert_from_path(uploaded_file.name, poppler_path=poppler_path)
            if not images:
                return {"error": "No pages found in PDF"}
            image = np.array(images[0])
        else:
            image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), cv2.IMREAD_COLOR)

        if image is None:
            return {"error": "Failed to load image"}

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        edges = cv2.Canny(gray, 50, 150)
        lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=10)

        total_wall_length_pixels = sum(
            np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
            for line in lines for x1, y1, x2, y2 in [line[0]]
        ) if lines is not None else 0

        img_h, img_w = image.shape[:2]
        pixel_to_meter_w = blueprint_width_m / img_w
        pixel_to_meter_h = blueprint_height_m / img_h
        avg_pixel_to_meter = (pixel_to_meter_w + pixel_to_meter_h) / 2

        total_wall_length_m = total_wall_length_pixels * avg_pixel_to_meter
        wall_height_m = 3
        wall_area = total_wall_length_m * wall_height_m

        total_area = blueprint_width_m * blueprint_height_m
        foundation_area = total_area * 0.1

        materials = calculate_materials_from_dimensions(wall_area, foundation_area)
        return {
            "cement": f"{materials['cement']} kg",
            "bricks": f"{materials['bricks']} units",
            "steel": f"{materials['steel']} kg"
        }

    except Exception as e:
        return {"error": f"Processing failed: {str(e)}"}

# Gradio UI
interface = gr.Interface(
    fn=process_blueprint,
    inputs=[
        gr.File(label="Upload Blueprint (PDF or Image)", file_types=[".pdf", ".png", ".jpg", ".jpeg"]),
        gr.Number(label="Blueprint Width (meters)", value=27),
        gr.Number(label="Blueprint Height (meters)", value=9.78),
        gr.Textbox(label="Poppler Path (Optional)", placeholder="e.g., C:/poppler/bin")
    ],
    outputs=gr.JSON(label="Estimated Materials"),
    title="Blueprint Estimator",
    description="Upload a blueprint as PDF or image to estimate materials (cement, bricks, steel)."
)

if __name__ == "__main__":
    interface.launch()