Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
+
from pdf2image import convert_from_path # Import the PDF-to-image conversion library
|
6 |
+
|
7 |
+
# Function to calculate materials based on blueprint dimensions
|
8 |
+
def calculate_materials_from_dimensions(wall_area, foundation_area):
|
9 |
+
materials = {
|
10 |
+
"cement": 0,
|
11 |
+
"bricks": 0,
|
12 |
+
"steel": 0
|
13 |
+
}
|
14 |
+
|
15 |
+
# Wall calculations (in m²)
|
16 |
+
if wall_area > 0:
|
17 |
+
materials['cement'] += wall_area * 10 # 10 kg cement per m² for walls
|
18 |
+
materials['bricks'] += wall_area * 500 # 500 bricks per m² for walls
|
19 |
+
materials['steel'] += wall_area * 2 # 2 kg steel per m² for walls
|
20 |
+
|
21 |
+
# Foundation calculations (in m²)
|
22 |
+
if foundation_area > 0:
|
23 |
+
materials['cement'] += foundation_area * 20 # 20 kg cement per m² for foundation
|
24 |
+
materials['bricks'] += foundation_area * 750 # 750 bricks per m² for foundation
|
25 |
+
materials['steel'] += foundation_area * 5 # 5 kg steel per m² for foundation
|
26 |
+
|
27 |
+
return materials
|
28 |
+
|
29 |
+
# Function to process the blueprint and extract dimensions
|
30 |
+
def process_blueprint(image_path):
|
31 |
+
image = None
|
32 |
+
try:
|
33 |
+
# Check if the uploaded file is a PDF
|
34 |
+
if image_path.lower().endswith(".pdf"):
|
35 |
+
try:
|
36 |
+
# Convert the PDF to an image (first page)
|
37 |
+
images = convert_from_path(image_path, first_page=1, last_page=1)
|
38 |
+
image = np.array(images[0]) # Get the first page
|
39 |
+
print("Successfully converted PDF to image.")
|
40 |
+
except Exception as e:
|
41 |
+
print(f"Error processing the PDF file: {e}")
|
42 |
+
raise ValueError("Error processing the PDF file. Please ensure the file is a valid blueprint PDF.")
|
43 |
+
else:
|
44 |
+
# Open the image file
|
45 |
+
image = cv2.imread(image_path)
|
46 |
+
print("Successfully loaded image.")
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
print(f"Error loading file: {e}")
|
50 |
+
raise ValueError("Error loading the image. Please try again with a valid image or PDF file.")
|
51 |
+
|
52 |
+
# Check if image is successfully loaded
|
53 |
+
if image is None:
|
54 |
+
raise ValueError("Could not load the image or PDF file.")
|
55 |
+
|
56 |
+
# Convert to grayscale for easier processing
|
57 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
58 |
+
|
59 |
+
# Apply edge detection to find lines (walls)
|
60 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
61 |
+
|
62 |
+
# Use Hough Transform to detect lines (walls)
|
63 |
+
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=50, maxLineGap=10)
|
64 |
+
|
65 |
+
# Calculate total wall length (in pixels)
|
66 |
+
total_wall_length_pixels = 0
|
67 |
+
if lines is not None:
|
68 |
+
for line in lines:
|
69 |
+
x1, y1, x2, y2 = line[0]
|
70 |
+
length = np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
|
71 |
+
total_wall_length_pixels += length
|
72 |
+
|
73 |
+
# From the blueprint, we know the dimensions (27 m × 9.78 m)
|
74 |
+
image_height, image_width = image.shape[:2]
|
75 |
+
blueprint_width_m = 27 # From the blueprint (27 m)
|
76 |
+
blueprint_height_m = 9.78 # From the blueprint (9.78 m)
|
77 |
+
|
78 |
+
# Calculate pixel-to-meter ratio
|
79 |
+
pixel_to_meter_width = blueprint_width_m / image_width
|
80 |
+
pixel_to_meter_height = blueprint_height_m / image_height
|
81 |
+
pixel_to_meter = (pixel_to_meter_width + pixel_to_meter_height) / 2 # Average for simplicity
|
82 |
+
|
83 |
+
# Convert wall length to meters
|
84 |
+
total_wall_length_m = total_wall_length_pixels * pixel_to_meter
|
85 |
+
|
86 |
+
# Estimate wall area (assume wall height of 3 m for simplicity)
|
87 |
+
wall_height_m = 3 # Standard room height
|
88 |
+
wall_area = total_wall_length_m * wall_height_m
|
89 |
+
|
90 |
+
# Estimate foundation area (based on the blueprint's total area)
|
91 |
+
total_area = blueprint_width_m * blueprint_height_m # 27 m × 9.78 m
|
92 |
+
foundation_area = total_area * 0.1 # Assume 10% of the total area is foundation
|
93 |
+
|
94 |
+
# Calculate materials
|
95 |
+
materials = calculate_materials_from_dimensions(wall_area, foundation_area)
|
96 |
+
|
97 |
+
# Format the output
|
98 |
+
formatted_materials = {
|
99 |
+
"cement": f"{materials['cement']:.2f} kg",
|
100 |
+
"bricks": f"{materials['bricks']:.0f} units",
|
101 |
+
"steel": f"{materials['steel']:.2f} kg"
|
102 |
+
}
|
103 |
+
|
104 |
+
return formatted_materials
|
105 |
+
|
106 |
+
# Set up Gradio interface
|
107 |
+
interface = gr.Interface(
|
108 |
+
fn=process_blueprint,
|
109 |
+
inputs=gr.File(label="Upload Blueprint (Image or PDF)", type="filepath"), # Support both image and PDF files
|
110 |
+
outputs=gr.JSON(label="Material Estimates"),
|
111 |
+
title="Blueprint Material Estimator",
|
112 |
+
description="Upload a blueprint image or PDF to estimate construction materials."
|
113 |
+
)
|
114 |
+
|
115 |
+
# Launch the interface
|
116 |
+
if __name__ == "__main__":
|
117 |
+
interface.launch(share=False)
|