Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,10 @@ import gradio as gr
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
|
6 |
-
# Load the YOLOv5 model
|
7 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.pt') # Adjust if needed
|
8 |
|
9 |
-
# Example function to calculate materials based on detected areas
|
10 |
def calculate_materials(detected_objects, image_width, image_height):
|
11 |
materials = {
|
12 |
"cement": 0,
|
@@ -40,8 +40,37 @@ def calculate_materials(detected_objects, image_width, image_height):
|
|
40 |
|
41 |
# Define the function for image inference
|
42 |
def predict_image(image):
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
|
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
|
6 |
+
# Load the YOLOv5 model (adjust the path to your model if needed)
|
7 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.pt') # Adjust if needed
|
8 |
|
9 |
+
# Example function to calculate materials based on detected areas
|
10 |
def calculate_materials(detected_objects, image_width, image_height):
|
11 |
materials = {
|
12 |
"cement": 0,
|
|
|
40 |
|
41 |
# Define the function for image inference
|
42 |
def predict_image(image):
|
43 |
+
# Run inference on the input image
|
44 |
+
results = model(image)
|
45 |
|
46 |
+
# Get the detected objects as pandas dataframe (xywh format)
|
47 |
+
detected_objects = results.pandas().xywh[0] # First image in batch
|
48 |
+
|
49 |
+
# Print out the detection results for debugging purposes
|
50 |
+
print(f"Detected objects: {detected_objects}")
|
51 |
+
|
52 |
+
# Assume blueprint image size (in cm, adjust based on real-world image size)
|
53 |
+
image_width = 91 # Example width in cm (adjust this)
|
54 |
+
image_height = 61 # Example height in cm (adjust this)
|
55 |
+
|
56 |
+
# Process the detected objects and calculate materials
|
57 |
+
detected_objects_list = []
|
58 |
+
for _, row in detected_objects.iterrows():
|
59 |
+
detected_objects_list.append({
|
60 |
+
'name': row['name'], # Detected object class name (e.g., 'wall', 'foundation')
|
61 |
+
'bbox': [row['xmin'], row['ymin'], row['xmax'], row['ymax']] # Bounding box coordinates
|
62 |
+
})
|
63 |
+
|
64 |
+
# Calculate materials based on detected objects
|
65 |
+
materials = calculate_materials(detected_objects_list, image_width, image_height)
|
66 |
+
|
67 |
+
# Return the materials as a dictionary
|
68 |
+
return materials
|
69 |
+
|
70 |
+
# Set up Gradio interface for image input and JSON output
|
71 |
+
interface = gr.Interface(fn=predict_image, inputs=gr.Image(), outputs=gr.JSON())
|
72 |
+
|
73 |
+
# Launch the Gradio interface
|
74 |
+
if __name__ == "__main__":
|
75 |
+
interface.launch()
|
76 |
|