Komal133 commited on
Commit
b5c7371
·
verified ·
1 Parent(s): 98e55ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -22
app.py CHANGED
@@ -1,16 +1,15 @@
1
- import os
2
- os.environ['YOLO_CONFIG_DIR'] = '/tmp'
3
-
4
  import gradio as gr
5
  from PIL import Image
6
  import torch
 
7
  import requests
8
  from io import BytesIO
9
- from ultralytics import YOLO
10
 
11
- # ✅ Updated path to your model file
12
- model = YOLO("best.pt")
 
13
 
 
14
  class_labels = ["crack", "spalling", "rust", "deformation"]
15
  severity_mapping = {
16
  "crack": "medium",
@@ -19,24 +18,43 @@ severity_mapping = {
19
  "deformation": "high"
20
  }
21
 
22
- def detect_defects(image):
23
- results = model(image)
24
- result_image = results[0].plot()
25
- predictions = results[0].boxes
26
 
 
 
 
 
 
 
 
 
 
27
  output = []
28
- for box in predictions:
29
- cls = int(box.cls[0])
30
- label = class_labels[cls]
31
- conf = float(box.conf[0])
32
- severity = severity_mapping.get(label, "low")
33
- output.append({
34
- "type": label,
35
- "confidence": round(conf, 2),
36
- "severity": severity
37
- })
38
-
39
- return Image.fromarray(result_image), output
 
 
 
 
 
 
 
 
 
 
40
 
41
  demo = gr.Interface(
42
  fn=detect_defects,
 
 
 
 
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
4
+ from torchvision import models, transforms
5
  import requests
6
  from io import BytesIO
 
7
 
8
+ # Load pre-trained Faster R-CNN model from torchvision
9
+ model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
10
+ model.eval()
11
 
12
+ # Define the class labels for your defect types
13
  class_labels = ["crack", "spalling", "rust", "deformation"]
14
  severity_mapping = {
15
  "crack": "medium",
 
18
  "deformation": "high"
19
  }
20
 
21
+ # Transform for image preprocessing (resizing, normalization)
22
+ transform = transforms.Compose([
23
+ transforms.ToTensor(),
24
+ ])
25
 
26
+ def detect_defects(image):
27
+ # Convert the image to a tensor and add a batch dimension
28
+ image_tensor = transform(image).unsqueeze(0)
29
+
30
+ # Perform detection
31
+ with torch.no_grad():
32
+ predictions = model(image_tensor)
33
+
34
+ # Extract the predictions
35
  output = []
36
+ result_image = image.copy()
37
+ draw = ImageDraw.Draw(result_image)
38
+
39
+ for element in range(len(predictions[0]['boxes'])):
40
+ box = predictions[0]['boxes'][element].tolist()
41
+ label = int(predictions[0]['labels'][element])
42
+ score = float(predictions[0]['scores'][element])
43
+
44
+ # For simplicity, let's assume the labels correspond to the `class_labels` in order
45
+ if label < len(class_labels):
46
+ defect_type = class_labels[label]
47
+ severity = severity_mapping.get(defect_type, "low")
48
+ output.append({
49
+ "type": defect_type,
50
+ "confidence": round(score, 2),
51
+ "severity": severity
52
+ })
53
+
54
+ # Draw the bounding box on the image (optional visualization)
55
+ draw.rectangle(box, outline="red", width=3)
56
+
57
+ return result_image, output
58
 
59
  demo = gr.Interface(
60
  fn=detect_defects,