Kushalmanda commited on
Commit
13b9afe
·
verified ·
1 Parent(s): f45622f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -69
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
- # 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)
 
 
 
 
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)