Komal133 commited on
Commit
41d4208
·
verified ·
1 Parent(s): a0b3cd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -1,15 +1,15 @@
1
  import gradio as gr
2
- from PIL import Image, ImageDraw # Add ImageDraw import
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,42 +18,38 @@ severity_mapping = {
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(
 
1
  import gradio as gr
2
+ from PIL import Image, ImageDraw
3
  import torch
4
+ from torchvision import transforms
5
+ import torchvision
6
+ import io
7
 
8
+ # Load your custom-trained model (replace with actual path)
9
+ model = torch.load("model.pth", map_location=torch.device('cpu'))
10
  model.eval()
11
 
12
+ # Your actual defect classes
13
  class_labels = ["crack", "spalling", "rust", "deformation"]
14
  severity_mapping = {
15
  "crack": "medium",
 
18
  "deformation": "high"
19
  }
20
 
 
21
  transform = transforms.Compose([
22
  transforms.ToTensor(),
23
  ])
24
 
25
  def detect_defects(image):
 
26
  image_tensor = transform(image).unsqueeze(0)
27
+
 
28
  with torch.no_grad():
29
+ predictions = model(image_tensor)[0]
30
+
 
 
31
  result_image = image.copy()
32
  draw = ImageDraw.Draw(result_image)
33
+ output = []
34
+
35
+ for box, label, score in zip(predictions['boxes'], predictions['labels'], predictions['scores']):
36
+ score = score.item()
37
+ label = label.item()
38
 
39
+ if score > 0.3 and label < len(class_labels): # Filter by confidence and known classes
40
+ box = [int(x) for x in box.tolist()]
41
  defect_type = class_labels[label]
42
  severity = severity_mapping.get(defect_type, "low")
43
+
44
+ draw.rectangle(box, outline="red", width=3)
45
+ draw.text((box[0], box[1] - 10), f"{defect_type} ({score:.2f})", fill="red")
46
+
47
  output.append({
48
  "type": defect_type,
49
  "confidence": round(score, 2),
50
  "severity": severity
51
  })
52
 
 
 
 
53
  return result_image, output
54
 
55
  demo = gr.Interface(