Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -125,19 +125,33 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
| 125 |
|
| 126 |
out_width, out_height = resize_width, resize_height
|
| 127 |
output_path = "processed_output.mp4"
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
cap.release()
|
| 134 |
-
return "processed_output.mp4", json.dumps({"error": "
|
| 135 |
|
| 136 |
processed_frames = 0
|
| 137 |
all_detections = []
|
| 138 |
frame_times = []
|
| 139 |
detection_frame_count = 0
|
| 140 |
output_frame_count = 0
|
|
|
|
| 141 |
|
| 142 |
while True:
|
| 143 |
ret, frame = cap.read()
|
|
@@ -159,9 +173,10 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
| 159 |
conf = float(detection.conf)
|
| 160 |
box = detection.xyxy[0].cpu().numpy().astype(int).tolist()
|
| 161 |
label = model.names[cls]
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
| 165 |
|
| 166 |
if frame_detections:
|
| 167 |
detection_frame_count += 1
|
|
@@ -178,6 +193,7 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
| 178 |
# Write frame and duplicates
|
| 179 |
out.write(annotated_frame)
|
| 180 |
output_frame_count += 1
|
|
|
|
| 181 |
if frame_skip > 1:
|
| 182 |
for _ in range(frame_skip - 1):
|
| 183 |
out.write(annotated_frame)
|
|
@@ -195,8 +211,9 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
| 195 |
detection_summary = {
|
| 196 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 197 |
"frame": frame_count,
|
| 198 |
-
"
|
| 199 |
-
"
|
|
|
|
| 200 |
"gps": gps_coord,
|
| 201 |
"processing_time_ms": frame_time
|
| 202 |
}
|
|
@@ -204,9 +221,9 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
| 204 |
if len(log_entries) > 50:
|
| 205 |
log_entries.pop(0)
|
| 206 |
|
| 207 |
-
# Pad remaining frames
|
| 208 |
-
while output_frame_count < total_frames and
|
| 209 |
-
out.write(
|
| 210 |
output_frame_count += 1
|
| 211 |
|
| 212 |
last_metrics = update_metrics(all_detections)
|
|
@@ -250,7 +267,7 @@ def process_video(video, resize_width=320, resize_height=240, frame_skip=5):
|
|
| 250 |
|
| 251 |
# Gradio interface
|
| 252 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
| 253 |
-
gr.Markdown("#
|
| 254 |
with gr.Row():
|
| 255 |
with gr.Column(scale=3):
|
| 256 |
video_input = gr.Video(label="Upload Video")
|
|
|
|
| 125 |
|
| 126 |
out_width, out_height = resize_width, resize_height
|
| 127 |
output_path = "processed_output.mp4"
|
| 128 |
+
# Try codecs in order of preference
|
| 129 |
+
codecs = [('mp4v', '.mp4'), ('MJPG', '.avi'), ('XVID', '.avi')]
|
| 130 |
+
out = None
|
| 131 |
+
for codec, ext in codecs:
|
| 132 |
+
fourcc = cv2.VideoWriter_fourcc(*codec)
|
| 133 |
+
output_path = f"processed_output{ext}"
|
| 134 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (out_width, out_height))
|
| 135 |
+
if out.isOpened():
|
| 136 |
+
log_entries.append(f"Using codec: {codec}, output: {output_path}")
|
| 137 |
+
logging.info(f"Using codec: {codec}, output: {output_path}")
|
| 138 |
+
break
|
| 139 |
+
else:
|
| 140 |
+
log_entries.append(f"Failed to initialize codec: {codec}")
|
| 141 |
+
logging.warning(f"Failed to initialize codec: {codec}")
|
| 142 |
+
|
| 143 |
+
if not out or not out.isOpened():
|
| 144 |
+
log_entries.append("Error: All codecs failed to initialize video writer")
|
| 145 |
+
logging.error("All codecs failed to initialize video writer")
|
| 146 |
cap.release()
|
| 147 |
+
return "processed_output.mp4", json.dumps({"error": "All codecs failed"}, indent=2), "\n".join(log_entries), [], None, None
|
| 148 |
|
| 149 |
processed_frames = 0
|
| 150 |
all_detections = []
|
| 151 |
frame_times = []
|
| 152 |
detection_frame_count = 0
|
| 153 |
output_frame_count = 0
|
| 154 |
+
last_annotated_frame = None
|
| 155 |
|
| 156 |
while True:
|
| 157 |
ret, frame = cap.read()
|
|
|
|
| 173 |
conf = float(detection.conf)
|
| 174 |
box = detection.xyxy[0].cpu().numpy().astype(int).tolist()
|
| 175 |
label = model.names[cls]
|
| 176 |
+
if label != 'Crocodile': # Ignore irrelevant class
|
| 177 |
+
frame_detections.append({"label": label, "box": box, "conf": conf})
|
| 178 |
+
log_entries.append(f"Frame {frame_count}: Detected {label} with confidence {conf:.2f}")
|
| 179 |
+
logging.info(f"Frame {frame_count}: Detected {label} with confidence {conf:.2f}")
|
| 180 |
|
| 181 |
if frame_detections:
|
| 182 |
detection_frame_count += 1
|
|
|
|
| 193 |
# Write frame and duplicates
|
| 194 |
out.write(annotated_frame)
|
| 195 |
output_frame_count += 1
|
| 196 |
+
last_annotated_frame = annotated_frame
|
| 197 |
if frame_skip > 1:
|
| 198 |
for _ in range(frame_skip - 1):
|
| 199 |
out.write(annotated_frame)
|
|
|
|
| 211 |
detection_summary = {
|
| 212 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 213 |
"frame": frame_count,
|
| 214 |
+
"longitudinal": sum(1 for det in frame_detections if det["label"] == "Longitudinal"),
|
| 215 |
+
"pothole": sum(1 for det in frame_detections if det["label"] == "Pothole"),
|
| 216 |
+
"transverse": sum(1 for det in frame_detections if det["label"] == "Transverse"),
|
| 217 |
"gps": gps_coord,
|
| 218 |
"processing_time_ms": frame_time
|
| 219 |
}
|
|
|
|
| 221 |
if len(log_entries) > 50:
|
| 222 |
log_entries.pop(0)
|
| 223 |
|
| 224 |
+
# Pad remaining frames
|
| 225 |
+
while output_frame_count < total_frames and last_annotated_frame is not None:
|
| 226 |
+
out.write(last_annotated_frame)
|
| 227 |
output_frame_count += 1
|
| 228 |
|
| 229 |
last_metrics = update_metrics(all_detections)
|
|
|
|
| 267 |
|
| 268 |
# Gradio interface
|
| 269 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
| 270 |
+
gr.Markdown("# Road Defect Detection Dashboard")
|
| 271 |
with gr.Row():
|
| 272 |
with gr.Column(scale=3):
|
| 273 |
video_input = gr.Video(label="Upload Video")
|