Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,89 +1,97 @@
|
|
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 |
-
#
|
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 |
-
|
|
|
32 |
if foundation_area > 0:
|
33 |
-
materials['cement'] += foundation_area * 20
|
34 |
-
materials['bricks'] += foundation_area * 750
|
35 |
-
materials['steel'] += foundation_area * 5
|
36 |
-
|
37 |
return materials
|
38 |
|
39 |
-
#
|
40 |
-
def process_blueprint(
|
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 |
interface = gr.Interface(
|
81 |
fn=process_blueprint,
|
82 |
-
inputs=gr.
|
83 |
outputs=gr.JSON(label="Material Estimates"),
|
84 |
title="Blueprint Material Estimator",
|
85 |
-
description="Upload a blueprint image
|
86 |
)
|
87 |
|
|
|
88 |
if __name__ == "__main__":
|
89 |
interface.launch(share=False)
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
import gradio as gr
|
5 |
|
6 |
+
# Function to calculate materials based on blueprint dimensions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def calculate_materials_from_dimensions(wall_area, foundation_area):
|
8 |
materials = {
|
9 |
"cement": 0,
|
10 |
"bricks": 0,
|
11 |
+
"steel": 0
|
|
|
12 |
}
|
13 |
+
|
14 |
+
# Wall calculations (in m²)
|
15 |
if wall_area > 0:
|
16 |
+
materials['cement'] += wall_area * 10 # 10 kg cement per m² for walls
|
17 |
+
materials['bricks'] += wall_area * 500 # 500 bricks per m² for walls
|
18 |
+
materials['steel'] += wall_area * 2 # 2 kg steel per m² for walls
|
19 |
+
|
20 |
+
# Foundation calculations (in m²)
|
21 |
if foundation_area > 0:
|
22 |
+
materials['cement'] += foundation_area * 20 # 20 kg cement per m² for foundation
|
23 |
+
materials['bricks'] += foundation_area * 750 # 750 bricks per m² for foundation
|
24 |
+
materials['steel'] += foundation_area * 5 # 5 kg steel per m² for foundation
|
25 |
+
|
26 |
return materials
|
27 |
|
28 |
+
# Function to process the blueprint and extract dimensions
|
29 |
+
def process_blueprint(image_path):
|
30 |
+
# Open the image
|
31 |
+
image = cv2.imread(image_path)
|
32 |
+
if image is None:
|
33 |
+
raise ValueError("Could not load the image")
|
34 |
+
|
35 |
+
# Convert to grayscale for easier processing
|
36 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
37 |
+
|
38 |
+
# Apply edge detection to find lines (walls)
|
39 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
40 |
+
|
41 |
+
# Use Hough Transform to detect lines (walls)
|
42 |
+
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=100, minLineLength=50, maxLineGap=10)
|
43 |
+
|
44 |
+
# Calculate total wall length (in pixels)
|
45 |
+
total_wall_length_pixels = 0
|
46 |
+
if lines is not None:
|
47 |
+
for line in lines:
|
48 |
+
x1, y1, x2, y2 = line[0]
|
49 |
+
length = np.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
|
50 |
+
total_wall_length_pixels += length
|
51 |
+
|
52 |
+
# From the blueprint, we know the dimensions (27 m × 9.78 m)
|
53 |
+
# We also need to estimate the pixel-to-meter scale
|
54 |
+
image_height, image_width = image.shape[:2]
|
55 |
+
blueprint_width_m = 27 # From the blueprint (27 m)
|
56 |
+
blueprint_height_m = 9.78 # From the blueprint (9.78 m)
|
57 |
+
|
58 |
+
# Calculate pixel-to-meter ratio
|
59 |
+
pixel_to_meter_width = blueprint_width_m / image_width
|
60 |
+
pixel_to_meter_height = blueprint_height_m / image_height
|
61 |
+
pixel_to_meter = (pixel_to_meter_width + pixel_to_meter_height) / 2 # Average for simplicity
|
62 |
+
|
63 |
+
# Convert wall length to meters
|
64 |
+
total_wall_length_m = total_wall_length_pixels * pixel_to_meter
|
65 |
+
|
66 |
+
# Estimate wall area (assume wall height of 3 m for simplicity)
|
67 |
+
wall_height_m = 3 # Standard room height
|
68 |
+
wall_area = total_wall_length_m * wall_height_m
|
69 |
+
|
70 |
+
# Estimate foundation area (based on the blueprint's total area)
|
71 |
+
total_area = blueprint_width_m * blueprint_height_m # 27 m × 9.78 m
|
72 |
+
foundation_area = total_area * 0.1 # Assume 10% of the total area is foundation
|
73 |
+
|
74 |
+
# Calculate materials
|
75 |
+
materials = calculate_materials_from_dimensions(wall_area, foundation_area)
|
76 |
+
|
77 |
+
# Format the output
|
78 |
+
formatted_materials = {
|
79 |
+
"cement": f"{materials['cement']:.2f} kg",
|
80 |
+
"bricks": f"{materials['bricks']:.0f} units",
|
81 |
+
"steel": f"{materials['steel']:.2f} kg"
|
82 |
+
}
|
83 |
+
|
84 |
+
return formatted_materials
|
85 |
+
|
86 |
+
# Set up Gradio interface
|
87 |
interface = gr.Interface(
|
88 |
fn=process_blueprint,
|
89 |
+
inputs=gr.Image(type="filepath", label="Upload Blueprint Image"),
|
90 |
outputs=gr.JSON(label="Material Estimates"),
|
91 |
title="Blueprint Material Estimator",
|
92 |
+
description="Upload a blueprint image to estimate construction materials."
|
93 |
)
|
94 |
|
95 |
+
# Launch the interface
|
96 |
if __name__ == "__main__":
|
97 |
interface.launch(share=False)
|