Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,54 @@
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
|
|
4 |
|
5 |
# Load the YOLOv5 model from the uploaded file (e.g., 'yolov5s.pt')
|
6 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.pt') # Adjust the file name if needed
|
7 |
|
8 |
-
# Define the function for image inference
|
9 |
def predict_image(image):
|
10 |
-
|
11 |
-
results
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Set up Gradio interface to allow image uploads and get predictions
|
15 |
-
interface = gr.Interface(fn=predict_image, inputs=gr.Image(), outputs=
|
16 |
|
17 |
# Launch the interface
|
18 |
interface.launch()
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
|
6 |
# Load the YOLOv5 model from the uploaded file (e.g., 'yolov5s.pt')
|
7 |
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.pt') # Adjust the file name if needed
|
8 |
|
9 |
+
# Define the function for image inference and cement calculation
|
10 |
def predict_image(image):
|
11 |
+
# Run inference on the input image
|
12 |
+
results = model(image)
|
13 |
+
|
14 |
+
# Get the bounding boxes and class names
|
15 |
+
predictions = results.pandas().xywh
|
16 |
+
|
17 |
+
# Filter for bricks (assuming the model detects a "brick" class)
|
18 |
+
brick_class = 'brick' # Define the brick class label used in the YOLO model
|
19 |
+
brick_data = predictions[predictions['name'] == brick_class]
|
20 |
+
|
21 |
+
# If bricks are detected, calculate cement estimation
|
22 |
+
if len(brick_data) > 0:
|
23 |
+
# Predefined brick dimensions in cm (assuming a standard brick size of 20x10x5 cm)
|
24 |
+
brick_length = 20 # cm
|
25 |
+
brick_width = 10 # cm
|
26 |
+
brick_height = 5 # cm
|
27 |
+
|
28 |
+
# Assuming a cement-to-brick ratio (this depends on the specific mortar mixture used)
|
29 |
+
cement_per_brick = 0.01 # (in cubic meters, adjust this based on your own research or formula)
|
30 |
+
|
31 |
+
# Total bricks detected
|
32 |
+
total_bricks = len(brick_data)
|
33 |
+
|
34 |
+
# Calculate the volume of one brick in cubic meters (converted from cm)
|
35 |
+
brick_volume = (brick_length / 100) * (brick_width / 100) * (brick_height / 100)
|
36 |
+
|
37 |
+
# Calculate the total volume of all bricks in cubic meters
|
38 |
+
total_brick_volume = brick_volume * total_bricks
|
39 |
+
|
40 |
+
# Estimate the cement required in cubic meters based on the volume
|
41 |
+
total_cement = cement_per_brick * total_bricks
|
42 |
+
|
43 |
+
# Output the results
|
44 |
+
results_text = f"Detected {total_bricks} bricks.\nTotal Brick Volume: {total_brick_volume:.2f} m³\nEstimated Cement Needed: {total_cement:.2f} m³"
|
45 |
+
|
46 |
+
return results_text, results.show() # Optionally visualize the results (bounding boxes)
|
47 |
+
else:
|
48 |
+
return "No bricks detected in the image.", results.show()
|
49 |
|
50 |
# Set up Gradio interface to allow image uploads and get predictions
|
51 |
+
interface = gr.Interface(fn=predict_image, inputs=gr.Image(), outputs=["text", "image"])
|
52 |
|
53 |
# Launch the interface
|
54 |
interface.launch()
|