Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from PIL import Image, ImageDraw
|
| 3 |
import torch
|
| 4 |
-
from torchvision import
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
-
model =
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
-
#
|
| 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 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 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(
|