Kushalmanda commited on
Commit
acc4fa9
·
verified ·
1 Parent(s): cb4ad70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -5
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
- results = model(image) # Run inference on the input image
11
- results.show() # Optionally visualize the results (bounding boxes)
12
- return results.pandas().xywh # Return the results (bounding box coordinates)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Set up Gradio interface to allow image uploads and get predictions
15
- interface = gr.Interface(fn=predict_image, inputs=gr.Image(), outputs=gr.Dataframe())
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()