Spaces:
Sleeping
Sleeping
Update services/crack_detection_service.py
Browse files
services/crack_detection_service.py
CHANGED
@@ -1,55 +1,68 @@
|
|
1 |
-
import importlib.util
|
2 |
-
import sys
|
3 |
-
from transformers import DetrImageProcessor, DetrForObjectDetection
|
4 |
-
import torch
|
5 |
-
from PIL import Image
|
6 |
import cv2
|
7 |
-
import
|
8 |
|
9 |
-
|
10 |
-
if not importlib.util.find_spec("timm"):
|
11 |
try:
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
raise RuntimeError("Failed to install timm. Please ensure it is included in requirements.txt and restart the runtime.")
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", force_download=True)
|
20 |
-
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", force_download=True)
|
21 |
-
except Exception as e:
|
22 |
-
raise RuntimeError(f"Failed to load DETR model: {str(e)}")
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
outputs = model(**inputs)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
|
33 |
items = []
|
34 |
-
for
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
else:
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
return items
|
55 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
+
import numpy as np
|
3 |
|
4 |
+
def detect_cracks_and_holes(frame):
|
|
|
5 |
try:
|
6 |
+
# Convert frame to grayscale
|
7 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
8 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
|
|
9 |
|
10 |
+
# Edge detection using Canny
|
11 |
+
edges = cv2.Canny(blurred, 50, 150)
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Morphological operations to enhance cracks and holes
|
14 |
+
kernel = np.ones((3, 3), np.uint8)
|
15 |
+
dilated = cv2.dilate(edges, kernel, iterations=1)
|
16 |
+
eroded = cv2.erode(dilated, kernel, iterations=1)
|
|
|
17 |
|
18 |
+
# Find contours
|
19 |
+
contours, _ = cv2.findContours(eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
20 |
|
21 |
items = []
|
22 |
+
for contour in contours:
|
23 |
+
# Filter small contours (noise)
|
24 |
+
area = cv2.contourArea(contour)
|
25 |
+
if area < 50:
|
26 |
+
continue
|
27 |
+
|
28 |
+
# Get bounding box
|
29 |
+
x, y, w, h = cv2.boundingRect(contour)
|
30 |
+
area = w * h
|
31 |
+
|
32 |
+
# Calculate aspect ratio and circularity
|
33 |
+
aspect_ratio = float(w) / h if h > 0 else 0
|
34 |
+
perimeter = cv2.arcLength(contour, True)
|
35 |
+
circularity = 4 * np.pi * area / (perimeter * perimeter) if perimeter > 0 else 0
|
36 |
+
|
37 |
+
# Classify as crack or hole
|
38 |
+
if 0.1 < aspect_ratio < 10 and circularity < 0.5: # Long, thin shapes are cracks
|
39 |
+
item_type = 'crack'
|
40 |
+
# Check if it's an underlying crack (longer and thinner)
|
41 |
+
if aspect_ratio > 5:
|
42 |
+
severity = 'Underlying'
|
43 |
+
elif area > 5000:
|
44 |
+
severity = 'Severe'
|
45 |
+
elif area > 1000:
|
46 |
+
severity = 'Moderate'
|
47 |
+
else:
|
48 |
+
severity = 'Minor'
|
49 |
+
elif circularity > 0.7: # Circular shapes are holes
|
50 |
+
item_type = 'hole'
|
51 |
+
if area > 5000:
|
52 |
+
severity = 'Severe'
|
53 |
+
elif area > 1000:
|
54 |
+
severity = 'Moderate'
|
55 |
else:
|
56 |
+
severity = 'Minor'
|
57 |
+
else:
|
58 |
+
continue # Skip objects that don't match crack or hole criteria
|
59 |
+
|
60 |
+
items.append({
|
61 |
+
'type': item_type,
|
62 |
+
'box': [x, y, x + w, y + h],
|
63 |
+
'severity': severity,
|
64 |
+
'confidence': 0.95 # Simulated confidence
|
65 |
+
})
|
66 |
|
67 |
return items
|
68 |
except Exception as e:
|