Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,24 +3,29 @@ import torch
|
|
| 3 |
import cv2
|
| 4 |
from ultralytics import YOLO
|
| 5 |
|
| 6 |
-
#
|
| 7 |
def safe_load_yolo_model(path):
|
| 8 |
-
# Add necessary safe globals to allow the detection model class during loading
|
| 9 |
torch.serialization.add_safe_globals([torch, 'ultralytics.nn.tasks.DetectionModel'])
|
| 10 |
return YOLO(path)
|
| 11 |
|
| 12 |
-
# Load
|
| 13 |
model_yolo11 = safe_load_yolo_model('./data/yolo11n.pt')
|
| 14 |
model_best = safe_load_yolo_model('./data/best.pt')
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def process_video(video):
|
| 17 |
-
#
|
| 18 |
cap = cv2.VideoCapture(video)
|
| 19 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 20 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 21 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 22 |
|
| 23 |
-
# Create
|
| 24 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4
|
| 25 |
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))
|
| 26 |
|
|
@@ -29,27 +34,30 @@ def process_video(video):
|
|
| 29 |
if not ret:
|
| 30 |
break
|
| 31 |
|
| 32 |
-
#
|
| 33 |
results_yolo11 = model_yolo11(frame)
|
|
|
|
| 34 |
results_best = model_best(frame)
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
# For simplicity, we will overlay bounding boxes and labels from both models
|
| 38 |
for result in results_yolo11:
|
| 39 |
boxes = result.boxes
|
| 40 |
for box in boxes:
|
| 41 |
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
|
|
|
|
|
|
| 42 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 43 |
-
label
|
| 44 |
-
cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
| 45 |
|
|
|
|
| 46 |
for result in results_best:
|
| 47 |
boxes = result.boxes
|
| 48 |
for box in boxes:
|
| 49 |
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
|
|
|
|
|
|
| 50 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 51 |
-
label
|
| 52 |
-
cv2.putText(frame, label, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
| 53 |
|
| 54 |
# Write the processed frame to the output video
|
| 55 |
out.write(frame)
|
|
|
|
| 3 |
import cv2
|
| 4 |
from ultralytics import YOLO
|
| 5 |
|
| 6 |
+
# Load YOLO models
|
| 7 |
def safe_load_yolo_model(path):
|
|
|
|
| 8 |
torch.serialization.add_safe_globals([torch, 'ultralytics.nn.tasks.DetectionModel'])
|
| 9 |
return YOLO(path)
|
| 10 |
|
| 11 |
+
# Load the models
|
| 12 |
model_yolo11 = safe_load_yolo_model('./data/yolo11n.pt')
|
| 13 |
model_best = safe_load_yolo_model('./data/best.pt')
|
| 14 |
|
| 15 |
+
# Class names for YOLO model (replace with actual class names used in your YOLO model)
|
| 16 |
+
yolo_classes = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe']
|
| 17 |
+
|
| 18 |
+
# Class names for best.pt model (assumed classes for crack and pothole)
|
| 19 |
+
best_classes = ['Crack', 'Pothole']
|
| 20 |
+
|
| 21 |
def process_video(video):
|
| 22 |
+
# Open the video using OpenCV
|
| 23 |
cap = cv2.VideoCapture(video)
|
| 24 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 25 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 26 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 27 |
|
| 28 |
+
# Create VideoWriter to save output video
|
| 29 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Codec for .mp4
|
| 30 |
out = cv2.VideoWriter('output_video.mp4', fourcc, fps, (frame_width, frame_height))
|
| 31 |
|
|
|
|
| 34 |
if not ret:
|
| 35 |
break
|
| 36 |
|
| 37 |
+
# Detect with YOLOv11 (general object detection model)
|
| 38 |
results_yolo11 = model_yolo11(frame)
|
| 39 |
+
# Detect with best.pt (specialized model for cracks and potholes)
|
| 40 |
results_best = model_best(frame)
|
| 41 |
+
|
| 42 |
+
# Draw bounding boxes and labels for YOLOv11 (General Object Detection)
|
|
|
|
| 43 |
for result in results_yolo11:
|
| 44 |
boxes = result.boxes
|
| 45 |
for box in boxes:
|
| 46 |
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 47 |
+
class_id = int(box.cls[0]) # Class index for YOLO
|
| 48 |
+
label = f"YOLO: {yolo_classes[class_id]} - {box.conf[0]:.2f}" # Map class_id to class name
|
| 49 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 50 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
|
|
|
| 51 |
|
| 52 |
+
# Draw bounding boxes and labels for best.pt (Crack and Pothole detection)
|
| 53 |
for result in results_best:
|
| 54 |
boxes = result.boxes
|
| 55 |
for box in boxes:
|
| 56 |
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 57 |
+
class_id = int(box.cls[0]) # Class index for best.pt
|
| 58 |
+
label = f"Best: {best_classes[class_id]} - {box.conf[0]:.2f}" # Map class_id to specific labels
|
| 59 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
| 60 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
|
|
|
| 61 |
|
| 62 |
# Write the processed frame to the output video
|
| 63 |
out.write(frame)
|