Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,117 +1,89 @@
|
|
|
|
|
|
|
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
18 |
-
materials['bricks'] += wall_area * 500
|
19 |
-
materials['steel'] += wall_area * 2
|
20 |
-
|
21 |
-
# Foundation calculations (in m²)
|
22 |
if foundation_area > 0:
|
23 |
-
materials['cement'] += foundation_area * 20
|
24 |
-
materials['bricks'] += foundation_area * 750
|
25 |
-
materials['steel'] += foundation_area * 5
|
26 |
-
|
27 |
return materials
|
28 |
|
29 |
-
#
|
30 |
-
def process_blueprint(
|
31 |
-
image = None
|
32 |
try:
|
33 |
-
#
|
34 |
-
if
|
35 |
-
|
36 |
-
|
37 |
-
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
except Exception as e:
|
49 |
-
|
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 |
-
#
|
107 |
interface = gr.Interface(
|
108 |
fn=process_blueprint,
|
109 |
-
inputs=gr.File(label="Upload Blueprint (Image or PDF)", type="filepath"),
|
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)
|
|
|
1 |
+
import requests
|
2 |
+
import gradio as gr
|
3 |
+
from pdf2image import convert_from_path
|
4 |
import cv2
|
5 |
import numpy as np
|
|
|
|
|
|
|
6 |
|
7 |
+
# Hugging Face API Token (get this from your Hugging Face account)
|
8 |
+
HF_API_TOKEN = "your-hugging-face-api-token"
|
9 |
+
|
10 |
+
# Function to call Hugging Face Inference API for object detection (e.g., walls, beams)
|
11 |
+
def detect_regions(image):
|
12 |
+
api_url = "https://api-inference.huggingface.co/models/ultralytics/yolov8"
|
13 |
+
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
14 |
+
with open(image, "rb") as f:
|
15 |
+
data = f.read()
|
16 |
+
response = requests.post(api_url, headers=headers, data=data)
|
17 |
+
return response.json()
|
18 |
+
|
19 |
+
# Function to calculate materials (same as your original code)
|
20 |
def calculate_materials_from_dimensions(wall_area, foundation_area):
|
21 |
materials = {
|
22 |
"cement": 0,
|
23 |
"bricks": 0,
|
24 |
+
"steel": 0,
|
25 |
+
"sand": 0 # Added for your requirement
|
26 |
}
|
|
|
|
|
27 |
if wall_area > 0:
|
28 |
+
materials['cement'] += wall_area * 10
|
29 |
+
materials['bricks'] += wall_area * 500
|
30 |
+
materials['steel'] += wall_area * 2
|
31 |
+
materials['sand'] += wall_area * 5 # Assumption: 5 CFT per m²
|
|
|
32 |
if foundation_area > 0:
|
33 |
+
materials['cement'] += foundation_area * 20
|
34 |
+
materials['bricks'] += foundation_area * 750
|
35 |
+
materials['steel'] += foundation_area * 5
|
36 |
+
materials['sand'] += foundation_area * 10 # Assumption: 10 CFT per m²
|
37 |
return materials
|
38 |
|
39 |
+
# Main function to process the blueprint
|
40 |
+
def process_blueprint(file_path):
|
|
|
41 |
try:
|
42 |
+
# Convert PDF to image if needed
|
43 |
+
if file_path.lower().endswith(".pdf"):
|
44 |
+
images = convert_from_path(file_path, first_page=1, last_page=1)
|
45 |
+
image_path = "temp_image.jpg"
|
46 |
+
images[0].save(image_path, "JPEG")
|
|
|
|
|
|
|
|
|
|
|
47 |
else:
|
48 |
+
image_path = file_path
|
49 |
+
|
50 |
+
# Detect regions using Hugging Face API
|
51 |
+
regions = detect_regions(image_path)
|
52 |
+
# For simplicity, assume regions give us total wall length in pixels
|
53 |
+
total_wall_length_pixels = 2000 # Placeholder (replace with actual detection logic)
|
54 |
+
|
55 |
+
# Hardcoded dimensions (update with actual blueprint dimensions)
|
56 |
+
image = cv2.imread(image_path)
|
57 |
+
image_height, image_width = image.shape[:2]
|
58 |
+
blueprint_width_m = 27
|
59 |
+
blueprint_height_m = 9.78
|
60 |
+
pixel_to_meter = (blueprint_width_m / image_width + blueprint_height_m / image_height) / 2
|
61 |
+
total_wall_length_m = total_wall_length_pixels * pixel_to_meter
|
62 |
+
wall_area = total_wall_length_m * 3 # Assume wall height of 3m
|
63 |
+
total_area = blueprint_width_m * blueprint_height_m
|
64 |
+
foundation_area = total_area * 0.1
|
65 |
+
|
66 |
+
# Calculate materials
|
67 |
+
materials = calculate_materials_from_dimensions(wall_area, foundation_area)
|
68 |
+
|
69 |
+
# Format output
|
70 |
+
return {
|
71 |
+
"cement": f"{materials['cement']:.2f}",
|
72 |
+
"bricks": f"{materials['bricks']:.0f}",
|
73 |
+
"steel": f"{materials['steel']:.2f}",
|
74 |
+
"sand": f"{materials['sand']:.2f}"
|
75 |
+
}
|
76 |
except Exception as e:
|
77 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
+
# Gradio interface
|
80 |
interface = gr.Interface(
|
81 |
fn=process_blueprint,
|
82 |
+
inputs=gr.File(label="Upload Blueprint (Image or PDF)", type="filepath"),
|
83 |
outputs=gr.JSON(label="Material Estimates"),
|
84 |
title="Blueprint Material Estimator",
|
85 |
description="Upload a blueprint image or PDF to estimate construction materials."
|
86 |
)
|
87 |
|
|
|
88 |
if __name__ == "__main__":
|
89 |
+
interface.launch(share=False)
|