Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
|
|
| 1 |
from PIL import Image, ImageDraw
|
| 2 |
import torch
|
| 3 |
from torchvision import models, transforms
|
| 4 |
|
| 5 |
-
#
|
| 6 |
model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
|
| 7 |
model.eval()
|
| 8 |
|
| 9 |
-
#
|
| 10 |
COCO_INSTANCE_CATEGORY_NAMES = [
|
| 11 |
'__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
|
| 12 |
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter',
|
|
@@ -23,12 +24,11 @@ COCO_INSTANCE_CATEGORY_NAMES = [
|
|
| 23 |
|
| 24 |
# Image transformations
|
| 25 |
transform = transforms.Compose([
|
| 26 |
-
transforms.Resize((800, 800)), # Resize to 800x800 for faster processing
|
| 27 |
transforms.ToTensor(),
|
| 28 |
])
|
| 29 |
|
|
|
|
| 30 |
def get_severity(score):
|
| 31 |
-
"""Assign severity based on confidence score."""
|
| 32 |
if score >= 0.9:
|
| 33 |
return "High"
|
| 34 |
elif score >= 0.7:
|
|
@@ -37,7 +37,6 @@ def get_severity(score):
|
|
| 37 |
return "Low"
|
| 38 |
|
| 39 |
def detect_defects(image):
|
| 40 |
-
# Resize and transform the image
|
| 41 |
image_tensor = transform(image).unsqueeze(0)
|
| 42 |
|
| 43 |
with torch.no_grad():
|
|
@@ -49,7 +48,7 @@ def detect_defects(image):
|
|
| 49 |
output = []
|
| 50 |
for i in range(len(predictions[0]['boxes'])):
|
| 51 |
score = predictions[0]['scores'][i].item()
|
| 52 |
-
if score < 0.7: #
|
| 53 |
continue
|
| 54 |
|
| 55 |
box = predictions[0]['boxes'][i].tolist()
|
|
@@ -64,8 +63,22 @@ def detect_defects(image):
|
|
| 64 |
"severity": severity
|
| 65 |
})
|
| 66 |
|
| 67 |
-
# Draw bounding box and label
|
| 68 |
draw.rectangle(box, outline="red", width=3)
|
| 69 |
draw.text((box[0], box[1]), f"{label}: {severity}", fill="red")
|
| 70 |
|
| 71 |
return result_image, output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from PIL import Image, ImageDraw
|
| 3 |
import torch
|
| 4 |
from torchvision import models, transforms
|
| 5 |
|
| 6 |
+
# ✅ Directly load a pretrained model (you would need to fine-tune this model on structural defects)
|
| 7 |
model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
|
| 8 |
model.eval()
|
| 9 |
|
| 10 |
+
# Define labels (this could be adjusted if you have specific defect labels)
|
| 11 |
COCO_INSTANCE_CATEGORY_NAMES = [
|
| 12 |
'__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
|
| 13 |
'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter',
|
|
|
|
| 24 |
|
| 25 |
# Image transformations
|
| 26 |
transform = transforms.Compose([
|
|
|
|
| 27 |
transforms.ToTensor(),
|
| 28 |
])
|
| 29 |
|
| 30 |
+
# Default severity labels (can be customized based on detection confidence)
|
| 31 |
def get_severity(score):
|
|
|
|
| 32 |
if score >= 0.9:
|
| 33 |
return "High"
|
| 34 |
elif score >= 0.7:
|
|
|
|
| 37 |
return "Low"
|
| 38 |
|
| 39 |
def detect_defects(image):
|
|
|
|
| 40 |
image_tensor = transform(image).unsqueeze(0)
|
| 41 |
|
| 42 |
with torch.no_grad():
|
|
|
|
| 48 |
output = []
|
| 49 |
for i in range(len(predictions[0]['boxes'])):
|
| 50 |
score = predictions[0]['scores'][i].item()
|
| 51 |
+
if score < 0.7: # filter low-confidence predictions
|
| 52 |
continue
|
| 53 |
|
| 54 |
box = predictions[0]['boxes'][i].tolist()
|
|
|
|
| 63 |
"severity": severity
|
| 64 |
})
|
| 65 |
|
|
|
|
| 66 |
draw.rectangle(box, outline="red", width=3)
|
| 67 |
draw.text((box[0], box[1]), f"{label}: {severity}", fill="red")
|
| 68 |
|
| 69 |
return result_image, output
|
| 70 |
+
|
| 71 |
+
# Gradio Interface
|
| 72 |
+
demo = gr.Interface(
|
| 73 |
+
fn=detect_defects,
|
| 74 |
+
inputs=gr.Image(type="pil", label="Upload Drone Image"),
|
| 75 |
+
outputs=[
|
| 76 |
+
gr.Image(label="Detection Result"),
|
| 77 |
+
gr.JSON(label="Detected Faults with Severity")
|
| 78 |
+
],
|
| 79 |
+
title="Structural Defect Detection (Demo)",
|
| 80 |
+
description="Detects general objects using Faster R-CNN pretrained on COCO. This model needs fine-tuning for structural defects like cracks, rust, and spalling."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
demo.launch()
|