Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import torch
|
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
from ultralytics import YOLO, __version__ as ultralytics_version
|
|
|
6 |
|
7 |
# Debug: Check environment
|
8 |
print(f"Torch version: {torch.__version__}")
|
@@ -10,16 +11,20 @@ print(f"Gradio version: {gr.__version__}")
|
|
10 |
print(f"Ultralytics version: {ultralytics_version}")
|
11 |
print(f"CUDA available: {torch.cuda.is_available()}")
|
12 |
|
13 |
-
# Load
|
14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
15 |
print(f"Using device: {device}")
|
16 |
model = YOLO('./data/best.pt').to(device)
|
|
|
17 |
|
18 |
-
def process_video(video):
|
19 |
if video is None:
|
20 |
return "Error: No video uploaded"
|
21 |
|
22 |
-
#
|
|
|
|
|
|
|
23 |
cap = cv2.VideoCapture(video)
|
24 |
if not cap.isOpened():
|
25 |
return "Error: Could not open video file"
|
@@ -29,17 +34,20 @@ def process_video(video):
|
|
29 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
30 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
31 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
print(f"
|
36 |
|
37 |
# Set up video writer
|
38 |
output_path = "processed_output.mp4"
|
39 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
40 |
-
out = cv2.VideoWriter(output_path, fourcc, fps, (
|
41 |
|
42 |
frame_count = 0
|
|
|
43 |
|
44 |
while True:
|
45 |
ret, frame = cap.read()
|
@@ -47,31 +55,65 @@ def process_video(video):
|
|
47 |
break
|
48 |
|
49 |
frame_count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
print(f"Processing frame {frame_count}/{total_frames}")
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
|
55 |
-
# Run
|
56 |
-
results = model(frame)
|
57 |
annotated_frame = results[0].plot()
|
58 |
|
59 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
out.write(annotated_frame)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
# Release resources
|
63 |
cap.release()
|
64 |
out.release()
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
return output_path
|
67 |
|
68 |
# Gradio interface
|
69 |
iface = gr.Interface(
|
70 |
fn=process_video,
|
71 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
|
72 |
outputs=gr.Video(label="Processed Video"),
|
73 |
-
title="
|
74 |
-
description="Upload a
|
75 |
)
|
76 |
|
77 |
if __name__ == "__main__":
|
|
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
from ultralytics import YOLO, __version__ as ultralytics_version
|
6 |
+
import time
|
7 |
|
8 |
# Debug: Check environment
|
9 |
print(f"Torch version: {torch.__version__}")
|
|
|
11 |
print(f"Ultralytics version: {ultralytics_version}")
|
12 |
print(f"CUDA available: {torch.cuda.is_available()}")
|
13 |
|
14 |
+
# Load custom YOLO model
|
15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
16 |
print(f"Using device: {device}")
|
17 |
model = YOLO('./data/best.pt').to(device)
|
18 |
+
print(f"Model classes: {model.names}") # Print classes (should include cracks, potholes)
|
19 |
|
20 |
+
def process_video(video, resize_width=640, resize_height=480, frame_skip=1):
|
21 |
if video is None:
|
22 |
return "Error: No video uploaded"
|
23 |
|
24 |
+
# Start timer
|
25 |
+
start_time = time.time()
|
26 |
+
|
27 |
+
# Open input video
|
28 |
cap = cv2.VideoCapture(video)
|
29 |
if not cap.isOpened():
|
30 |
return "Error: Could not open video file"
|
|
|
34 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
35 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
36 |
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
37 |
+
expected_duration = total_frames / fps
|
38 |
+
print(f"Input video: {frame_width}x{frame_height}, {fps} FPS, {total_frames} frames, {expected_duration:.2f} seconds")
|
39 |
|
40 |
+
# Set output resolution
|
41 |
+
out_width, out_height = resize_width, resize_height
|
42 |
+
print(f"Output resolution: {out_width}x{out_height}")
|
43 |
|
44 |
# Set up video writer
|
45 |
output_path = "processed_output.mp4"
|
46 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'H264' if mp4v fails
|
47 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (out_width, out_height))
|
48 |
|
49 |
frame_count = 0
|
50 |
+
processed_frames = 0
|
51 |
|
52 |
while True:
|
53 |
ret, frame = cap.read()
|
|
|
55 |
break
|
56 |
|
57 |
frame_count += 1
|
58 |
+
|
59 |
+
# Skip frames if frame_skip > 1
|
60 |
+
if frame_count % frame_skip != 0:
|
61 |
+
continue
|
62 |
+
|
63 |
+
processed_frames += 1
|
64 |
print(f"Processing frame {frame_count}/{total_frames}")
|
65 |
|
66 |
+
# Resize frame for faster inference
|
67 |
+
frame = cv2.resize(frame, (out_width, out_height))
|
68 |
|
69 |
+
# Run YOLO inference (detect cracks and potholes)
|
70 |
+
results = model(frame, verbose=False, conf=0.5) # Confidence threshold 0.5
|
71 |
annotated_frame = results[0].plot()
|
72 |
|
73 |
+
# Log detections
|
74 |
+
for detection in results[0].boxes:
|
75 |
+
cls = int(detection.cls)
|
76 |
+
conf = float(detection.conf)
|
77 |
+
print(f"Frame {frame_count}: Detected {model.names[cls]} with confidence {conf:.2f}")
|
78 |
+
|
79 |
+
# Write annotated frame
|
80 |
out.write(annotated_frame)
|
81 |
+
|
82 |
+
# Duplicate frames if skipping to maintain duration
|
83 |
+
if frame_skip > 1:
|
84 |
+
for _ in range(frame_skip - 1):
|
85 |
+
if frame_count + 1 <= total_frames:
|
86 |
+
out.write(annotated_frame)
|
87 |
+
frame_count += 1
|
88 |
|
89 |
# Release resources
|
90 |
cap.release()
|
91 |
out.release()
|
92 |
+
|
93 |
+
# Verify output duration
|
94 |
+
cap = cv2.VideoCapture(output_path)
|
95 |
+
output_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
96 |
+
output_fps = cap.get(cv2.CAP_PROP_FPS)
|
97 |
+
output_duration = output_frames / output_fps
|
98 |
+
cap.release()
|
99 |
+
|
100 |
+
print(f"Output video: {output_frames} frames, {output_fps} FPS, {output_duration:.2f} seconds")
|
101 |
+
print(f"Processing time: {time.time() - start_time:.2f} seconds")
|
102 |
+
|
103 |
return output_path
|
104 |
|
105 |
# Gradio interface
|
106 |
iface = gr.Interface(
|
107 |
fn=process_video,
|
108 |
+
inputs=[
|
109 |
+
gr.Video(label="Upload Video"),
|
110 |
+
gr.Slider(minimum=320, maximum=1280, value=640, label="Output Width", step=1),
|
111 |
+
gr.Slider(minimum=240, maximum=720, value=480, label="Output Height", step=1),
|
112 |
+
gr.Slider(minimum=1, maximum=5, value=1, label="Frame Skip (1 = process all frames)", step=1)
|
113 |
+
],
|
114 |
outputs=gr.Video(label="Processed Video"),
|
115 |
+
title="Crack and Pothole Detection with YOLO",
|
116 |
+
description="Upload a video to detect cracks and potholes. Adjust resolution and frame skip for faster processing."
|
117 |
)
|
118 |
|
119 |
if __name__ == "__main__":
|