Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,63 +8,74 @@ def safe_load_yolo_model(path):
|
|
8 |
torch.serialization.add_safe_globals([torch, 'ultralytics.nn.tasks.DetectionModel'])
|
9 |
return YOLO(path)
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
cap = cv2.VideoCapture(video)
|
18 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
19 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
20 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
while cap.isOpened():
|
27 |
ret, frame = cap.read()
|
28 |
if not ret:
|
29 |
break
|
30 |
-
|
31 |
-
# Detect with YOLOv11 (general object detection model)
|
32 |
-
results_yolo11 = model_yolo11(frame)
|
33 |
-
# Detect with best.pt (specialized model for cracks and potholes)
|
34 |
-
results_best = model_best(frame)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
boxes = result.boxes
|
39 |
-
for box in boxes:
|
40 |
-
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
41 |
-
class_id = int(box.cls[0]) # Class index for YOLO
|
42 |
-
# Use model's built-in class names
|
43 |
-
label = f"{model_yolo11.names[class_id]} - {box.conf[0]:.2f}"
|
44 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
45 |
-
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 2)
|
56 |
-
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
|
57 |
|
58 |
-
# Write the processed frame to the output video
|
59 |
out.write(frame)
|
60 |
|
61 |
cap.release()
|
62 |
out.release()
|
63 |
-
|
64 |
return 'output_video.mp4'
|
65 |
|
66 |
-
# Gradio
|
67 |
-
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
-
# Launch the app
|
70 |
iface.launch()
|
|
|
8 |
torch.serialization.add_safe_globals([torch, 'ultralytics.nn.tasks.DetectionModel'])
|
9 |
return YOLO(path)
|
10 |
|
11 |
+
# Dictionary of model paths
|
12 |
+
model_paths = {
|
13 |
+
'YOLOv11': './data/yolo11n.pt',
|
14 |
+
'Crack & Pothole Detector': './data/best2.pt',
|
15 |
+
'Bridge Inspector': './data/bridge.pt',
|
16 |
+
'Road Surface Detector': './data/road.pt',
|
17 |
+
'Pipe Detector': './data/pipe.pt'
|
18 |
+
}
|
19 |
|
20 |
+
# Load models into memory
|
21 |
+
models = {name: safe_load_yolo_model(path) for name, path in model_paths.items()}
|
22 |
+
|
23 |
+
# Assign colors for each model
|
24 |
+
model_colors = {
|
25 |
+
'YOLOv11': (0, 255, 0),
|
26 |
+
'Crack & Pothole Detector': (255, 0, 0),
|
27 |
+
'Bridge Inspector': (0, 0, 255),
|
28 |
+
'Road Surface Detector': (255, 255, 0),
|
29 |
+
'Pipe Detector': (255, 0, 255)
|
30 |
+
}
|
31 |
+
|
32 |
+
def process_video(video, selected_model):
|
33 |
cap = cv2.VideoCapture(video)
|
34 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
35 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
36 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
37 |
|
38 |
+
out = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_width, frame_height))
|
39 |
+
|
40 |
+
use_models = models if selected_model == 'All' else {selected_model: models[selected_model]}
|
41 |
|
42 |
while cap.isOpened():
|
43 |
ret, frame = cap.read()
|
44 |
if not ret:
|
45 |
break
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
for model_name, model in use_models.items():
|
48 |
+
results = model(frame)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
for result in results:
|
51 |
+
for box in result.boxes:
|
52 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
53 |
+
class_id = int(box.cls[0])
|
54 |
+
label = f"{model.names[class_id]} - {box.conf[0]:.2f}"
|
55 |
+
color = model_colors.get(model_name, (0, 255, 255))
|
56 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
|
57 |
+
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
|
|
|
|
|
58 |
|
|
|
59 |
out.write(frame)
|
60 |
|
61 |
cap.release()
|
62 |
out.release()
|
|
|
63 |
return 'output_video.mp4'
|
64 |
|
65 |
+
# Gradio Interface
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=process_video,
|
68 |
+
inputs=[
|
69 |
+
gr.Video(label="Upload a Video"),
|
70 |
+
gr.Dropdown(
|
71 |
+
choices=["All"] + list(model_paths.keys()),
|
72 |
+
label="Select Model(s)",
|
73 |
+
value="All"
|
74 |
+
)
|
75 |
+
],
|
76 |
+
outputs=gr.Video(label="Processed Output"),
|
77 |
+
live=False,
|
78 |
+
title="Multi-Model YOLOv8 Video Inference"
|
79 |
+
)
|
80 |
|
|
|
81 |
iface.launch()
|