Spaces:
Runtime error
Runtime error
Update services/crack_detection_service.py
Browse files- services/crack_detection_service.py +113 -14
services/crack_detection_service.py
CHANGED
|
@@ -9,21 +9,22 @@ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
| 9 |
MODEL_PATH = os.path.join(BASE_DIR, "../models/yolov8m-seg.pt")
|
| 10 |
model = YOLO(MODEL_PATH)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
-
Detect cracks
|
| 15 |
Args:
|
| 16 |
frame: Input frame (numpy array)
|
|
|
|
| 17 |
Returns:
|
| 18 |
-
list: List of detected
|
| 19 |
"""
|
| 20 |
-
# Run YOLOv8 inference
|
| 21 |
results = model(frame)
|
| 22 |
-
|
| 23 |
detected_items = []
|
| 24 |
line_counter = 1 # Initialize counter for numbered labels
|
| 25 |
|
| 26 |
-
# Process detections
|
| 27 |
for r in results:
|
| 28 |
for box in r.boxes:
|
| 29 |
conf = float(box.conf[0])
|
|
@@ -31,29 +32,127 @@ def detect_cracks_and_objects(frame):
|
|
| 31 |
continue
|
| 32 |
cls = int(box.cls[0])
|
| 33 |
label = model.names[cls]
|
| 34 |
-
if label
|
| 35 |
continue
|
| 36 |
xyxy = box.xyxy[0].cpu().numpy()
|
| 37 |
x_min, y_min, x_max, y_max = map(int, xyxy)
|
| 38 |
|
| 39 |
# Simulate severity for cracks
|
| 40 |
-
severity =
|
| 41 |
-
if label == "crack":
|
| 42 |
-
severity = random.choice(["low", "medium", "high"])
|
| 43 |
|
| 44 |
# Add numbered label
|
| 45 |
-
detection_label = f"Line {line_counter} -
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
item = {
|
| 47 |
"type": label,
|
| 48 |
"label": detection_label,
|
| 49 |
"confidence": conf,
|
| 50 |
"coordinates": [x_min, y_min, x_max, y_max]
|
| 51 |
}
|
| 52 |
-
if severity:
|
| 53 |
-
item["severity"] = severity
|
| 54 |
|
| 55 |
detected_items.append(item)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 57 |
line_counter += 1
|
| 58 |
|
| 59 |
-
return detected_items
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
MODEL_PATH = os.path.join(BASE_DIR, "../models/yolov8m-seg.pt")
|
| 10 |
model = YOLO(MODEL_PATH)
|
| 11 |
|
| 12 |
+
import random
|
| 13 |
+
|
| 14 |
+
def detect_cracks(frame, model):
|
| 15 |
"""
|
| 16 |
+
Detect cracks in a frame using YOLOv8.
|
| 17 |
Args:
|
| 18 |
frame: Input frame (numpy array)
|
| 19 |
+
model: YOLO model
|
| 20 |
Returns:
|
| 21 |
+
list: List of detected cracks with type, label, coordinates, confidence, and severity
|
| 22 |
"""
|
| 23 |
+
# Run YOLOv8 inference for cracks
|
| 24 |
results = model(frame)
|
|
|
|
| 25 |
detected_items = []
|
| 26 |
line_counter = 1 # Initialize counter for numbered labels
|
| 27 |
|
|
|
|
| 28 |
for r in results:
|
| 29 |
for box in r.boxes:
|
| 30 |
conf = float(box.conf[0])
|
|
|
|
| 32 |
continue
|
| 33 |
cls = int(box.cls[0])
|
| 34 |
label = model.names[cls]
|
| 35 |
+
if label != "crack": # Process only cracks
|
| 36 |
continue
|
| 37 |
xyxy = box.xyxy[0].cpu().numpy()
|
| 38 |
x_min, y_min, x_max, y_max = map(int, xyxy)
|
| 39 |
|
| 40 |
# Simulate severity for cracks
|
| 41 |
+
severity = random.choice(["low", "medium", "high"])
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Add numbered label
|
| 44 |
+
detection_label = f"Line {line_counter} - Crack (Conf: {conf:.2f})"
|
| 45 |
+
item = {
|
| 46 |
+
"type": label,
|
| 47 |
+
"label": detection_label,
|
| 48 |
+
"confidence": conf,
|
| 49 |
+
"coordinates": [x_min, y_min, x_max, y_max],
|
| 50 |
+
"severity": severity
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
detected_items.append(item)
|
| 54 |
+
line_counter += 1
|
| 55 |
+
|
| 56 |
+
return detected_items
|
| 57 |
+
|
| 58 |
+
def detect_potholes(frame, model):
|
| 59 |
+
"""
|
| 60 |
+
Detect potholes in a frame using YOLOv8.
|
| 61 |
+
Args:
|
| 62 |
+
frame: Input frame (numpy array)
|
| 63 |
+
model: YOLO model
|
| 64 |
+
Returns:
|
| 65 |
+
list: List of detected potholes with type, label, coordinates, and confidence
|
| 66 |
+
"""
|
| 67 |
+
# Run YOLOv8 inference for potholes
|
| 68 |
+
results = model(frame)
|
| 69 |
+
detected_items = []
|
| 70 |
+
line_counter = 1 # Initialize counter for numbered labels
|
| 71 |
+
|
| 72 |
+
for r in results:
|
| 73 |
+
for box in r.boxes:
|
| 74 |
+
conf = float(box.conf[0])
|
| 75 |
+
if conf < 0.5:
|
| 76 |
+
continue
|
| 77 |
+
cls = int(box.cls[0])
|
| 78 |
+
label = model.names[cls]
|
| 79 |
+
if label != "pothole": # Process only potholes
|
| 80 |
+
continue
|
| 81 |
+
xyxy = box.xyxy[0].cpu().numpy()
|
| 82 |
+
x_min, y_min, x_max, y_max = map(int, xyxy)
|
| 83 |
+
|
| 84 |
+
# Add numbered label
|
| 85 |
+
detection_label = f"Line {line_counter} - Pothole (Conf: {conf:.2f})"
|
| 86 |
item = {
|
| 87 |
"type": label,
|
| 88 |
"label": detection_label,
|
| 89 |
"confidence": conf,
|
| 90 |
"coordinates": [x_min, y_min, x_max, y_max]
|
| 91 |
}
|
|
|
|
|
|
|
| 92 |
|
| 93 |
detected_items.append(item)
|
| 94 |
+
line_counter += 1
|
| 95 |
+
|
| 96 |
+
return detected_items
|
| 97 |
+
|
| 98 |
+
def detect_objects(frame, model):
|
| 99 |
+
"""
|
| 100 |
+
Detect objects in a frame using YOLOv8.
|
| 101 |
+
Args:
|
| 102 |
+
frame: Input frame (numpy array)
|
| 103 |
+
model: YOLO model
|
| 104 |
+
Returns:
|
| 105 |
+
list: List of detected objects with type, label, coordinates, and confidence
|
| 106 |
+
"""
|
| 107 |
+
# Run YOLOv8 inference for other objects
|
| 108 |
+
results = model(frame)
|
| 109 |
+
detected_items = []
|
| 110 |
+
line_counter = 1 # Initialize counter for numbered labels
|
| 111 |
+
|
| 112 |
+
for r in results:
|
| 113 |
+
for box in r.boxes:
|
| 114 |
+
conf = float(box.conf[0])
|
| 115 |
+
if conf < 0.5:
|
| 116 |
+
continue
|
| 117 |
+
cls = int(box.cls[0])
|
| 118 |
+
label = model.names[cls]
|
| 119 |
+
if label != "object": # Process only objects
|
| 120 |
+
continue
|
| 121 |
+
xyxy = box.xyxy[0].cpu().numpy()
|
| 122 |
+
x_min, y_min, x_max, y_max = map(int, xyxy)
|
| 123 |
+
|
| 124 |
+
# Add numbered label
|
| 125 |
+
detection_label = f"Line {line_counter} - Object (Conf: {conf:.2f})"
|
| 126 |
+
item = {
|
| 127 |
+
"type": label,
|
| 128 |
+
"label": detection_label,
|
| 129 |
+
"confidence": conf,
|
| 130 |
+
"coordinates": [x_min, y_min, x_max, y_max]
|
| 131 |
+
}
|
| 132 |
|
| 133 |
+
detected_items.append(item)
|
| 134 |
line_counter += 1
|
| 135 |
|
| 136 |
+
return detected_items
|
| 137 |
+
|
| 138 |
+
def detect_items_in_sequence(frame, model):
|
| 139 |
+
"""
|
| 140 |
+
Run crack, pothole, and object detection sequentially.
|
| 141 |
+
Args:
|
| 142 |
+
frame: Input frame (numpy array)
|
| 143 |
+
model: YOLO model
|
| 144 |
+
Returns:
|
| 145 |
+
list: List of detected items (crack, pothole, object)
|
| 146 |
+
"""
|
| 147 |
+
detected_items = []
|
| 148 |
+
|
| 149 |
+
# Detect cracks first
|
| 150 |
+
detected_items.extend(detect_cracks(frame, model))
|
| 151 |
+
|
| 152 |
+
# Detect potholes second
|
| 153 |
+
detected_items.extend(detect_potholes(frame, model))
|
| 154 |
+
|
| 155 |
+
# Detect objects third
|
| 156 |
+
detected_items.extend(detect_objects(frame, model))
|
| 157 |
+
|
| 158 |
+
return detected_items
|