Update model.py
Browse files
model.py
CHANGED
|
@@ -1,32 +1,28 @@
|
|
| 1 |
from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation
|
|
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
-
# Load model
|
| 8 |
processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 9 |
model = AutoModelForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
ROAD_LABELS = [0, 8] # adjust if needed based on actual mapping
|
| 13 |
|
| 14 |
def predict_defect(image: Image.Image):
|
| 15 |
original = np.array(image)
|
| 16 |
inputs = processor(images=image, return_tensors="pt")
|
| 17 |
with torch.no_grad():
|
| 18 |
outputs = model(**inputs)
|
| 19 |
-
|
| 20 |
logits = outputs.logits
|
| 21 |
segmentation = torch.argmax(logits.squeeze(), dim=0).detach().cpu().numpy()
|
| 22 |
-
|
| 23 |
-
# Resize mask to match original image size
|
| 24 |
resized_mask = cv2.resize(segmentation.astype(np.uint8), (original.shape[1], original.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
overlay = original.copy()
|
| 30 |
-
overlay[
|
| 31 |
-
|
| 32 |
-
return Image.fromarray(overlay)
|
|
|
|
| 1 |
from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation
|
| 2 |
+
from PIL import Image
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
|
|
|
| 6 |
|
|
|
|
| 7 |
processor = AutoImageProcessor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 8 |
model = AutoModelForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 9 |
|
| 10 |
+
ROAD_LABELS = [0, 8] # class indices to consider as road
|
|
|
|
| 11 |
|
| 12 |
def predict_defect(image: Image.Image):
|
| 13 |
original = np.array(image)
|
| 14 |
inputs = processor(images=image, return_tensors="pt")
|
| 15 |
with torch.no_grad():
|
| 16 |
outputs = model(**inputs)
|
|
|
|
| 17 |
logits = outputs.logits
|
| 18 |
segmentation = torch.argmax(logits.squeeze(), dim=0).detach().cpu().numpy()
|
|
|
|
|
|
|
| 19 |
resized_mask = cv2.resize(segmentation.astype(np.uint8), (original.shape[1], original.shape[0]), interpolation=cv2.INTER_NEAREST)
|
| 20 |
|
| 21 |
+
# Mark only suspicious areas (non-road) unless it's oversegmenting
|
| 22 |
+
defect_mask = ~np.isin(resized_mask, ROAD_LABELS)
|
| 23 |
+
if np.sum(defect_mask) / resized_mask.size > 0.4:
|
| 24 |
+
defect_mask[:] = False
|
| 25 |
|
| 26 |
overlay = original.copy()
|
| 27 |
+
overlay[defect_mask] = [255, 0, 0]
|
| 28 |
+
return Image.fromarray(overlay)
|
|
|