Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
| 12 |
-
model =
|
|
|
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
output = []
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|