Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,11 @@ from PIL import Image, ImageDraw
|
|
| 3 |
import torch
|
| 4 |
from torchvision import models, transforms
|
| 5 |
|
| 6 |
-
# ✅ Directly load a pretrained model (
|
| 7 |
model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
|
| 8 |
model.eval()
|
| 9 |
|
| 10 |
-
#
|
| 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',
|
|
@@ -22,10 +22,20 @@ COCO_INSTANCE_CATEGORY_NAMES = [
|
|
| 22 |
'hair drier', 'toothbrush'
|
| 23 |
]
|
| 24 |
|
|
|
|
| 25 |
transform = transforms.Compose([
|
| 26 |
transforms.ToTensor(),
|
| 27 |
])
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def detect_defects(image):
|
| 30 |
image_tensor = transform(image).unsqueeze(0)
|
| 31 |
|
|
@@ -45,17 +55,20 @@ def detect_defects(image):
|
|
| 45 |
label_idx = predictions[0]['labels'][i].item()
|
| 46 |
label = COCO_INSTANCE_CATEGORY_NAMES[label_idx]
|
| 47 |
|
|
|
|
|
|
|
| 48 |
output.append({
|
| 49 |
"type": label,
|
| 50 |
"confidence": round(score, 2),
|
| 51 |
-
"severity":
|
| 52 |
})
|
| 53 |
|
| 54 |
draw.rectangle(box, outline="red", width=3)
|
| 55 |
-
draw.text((box[0], box[1]), label, fill="red")
|
| 56 |
|
| 57 |
return result_image, output
|
| 58 |
|
|
|
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=detect_defects,
|
| 61 |
inputs=gr.Image(type="pil", label="Upload Drone Image"),
|
|
@@ -64,7 +77,7 @@ demo = gr.Interface(
|
|
| 64 |
gr.JSON(label="Detected Faults with Severity")
|
| 65 |
],
|
| 66 |
title="Structural Defect Detection (Demo)",
|
| 67 |
-
description="Detects general objects using Faster R-CNN pretrained on COCO.
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 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',
|
|
|
|
| 22 |
'hair drier', 'toothbrush'
|
| 23 |
]
|
| 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:
|
| 35 |
+
return "Medium"
|
| 36 |
+
else:
|
| 37 |
+
return "Low"
|
| 38 |
+
|
| 39 |
def detect_defects(image):
|
| 40 |
image_tensor = transform(image).unsqueeze(0)
|
| 41 |
|
|
|
|
| 55 |
label_idx = predictions[0]['labels'][i].item()
|
| 56 |
label = COCO_INSTANCE_CATEGORY_NAMES[label_idx]
|
| 57 |
|
| 58 |
+
severity = get_severity(score)
|
| 59 |
+
|
| 60 |
output.append({
|
| 61 |
"type": label,
|
| 62 |
"confidence": round(score, 2),
|
| 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"),
|
|
|
|
| 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__":
|