Spaces:
Runtime error
Runtime error
Update services/under_construction/culvert_check.py
Browse files
services/under_construction/culvert_check.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# services/under_construction/culvert_check.py
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
from ultralytics import YOLO
|
|
@@ -6,58 +5,50 @@ import os
|
|
| 6 |
|
| 7 |
# Load YOLOv8m model
|
| 8 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
-
MODEL_PATH = os.path.join(BASE_DIR, "../../models/yolov8m.pt")
|
| 10 |
model = YOLO(MODEL_PATH)
|
| 11 |
|
| 12 |
-
def
|
| 13 |
"""
|
| 14 |
-
Detect culverts in a frame
|
| 15 |
Args:
|
| 16 |
frame: Input frame (numpy array)
|
| 17 |
Returns:
|
| 18 |
-
|
| 19 |
-
numpy array: Annotated frame
|
| 20 |
"""
|
| 21 |
-
# Run YOLOv8 inference
|
| 22 |
results = model(frame)
|
| 23 |
|
| 24 |
detections = []
|
| 25 |
line_counter = 1 # Initialize counter for numbered labels
|
| 26 |
|
| 27 |
-
# Process detections
|
| 28 |
for r in results:
|
| 29 |
for box in r.boxes:
|
| 30 |
conf = float(box.conf[0])
|
| 31 |
-
if conf < 0.5:
|
| 32 |
continue
|
| 33 |
cls = int(box.cls[0])
|
| 34 |
label = model.names[cls]
|
| 35 |
-
if label != "culvert": # Assuming "culvert" class exists
|
| 36 |
continue
|
| 37 |
xyxy = box.xyxy[0].cpu().numpy()
|
| 38 |
x_min, y_min, x_max, y_max = map(int, xyxy)
|
| 39 |
|
| 40 |
# Add numbered label
|
| 41 |
-
detection_label = f"Line {line_counter} -
|
| 42 |
detections.append({
|
|
|
|
| 43 |
"label": detection_label,
|
| 44 |
"confidence": conf,
|
| 45 |
"coordinates": [x_min, y_min, x_max, y_max]
|
| 46 |
})
|
| 47 |
|
| 48 |
# Draw bounding box and label
|
| 49 |
-
color = (
|
| 50 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
| 51 |
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
| 52 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 53 |
|
| 54 |
line_counter += 1
|
| 55 |
|
| 56 |
-
return
|
| 57 |
-
|
| 58 |
-
def process_culverts(frame):
|
| 59 |
-
"""
|
| 60 |
-
Wrapper function for integration with app.py.
|
| 61 |
-
"""
|
| 62 |
-
result = detect_culverts(frame)
|
| 63 |
-
return result["detections"], result["frame"]
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
from ultralytics import YOLO
|
|
|
|
| 5 |
|
| 6 |
# Load YOLOv8m model
|
| 7 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
+
MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8m.pt"))
|
| 9 |
model = YOLO(MODEL_PATH)
|
| 10 |
|
| 11 |
+
def process_culverts(frame):
|
| 12 |
"""
|
| 13 |
+
Detect culverts in a frame.
|
| 14 |
Args:
|
| 15 |
frame: Input frame (numpy array)
|
| 16 |
Returns:
|
| 17 |
+
list: List of detected culverts
|
| 18 |
+
numpy array: Annotated frame with numbered labels
|
| 19 |
"""
|
|
|
|
| 20 |
results = model(frame)
|
| 21 |
|
| 22 |
detections = []
|
| 23 |
line_counter = 1 # Initialize counter for numbered labels
|
| 24 |
|
|
|
|
| 25 |
for r in results:
|
| 26 |
for box in r.boxes:
|
| 27 |
conf = float(box.conf[0])
|
| 28 |
+
if conf < 0.5:
|
| 29 |
continue
|
| 30 |
cls = int(box.cls[0])
|
| 31 |
label = model.names[cls]
|
| 32 |
+
if label != "culvert": # Assuming "culvert" class exists
|
| 33 |
continue
|
| 34 |
xyxy = box.xyxy[0].cpu().numpy()
|
| 35 |
x_min, y_min, x_max, y_max = map(int, xyxy)
|
| 36 |
|
| 37 |
# Add numbered label
|
| 38 |
+
detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
|
| 39 |
detections.append({
|
| 40 |
+
"type": label,
|
| 41 |
"label": detection_label,
|
| 42 |
"confidence": conf,
|
| 43 |
"coordinates": [x_min, y_min, x_max, y_max]
|
| 44 |
})
|
| 45 |
|
| 46 |
# Draw bounding box and label
|
| 47 |
+
color = (128, 128, 128) # Gray for culverts
|
| 48 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
| 49 |
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
| 50 |
cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 51 |
|
| 52 |
line_counter += 1
|
| 53 |
|
| 54 |
+
return detections, frame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|