Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,26 @@
|
|
| 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 |
-
#
|
| 9 |
-
model =
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
transform = transforms.Compose([
|
| 22 |
transforms.ToTensor(),
|
|
@@ -26,29 +30,29 @@ def detect_defects(image):
|
|
| 26 |
image_tensor = transform(image).unsqueeze(0)
|
| 27 |
|
| 28 |
with torch.no_grad():
|
| 29 |
-
predictions = model(image_tensor)
|
| 30 |
|
| 31 |
result_image = image.copy()
|
| 32 |
draw = ImageDraw.Draw(result_image)
|
| 33 |
-
output = []
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
severity = severity_mapping.get(defect_type, "low")
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"confidence": round(score, 2),
|
| 50 |
-
"severity": severity
|
| 51 |
-
})
|
| 52 |
|
| 53 |
return result_image, output
|
| 54 |
|
|
@@ -59,8 +63,8 @@ demo = gr.Interface(
|
|
| 59 |
gr.Image(label="Detection Result"),
|
| 60 |
gr.JSON(label="Detected Faults with Severity")
|
| 61 |
],
|
| 62 |
-
title="Structural Defect Detection",
|
| 63 |
-
description="
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
|
|
|
| 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 (no model.pth needed)
|
| 7 |
+
model = models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
|
| 8 |
model.eval()
|
| 9 |
|
| 10 |
+
# You must define default labels (COCO classes)
|
| 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',
|
| 14 |
+
'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra',
|
| 15 |
+
'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis',
|
| 16 |
+
'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
|
| 17 |
+
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon',
|
| 18 |
+
'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
|
| 19 |
+
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet',
|
| 20 |
+
'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven',
|
| 21 |
+
'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
|
| 22 |
+
'hair drier', 'toothbrush'
|
| 23 |
+
]
|
| 24 |
|
| 25 |
transform = transforms.Compose([
|
| 26 |
transforms.ToTensor(),
|
|
|
|
| 30 |
image_tensor = transform(image).unsqueeze(0)
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
+
predictions = model(image_tensor)
|
| 34 |
|
| 35 |
result_image = image.copy()
|
| 36 |
draw = ImageDraw.Draw(result_image)
|
|
|
|
| 37 |
|
| 38 |
+
output = []
|
| 39 |
+
for i in range(len(predictions[0]['boxes'])):
|
| 40 |
+
score = predictions[0]['scores'][i].item()
|
| 41 |
+
if score < 0.7: # filter low-confidence predictions
|
| 42 |
+
continue
|
| 43 |
|
| 44 |
+
box = predictions[0]['boxes'][i].tolist()
|
| 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": "unknown"
|
| 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 |
|
|
|
|
| 63 |
gr.Image(label="Detection Result"),
|
| 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. Not specific to cracks or faults."
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|