Spaces:
Sleeping
Sleeping
Update services/under_construction/earthwork_detection.py
Browse files
services/under_construction/earthwork_detection.py
CHANGED
|
@@ -2,25 +2,36 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def process_earthwork(frame):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
detections = []
|
| 23 |
-
line_counter = 1
|
| 24 |
|
| 25 |
for r in results:
|
| 26 |
for box in r.boxes:
|
|
@@ -29,12 +40,11 @@ def process_earthwork(frame):
|
|
| 29 |
continue
|
| 30 |
cls = int(box.cls[0])
|
| 31 |
label = model.names[cls]
|
| 32 |
-
if label != "earthwork":
|
| 33 |
continue
|
| 34 |
-
xyxy = box.xyxy[0].cpu().numpy()
|
| 35 |
-
x_min, y_min, x_max, y_max =
|
| 36 |
|
| 37 |
-
# Add numbered label
|
| 38 |
detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
|
| 39 |
detections.append({
|
| 40 |
"type": label,
|
|
@@ -43,12 +53,12 @@ def process_earthwork(frame):
|
|
| 43 |
"coordinates": [x_min, y_min, x_max, y_max]
|
| 44 |
})
|
| 45 |
|
| 46 |
-
|
| 47 |
-
color = (255, 228, 181) # Moccasin for earthwork
|
| 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.
|
| 51 |
|
| 52 |
line_counter += 1
|
| 53 |
|
|
|
|
| 54 |
return detections, frame
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import os
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
filename="app.log",
|
| 9 |
+
level=logging.INFO,
|
| 10 |
+
format="%(asctime)s - %(levelname)s - %(message)s"
|
| 11 |
+
)
|
| 12 |
|
|
|
|
| 13 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 14 |
MODEL_PATH = os.path.abspath(os.path.join(BASE_DIR, "../../models/yolov8m.pt"))
|
| 15 |
+
try:
|
| 16 |
+
model = YOLO(MODEL_PATH)
|
| 17 |
+
logging.info("Loaded YOLOv8m model for earthwork detection.")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
logging.error(f"Failed to load YOLOv8m model: {str(e)}")
|
| 20 |
+
model = None
|
| 21 |
|
| 22 |
def process_earthwork(frame):
|
| 23 |
+
if model is None:
|
| 24 |
+
logging.error("YOLO model not loaded. Skipping earthwork detection.")
|
| 25 |
+
return [], frame
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
results = model(frame)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logging.error(f"Error during YOLO inference: {str(e)}")
|
| 31 |
+
return [], frame
|
| 32 |
|
| 33 |
detections = []
|
| 34 |
+
line_counter = 1
|
| 35 |
|
| 36 |
for r in results:
|
| 37 |
for box in r.boxes:
|
|
|
|
| 40 |
continue
|
| 41 |
cls = int(box.cls[0])
|
| 42 |
label = model.names[cls]
|
| 43 |
+
if label != "earthwork":
|
| 44 |
continue
|
| 45 |
+
xyxy = box.xyxy[0].cpu().numpy().astype(int)
|
| 46 |
+
x_min, y_min, x_max, y_max = xyxy
|
| 47 |
|
|
|
|
| 48 |
detection_label = f"Line {line_counter} - {label.capitalize()} (Conf: {conf:.2f})"
|
| 49 |
detections.append({
|
| 50 |
"type": label,
|
|
|
|
| 53 |
"coordinates": [x_min, y_min, x_max, y_max]
|
| 54 |
})
|
| 55 |
|
| 56 |
+
color = (255, 165, 0) # Orange as requested
|
|
|
|
| 57 |
cv2.rectangle(frame, (x_min, y_min), (x_max, y_max), color, 2)
|
| 58 |
cv2.putText(frame, detection_label, (x_min, y_min - 10),
|
| 59 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
|
| 60 |
|
| 61 |
line_counter += 1
|
| 62 |
|
| 63 |
+
logging.info(f"Detected {len(detections)} earthwork activities in under_construction.")
|
| 64 |
return detections, frame
|