Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,615 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from ultralytics import YOLO
|
|
|
|
|
|
|
5 |
|
6 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def safe_load_yolo_model(path):
|
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/best.pt',
|
15 |
'Toll gates': './data/best2.pt'
|
16 |
-
|
17 |
}
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
-
# Assign colors for each model
|
23 |
model_colors = {
|
24 |
-
'YOLOv11': (0, 255, 0),
|
25 |
-
'Crack & Pothole Detector': (255, 0, 0),
|
26 |
-
'Toll gates': (0, 0, 255)
|
27 |
}
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
cap = cv2.VideoCapture(video)
|
31 |
-
|
|
|
|
|
|
|
32 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
33 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
40 |
ret, frame = cap.read()
|
41 |
if not ret:
|
42 |
break
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
|
|
|
|
47 |
for result in results:
|
48 |
for box in result.boxes:
|
49 |
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
50 |
class_id = int(box.cls[0])
|
51 |
label = f"{model.names[class_id]} - {box.conf[0]:.2f}"
|
52 |
color = model_colors.get(model_name, (0, 255, 255))
|
53 |
-
cv2.rectangle(
|
54 |
-
cv2.putText(
|
55 |
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
-
cap.release()
|
59 |
out.release()
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
)
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
import json
|
7 |
+
import logging
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
import csv
|
10 |
+
import time
|
11 |
+
from datetime import datetime
|
12 |
+
from collections import Counter
|
13 |
+
from typing import List, Dict, Any, Optional
|
14 |
from ultralytics import YOLO
|
15 |
+
import piexif
|
16 |
+
import zipfile
|
17 |
+
import base64
|
18 |
|
19 |
+
# Directory setup
|
20 |
+
os.environ["YOLO_CONFIG_DIR"] = "/tmp/Ultralytics"
|
21 |
+
logging.basicConfig(filename="app.log", level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
22 |
+
|
23 |
+
CAPTURED_FRAMES_DIR = "captured_frames"
|
24 |
+
OUTPUT_DIR = "outputs"
|
25 |
+
FLIGHT_LOG_DIR = "flight_logs"
|
26 |
+
os.makedirs(CAPTURED_FRAMES_DIR, exist_ok=True)
|
27 |
+
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
28 |
+
os.makedirs(FLIGHT_LOG_DIR, exist_ok=True)
|
29 |
+
os.chmod(CAPTURED_FRAMES_DIR, 0o777)
|
30 |
+
os.chmod(OUTPUT_DIR, 0o777)
|
31 |
+
os.chmod(FLIGHT_LOG_DIR, 0o777)
|
32 |
+
|
33 |
+
# Global variables
|
34 |
+
log_entries: List[str] = []
|
35 |
+
detected_counts: List[int] = []
|
36 |
+
detected_issues: List[str] = []
|
37 |
+
gps_coordinates: List[List[float]] = []
|
38 |
+
last_metrics: Dict[str, Any] = {}
|
39 |
+
frame_count: int = 0
|
40 |
+
SAVE_IMAGE_INTERVAL = 1
|
41 |
+
DETECTION_CLASSES = ["Longitudinal", "Pothole", "Transverse", "Toll gate"] # Updated for Toll gates
|
42 |
+
MAX_IMAGES = 500
|
43 |
+
|
44 |
+
# Model setup
|
45 |
def safe_load_yolo_model(path):
|
46 |
torch.serialization.add_safe_globals([torch, 'ultralytics.nn.tasks.DetectionModel'])
|
47 |
return YOLO(path)
|
48 |
|
|
|
49 |
model_paths = {
|
50 |
'YOLOv11': './data/yolo11n.pt',
|
51 |
'Crack & Pothole Detector': './data/best.pt',
|
52 |
'Toll gates': './data/best2.pt'
|
|
|
53 |
}
|
54 |
|
55 |
+
models = {name: safe_load_yolo_model(path).to("cuda" if torch.cuda.is_available() else "cpu") for name, path in model_paths.items()}
|
56 |
+
for name, model in models.items():
|
57 |
+
if torch.cuda.is_available():
|
58 |
+
model.half()
|
59 |
|
|
|
60 |
model_colors = {
|
61 |
+
'YOLOv11': (0, 255, 0), # Green
|
62 |
+
'Crack & Pothole Detector': (255, 0, 0), # Red
|
63 |
+
'Toll gates': (0, 0, 255) # Blue
|
64 |
}
|
65 |
|
66 |
+
# Helper functions (unchanged)
|
67 |
+
def zip_all_outputs(report_path: str, video_path: str, chart_path: str, map_path: str) -> str:
|
68 |
+
zip_path = os.path.join(OUTPUT_DIR, f"drone_analysis_outputs_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip")
|
69 |
+
try:
|
70 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_STORED) as zipf:
|
71 |
+
if os.path.exists(report_path):
|
72 |
+
zipf.write(report_path, os.path.basename(report_path))
|
73 |
+
if os.path.exists(video_path):
|
74 |
+
zipf.write(video_path, os.path.join("outputs", os.path.basename(video_path)))
|
75 |
+
if os.path.exists(chart_path):
|
76 |
+
zipf.write(chart_path, os.path.join("outputs", os.path.basename(chart_path)))
|
77 |
+
if os.path.exists(map_path):
|
78 |
+
zipf.write(map_path, os.path.join("outputs", os.path.basename(map_path)))
|
79 |
+
for file in detected_issues:
|
80 |
+
if os.path.exists(file):
|
81 |
+
zipf.write(file, os.path.join("captured_frames", os.path.basename(file)))
|
82 |
+
for root, _, files in os.walk(FLIGHT_LOG_DIR):
|
83 |
+
for file in files:
|
84 |
+
file_path = os.path.join(root, file)
|
85 |
+
zipf.write(file_path, os.path.join("flight_logs", file))
|
86 |
+
log_entries.append(f"Created ZIP: {zip_path}")
|
87 |
+
return zip_path
|
88 |
+
except Exception as e:
|
89 |
+
log_entries.append(f"Error: Failed to create ZIP: {str(e)}")
|
90 |
+
return ""
|
91 |
+
|
92 |
+
def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) -> str:
|
93 |
+
map_path = os.path.join(OUTPUT_DIR, f"map_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png")
|
94 |
+
plt.figure(figsize=(4, 4))
|
95 |
+
plt.scatter([x[1] for x in gps_coords], [x[0] for x in gps_coords], c='blue', label='GPS Points')
|
96 |
+
plt.title("Issue Locations Map")
|
97 |
+
plt.xlabel("Longitude")
|
98 |
+
plt.ylabel("Latitude")
|
99 |
+
plt.legend()
|
100 |
+
plt.savefig(map_path)
|
101 |
+
plt.close()
|
102 |
+
return map_path
|
103 |
+
|
104 |
+
def write_geotag(image_path: str, gps_coord: List[float]) -> bool:
|
105 |
+
try:
|
106 |
+
lat = abs(gps_coord[0])
|
107 |
+
lon = abs(gps_coord[1])
|
108 |
+
lat_ref = "N" if gps_coord[0] >= 0 else "S"
|
109 |
+
lon_ref = "E" if gps_coord[1] >= 0 else "W"
|
110 |
+
exif_dict = piexif.load(image_path) if os.path.exists(image_path) else {"GPS": {}}
|
111 |
+
exif_dict["GPS"] = {
|
112 |
+
piexif.GPSIFD.GPSLatitudeRef: lat_ref,
|
113 |
+
piexif.GPSIFD.GPSLatitude: ((int(lat), 1), (0, 1), (0, 1)),
|
114 |
+
piexif.GPSIFD.GPSLongitudeRef: lon_ref,
|
115 |
+
piexif.GPSIFD.GPSLongitude: ((int(lon), 1), (0, 1), (0, 1))
|
116 |
+
}
|
117 |
+
piexif.insert(piexif.dump(exif_dict), image_path)
|
118 |
+
return True
|
119 |
+
except Exception as e:
|
120 |
+
log_entries.append(f"Error: Failed to geotag {image_path}: {str(e)}")
|
121 |
+
return False
|
122 |
+
|
123 |
+
def write_flight_log(frame_count: int, gps_coord: List[float], timestamp: str) -> str:
|
124 |
+
log_path = os.path.join(FLIGHT_LOG_DIR, f"flight_log_{frame_count:06d}.csv")
|
125 |
+
try:
|
126 |
+
with open(log_path, 'w', newline='') as csvfile:
|
127 |
+
writer = csv.writer(csvfile)
|
128 |
+
writer.writerow(["Frame", "Timestamp", "Latitude", "Longitude", "Speed_ms", "Satellites", "Altitude_m"])
|
129 |
+
writer.writerow([frame_count, timestamp, gps_coord[0], gps_coord[1], 5.0, 12, 60])
|
130 |
+
return log_path
|
131 |
+
except Exception as e:
|
132 |
+
log_entries.append(f"Error: Failed to write flight log {log_path}: {str(e)}")
|
133 |
+
return ""
|
134 |
+
|
135 |
+
def check_image_quality(frame: np.ndarray, input_resolution: int) -> bool:
|
136 |
+
height, width, _ = frame.shape
|
137 |
+
frame_resolution = width * height
|
138 |
+
if frame_resolution < 2_073_600:
|
139 |
+
log_entries.append(f"Frame {frame_count}: Resolution {width}x{height} below 2MP")
|
140 |
+
return False
|
141 |
+
if frame_resolution < input_resolution:
|
142 |
+
log_entries.append(f"Frame {frame_count}: Output resolution below input")
|
143 |
+
return False
|
144 |
+
return True
|
145 |
+
|
146 |
+
def update_metrics(detections: List[Dict[str, Any]]) -> Dict[str, Any]:
|
147 |
+
counts = Counter([(det["label"], det["model"]) for det in detections])
|
148 |
+
return {
|
149 |
+
"items": [{"type": k[0], "model": k[1], "count": v} for k, v in counts.items()],
|
150 |
+
"total_detections": len(detections),
|
151 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
152 |
+
}
|
153 |
+
|
154 |
+
def generate_line_chart() -> Optional[str]:
|
155 |
+
if not detected_counts:
|
156 |
+
return None
|
157 |
+
plt.figure(figsize=(4, 2))
|
158 |
+
plt.plot(detected_counts[-50:], marker='o', color='#FF8C00')
|
159 |
+
plt.title("Detections Over Time")
|
160 |
+
plt.xlabel("Frame")
|
161 |
+
plt.ylabel("Count")
|
162 |
+
plt.grid(True)
|
163 |
+
plt.tight_layout()
|
164 |
+
chart_path = os.path.join(OUTPUT_DIR, f"chart_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png")
|
165 |
+
plt.savefig(chart_path)
|
166 |
+
plt.close()
|
167 |
+
return chart_path
|
168 |
+
|
169 |
+
def generate_report(
|
170 |
+
metrics: Dict[str, Any],
|
171 |
+
detected_issues: List[str],
|
172 |
+
gps_coordinates: List[List[float]],
|
173 |
+
all_detections: List[Dict[str, Any]],
|
174 |
+
frame_count: int,
|
175 |
+
total_time: float,
|
176 |
+
output_frames: int,
|
177 |
+
output_fps: float,
|
178 |
+
output_duration: float,
|
179 |
+
detection_frame_count: int,
|
180 |
+
chart_path: str,
|
181 |
+
map_path: str,
|
182 |
+
frame_times: List[float],
|
183 |
+
resize_times: List[float],
|
184 |
+
inference_times: List[float],
|
185 |
+
io_times: List[float]
|
186 |
+
) -> str:
|
187 |
+
log_entries.append("Generating report...")
|
188 |
+
report_path = os.path.join(OUTPUT_DIR, f"drone_analysis_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html")
|
189 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
190 |
+
report_content = [
|
191 |
+
"<!DOCTYPE html>",
|
192 |
+
"<html lang='en'>",
|
193 |
+
"<head>",
|
194 |
+
"<meta charset='UTF-8'>",
|
195 |
+
"<title>NHAI Drone Survey Analysis Report</title>",
|
196 |
+
"<style>",
|
197 |
+
"body { font-family: Arial, sans-serif; margin: 40px; }",
|
198 |
+
"h1, h2, h3 { color: #333; }",
|
199 |
+
"ul { margin-left: 20px; }",
|
200 |
+
"table { border-collapse: collapse; width: 100%; margin: 10px 0; }",
|
201 |
+
"th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }",
|
202 |
+
"th { background-color: #f2f2f2; }",
|
203 |
+
"img { max-width: 600px; height: auto; margin: 10px 0; }",
|
204 |
+
"p.caption { font-weight: bold; margin: 5px 0; }",
|
205 |
+
"</style>",
|
206 |
+
"</head>",
|
207 |
+
"<body>",
|
208 |
+
"<h1>NHAI Drone Survey Analysis Report</h1>",
|
209 |
+
"<h2>Project Details</h2>",
|
210 |
+
"<ul>",
|
211 |
+
"<li><strong>Project Name:</strong> NH-44 Delhi-Hyderabad Section (Package XYZ)</li>",
|
212 |
+
"<li><strong>Highway Section:</strong> Km 100 to Km 150</li>",
|
213 |
+
"<li><strong>State:</strong> Telangana</li>",
|
214 |
+
"<li><strong>Region:</strong> South</li>",
|
215 |
+
f"<li><strong>Survey Date:</strong> {datetime.now().strftime('%Y-%m-%d')}</li>",
|
216 |
+
"<li><strong>Drone Service Provider:</strong> ABC Drone Services Pvt. Ltd.</li>",
|
217 |
+
"<li><strong>Technology Service Provider:</strong> XYZ AI Analytics Ltd.</li>",
|
218 |
+
f"<li><strong>Work Order Reference:</strong> Data Lake WO-{datetime.now().strftime('%Y%m%d')}-XYZ</li>",
|
219 |
+
"<li><strong>Report Prepared By:</strong> Nagasurendra, Data Analyst</li>",
|
220 |
+
f"<li><strong>Report Date:</strong> {datetime.now().strftime('%Y-%m-%d')}</li>",
|
221 |
+
"</ul>",
|
222 |
+
"<h2>1. Introduction</h2>",
|
223 |
+
"<p>This report consolidates drone survey results for NH-44 (Km 100–150) using multiple YOLO models for detecting road defects and toll gates.</p>",
|
224 |
+
"<h2>2. Drone Survey Metadata</h2>",
|
225 |
+
"<ul>",
|
226 |
+
"<li><strong>Drone Speed:</strong> 5 m/s</li>",
|
227 |
+
"<li><strong>Drone Height:</strong> 60 m</li>",
|
228 |
+
"<li><strong>Camera Sensor:</strong> RGB, 12 MP</li>",
|
229 |
+
"<li><strong>Recording Type:</strong> JPEG, 90° nadir</li>",
|
230 |
+
"<li><strong>Image Overlap:</strong> 85%</li>",
|
231 |
+
"<li><strong>Flight Pattern:</strong> Single lap, ROW centered</li>",
|
232 |
+
"<li><strong>Geotagging:</strong> Enabled</li>",
|
233 |
+
"<li><strong>Satellite Lock:</strong> 12 satellites</li>",
|
234 |
+
"<li><strong>Terrain Follow Mode:</strong> Enabled</li>",
|
235 |
+
"</ul>",
|
236 |
+
"<h2>3. Quality Check Results</h2>",
|
237 |
+
"<ul>",
|
238 |
+
"<li><strong>Resolution:</strong> 1920x1080</li>",
|
239 |
+
"<li><strong>Overlap:</strong> 85%</li>",
|
240 |
+
"<li><strong>Camera Angle:</strong> 90° nadir</li>",
|
241 |
+
"<li><strong>Drone Speed:</strong> ≤ 5 m/s</li>",
|
242 |
+
"<li><strong>Geotagging:</strong> 100% compliant</li>",
|
243 |
+
"<li><strong>QC Status:</strong> Passed</li>",
|
244 |
+
"</ul>",
|
245 |
+
"<h2>4. AI/ML Analytics</h2>",
|
246 |
+
f"<p><strong>Total Frames Processed:</strong> {frame_count}</p>",
|
247 |
+
f"<p><strong>Detection Frames:</strong> {detection_frame_count} ({detection_frame_count/frame_count*100:.1f}%)</p>",
|
248 |
+
f"<p><strong>Total Detections:</strong> {metrics['total_detections']}</p>",
|
249 |
+
"<p><strong>Breakdown by Model and Type:</strong></p>",
|
250 |
+
"<ul>"
|
251 |
+
]
|
252 |
+
|
253 |
+
for item in metrics.get("items", []):
|
254 |
+
percentage = (item["count"] / metrics["total_detections"] * 100) if metrics["total_detections"] > 0 else 0
|
255 |
+
report_content.append(f"<li>{item['type']} (Model: {item['model']}): {item['count']} ({percentage:.1f}%)</li>")
|
256 |
+
report_content.extend([
|
257 |
+
"</ul>",
|
258 |
+
f"<p><strong>Processing Time:</strong> {total_time:.1f} seconds</p>",
|
259 |
+
f"<p><strong>Average Frame Time:</strong> {sum(frame_times)/len(frame_times):.1f} ms</p>" if frame_times else "<p><strong>Average Frame Time:</strong> N/A</p>",
|
260 |
+
f"<p><strong>Average Resize Time:</strong> {sum(resize_times)/len(resize_times):.1f} ms</p>" if resize_times else "<p><strong>Average Resize Time:</strong> N/A</p>",
|
261 |
+
f"<p><strong>Average Inference Time:</strong> {sum(inference_times)/len(inference_times):.1f} ms</p>" if inference_times else "<p><strong>Average Inference Time:</strong> N/A</p>",
|
262 |
+
f"<p><strong>Average I/O Time:</strong> {sum(io_times)/len(io_times):.1f} ms</p>" if io_times else "<p><strong>Average I/O Time:</strong> N/A</p>",
|
263 |
+
f"<p><strong>Timestamp:</strong> {metrics.get('timestamp', 'N/A')}</p>",
|
264 |
+
"<p><strong>Summary:</strong> Road defects and toll gates detected across multiple models.</p>",
|
265 |
+
"<h2>5. Output File Structure</h2>",
|
266 |
+
"<p>ZIP file contains:</p>",
|
267 |
+
"<ul>",
|
268 |
+
f"<li><code>drone_analysis_report_{timestamp}.html</code>: This report</li>",
|
269 |
+
"<li><code>outputs/processed_output.mp4</code>: Processed video with annotations</li>",
|
270 |
+
f"<li><code>outputs/chart_{timestamp}.png</code>: Detection trend chart</li>",
|
271 |
+
f"<li><code>outputs/map_{timestamp}.png</code>: Issue locations map</li>",
|
272 |
+
"<li><code>captured_frames/detected_<frame>.jpg</code>: Geotagged images for detected issues</li>",
|
273 |
+
"<li><code>flight_logs/flight_log_<frame>.csv</code>: Flight logs matching image frames</li>",
|
274 |
+
"</ul>",
|
275 |
+
"<p><strong>Note:</strong> Images and logs share frame numbers (e.g., <code>detected_000001.jpg</code> corresponds to <code>flight_log_000001.csv</code>).</p>",
|
276 |
+
"<h2>6. Geotagged Images</h2>",
|
277 |
+
f"<p><strong>Total Images:</strong> {len(detected_issues)}</p>",
|
278 |
+
f"<p><strong>Storage:</strong> Data Lake <code>/project_xyz/images/{datetime.now().strftime('%Y%m%d')}</code></p>",
|
279 |
+
"<table>",
|
280 |
+
"<tr><th>Frame</th><th>Issue Type</th><th>Model</th><th>GPS (Lat, Lon)</th><th>Timestamp</th><th>Confidence</th><th>Image Path</th></tr>"
|
281 |
+
])
|
282 |
+
|
283 |
+
for detection in all_detections[:100]:
|
284 |
+
report_content.append(
|
285 |
+
f"<tr><td>{detection['frame']:06d}</td><td>{detection['label']}</td><td>{detection['model']}</td><td>({detection['gps'][0]:.6f}, {detection['gps'][1]:.6f})</td><td>{detection['timestamp']}</td><td>{detection['conf']:.1f}</td><td>captured_frames/{os.path.basename(detection['path'])}</td></tr>"
|
286 |
+
)
|
287 |
+
|
288 |
+
report_content.extend([
|
289 |
+
"</table>",
|
290 |
+
"<h2>7. Flight Logs</h2>",
|
291 |
+
f"<p><strong>Total Logs:</strong> {len(detected_issues)}</p>",
|
292 |
+
f"<p><strong>Storage:</strong> Data Lake <code>/project_xyz/flight_logs/{datetime.now().strftime('%Y%m%d')}</code></p>",
|
293 |
+
"<table>",
|
294 |
+
"<tr><th>Frame</th><th>Timestamp</th><th>Latitude</th><th>Longitude</th><th>Speed (m/s)</th><th>Satellites</th><th>Altitude (m)</th><th>Log Path</th></tr>"
|
295 |
+
])
|
296 |
+
|
297 |
+
for detection in all_detections[:100]:
|
298 |
+
log_path = f"flight_logs/flight_log_{detection['frame']:06d}.csv"
|
299 |
+
report_content.append(
|
300 |
+
f"<tr><td>{detection['frame']:06d}</td><td>{detection['timestamp']}</td><td>{detection['gps'][0]:.6f}</td><td>{detection['gps'][1]:.6f}</td><td>5.0</td><td>12</td><td>60</td><td>{log_path}</td></tr>"
|
301 |
+
)
|
302 |
+
|
303 |
+
report_content.extend([
|
304 |
+
"</table>",
|
305 |
+
"<h2>8. Processed Video</h2>",
|
306 |
+
f"<p><strong>Path:</strong> outputs/processed_output.mp4</p>",
|
307 |
+
f"<p><strong>Frames:</strong> {output_frames}</p>",
|
308 |
+
f"<p><strong>FPS:</strong> {output_fps:.1f}</p>",
|
309 |
+
f"<p><strong>Duration:</strong> {output_duration:.1f} seconds</p>",
|
310 |
+
"<h2>9. Visualizations</h2>",
|
311 |
+
f"<p><strong>Detection Trend Chart:</strong> outputs/chart_{timestamp}.png</p>",
|
312 |
+
f"<p><strong>Issue Locations Map:</strong> outputs/map_{timestamp}.png</p>",
|
313 |
+
"<h2>10. Processing Timestamps</h2>",
|
314 |
+
f"<p><strong>Total Processing Time:</strong> {total_time:.1f} seconds</p>",
|
315 |
+
"<p><strong>Log Entries (Last 10):</strong></p>",
|
316 |
+
"<ul>"
|
317 |
+
])
|
318 |
+
|
319 |
+
for entry in log_entries[-10:]:
|
320 |
+
report_content.append(f"<li>{entry}</li>")
|
321 |
+
|
322 |
+
report_content.extend([
|
323 |
+
"</ul>",
|
324 |
+
"<h2>11. Stakeholder Validation</h2>",
|
325 |
+
"<ul>",
|
326 |
+
"<li><strong>AE/IE Comments:</strong> [Pending]</li>",
|
327 |
+
"<li><strong>PD/RO Comments:</strong> [Pending]</li>",
|
328 |
+
"</ul>",
|
329 |
+
"<h2>12. Recommendations</h2>",
|
330 |
+
"<ul>",
|
331 |
+
"<li>Repair potholes in high-traffic areas.</li>",
|
332 |
+
"<li>Seal cracks to prevent further degradation.</li>",
|
333 |
+
"<li>Inspect detected toll gates for compliance.</li>",
|
334 |
+
"</ul>",
|
335 |
+
"<h2>13. Data Lake References</h2>",
|
336 |
+
"<ul>",
|
337 |
+
f"<li><strong>Images:</strong> <code>/project_xyz/images/{datetime.now().strftime('%Y%m%d')}</code></li>",
|
338 |
+
f"<li><strong>Flight Logs:</strong> <code>/project_xyz/flight_logs/{datetime.now().strftime('%Y%m%d')}</code></li>",
|
339 |
+
f"<li><strong>Video:</strong> <code>/project_xyz/videos/processed_output_{timestamp}.mp4</code></li>",
|
340 |
+
f"<li><strong>DAMS Dashboard:</strong> <code>/project_xyz/dams/{datetime.now().strftime('%Y%m%d')}</code></li>",
|
341 |
+
"</ul>",
|
342 |
+
"<h2>14. Captured Images</h2>",
|
343 |
+
"<p>Below are the embedded images from the captured frames directory showing detected issues:</p>",
|
344 |
+
""
|
345 |
+
])
|
346 |
+
|
347 |
+
for image_path in detected_issues:
|
348 |
+
if os.path.exists(image_path):
|
349 |
+
image_name = os.path.basename(image_path)
|
350 |
+
try:
|
351 |
+
with open(image_path, "rb") as image_file:
|
352 |
+
base64_string = base64.b64encode(image_file.read()).decode('utf-8')
|
353 |
+
report_content.append(f"<img src='data:image/jpeg;base64,{base64_string}' alt='{image_name}'>")
|
354 |
+
report_content.append(f"<p class='caption'>Image: {image_name}</p>")
|
355 |
+
report_content.append("")
|
356 |
+
except Exception as e:
|
357 |
+
log_entries.append(f"Error: Failed to encode image {image_name} to base64: {str(e)}")
|
358 |
+
|
359 |
+
report_content.extend([
|
360 |
+
"</body>",
|
361 |
+
"</html>"
|
362 |
+
])
|
363 |
+
|
364 |
+
try:
|
365 |
+
with open(report_path, 'w') as f:
|
366 |
+
f.write("\n".join(report_content))
|
367 |
+
log_entries.append(f"Report saved at: {report_path}")
|
368 |
+
return report_path
|
369 |
+
except Exception as e:
|
370 |
+
log_entries.append(f"Error: Failed to save report: {str(e)}")
|
371 |
+
return ""
|
372 |
+
|
373 |
+
def process_video(video, selected_model, resize_width=1920, resize_height=1080, frame_skip=10):
|
374 |
+
global frame_count, last_metrics, detected_counts, detected_issues, gps_coordinates, log_entries
|
375 |
+
frame_count = 0
|
376 |
+
detected_counts.clear()
|
377 |
+
detected_issues.clear()
|
378 |
+
gps_coordinates.clear()
|
379 |
+
log_entries.clear()
|
380 |
+
last_metrics = {}
|
381 |
+
|
382 |
+
if video is None:
|
383 |
+
log_entries.append("Error: No video uploaded")
|
384 |
+
return None, json.dumps({"error": "No video uploaded"}, indent=2), "\n".join(log_entries), [], None, None, None
|
385 |
+
|
386 |
+
log_entries.append("Starting video processing...")
|
387 |
+
start_time = time.time()
|
388 |
cap = cv2.VideoCapture(video)
|
389 |
+
if not cap.isOpened():
|
390 |
+
log_entries.append("Error: Could not open video file")
|
391 |
+
return None, json.dumps({"error": "Could not open video file"}, indent=2), "\n".join(log_entries), [], None, None, None
|
392 |
+
|
393 |
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
394 |
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
395 |
+
input_resolution = frame_width * frame_height
|
396 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
397 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
398 |
+
log_entries.append(f"Input video: {frame_width}x{frame_height} at {fps} FPS, {total_frames} frames")
|
399 |
|
400 |
+
out_width, out_height = resize_width, resize_height
|
401 |
+
output_path = os.path.join(OUTPUT_DIR, "processed_output.mp4")
|
402 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'XVID'), fps, (out_width, out_height))
|
403 |
+
if not out.isOpened():
|
404 |
+
log_entries.append("Error: Failed to initialize video writer")
|
405 |
+
cap.release()
|
406 |
+
return None, json.dumps({"error": "Video writer failed"}, indent=2), "\n".join(log_entries), [], None, None, None
|
407 |
|
408 |
+
processed_frames = 0
|
409 |
+
all_detections = []
|
410 |
+
frame_times = []
|
411 |
+
inference_times = []
|
412 |
+
resize_times = []
|
413 |
+
io_times = []
|
414 |
+
detection_frame_count = 0
|
415 |
+
output_frame_count = 0
|
416 |
+
last_annotated_frame = None
|
417 |
+
disk_space_threshold = 1024 * 1024 * 1024
|
418 |
|
419 |
+
# Select models based on dropdown
|
420 |
+
use_models = models if selected_model == "All" else {selected_model: models[selected_model]}
|
421 |
+
|
422 |
+
while True:
|
423 |
ret, frame = cap.read()
|
424 |
if not ret:
|
425 |
break
|
426 |
+
frame_count += 1
|
427 |
+
if frame_count % frame_skip != 0:
|
428 |
+
continue
|
429 |
+
processed_frames += 1
|
430 |
+
frame_start = time.time()
|
431 |
|
432 |
+
if os.statvfs(os.path.dirname(output_path)).f_frsize * os.statvfs(os.path.dirname(output_path)).f_bavail < disk_space_threshold:
|
433 |
+
log_entries.append("Error: Insufficient disk space")
|
434 |
+
break
|
435 |
+
|
436 |
+
frame = cv2.resize(frame, (out_width, out_height))
|
437 |
+
resize_times.append((time.time() - frame_start) * 1000)
|
438 |
+
|
439 |
+
if not check_image_quality(frame, input_resolution):
|
440 |
+
continue
|
441 |
+
|
442 |
+
annotated_frame = frame.copy()
|
443 |
+
frame_detections = []
|
444 |
+
inference_start = time.time()
|
445 |
|
446 |
+
for model_name, model in use_models.items():
|
447 |
+
results = model(annotated_frame, verbose=False, conf=0.5, iou=0.7)
|
448 |
for result in results:
|
449 |
for box in result.boxes:
|
450 |
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
451 |
class_id = int(box.cls[0])
|
452 |
label = f"{model.names[class_id]} - {box.conf[0]:.2f}"
|
453 |
color = model_colors.get(model_name, (0, 255, 255))
|
454 |
+
cv2.rectangle(annotated_frame, (x1, y1), (x2, y2), color, 2)
|
455 |
+
cv2.putText(annotated_frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color, 2)
|
456 |
|
457 |
+
if model.names[class_id] in DETECTION_CLASSES:
|
458 |
+
detection_data = {
|
459 |
+
"label": model.names[class_id],
|
460 |
+
"model": model_name,
|
461 |
+
"box": [x1, y1, x2, y2],
|
462 |
+
"conf": float(box.conf[0]),
|
463 |
+
"gps": [17.385044 + (frame_count * 0.0001), 78.486671 + (frame_count * 0.0001)],
|
464 |
+
"timestamp": f"{int(frame_count / fps // 60):02d}:{int(frame_count / fps % 60):02d}",
|
465 |
+
"frame": frame_count,
|
466 |
+
"path": os.path.join(CAPTURED_FRAMES_DIR, f"detected_{frame_count:06d}.jpg")
|
467 |
+
}
|
468 |
+
frame_detections.append(detection_data)
|
469 |
+
|
470 |
+
inference_times.append((time.time() - inference_start) * 1000)
|
471 |
+
|
472 |
+
frame_timestamp = frame_count / fps if fps > 0 else 0
|
473 |
+
timestamp_str = f"{int(frame_timestamp // 60):02d}:{int(frame_timestamp % 60):02d}"
|
474 |
+
gps_coord = [17.385044 + (frame_count * 0.0001), 78.486671 + (frame_count * 0.0001)]
|
475 |
+
gps_coordinates.append(gps_coord)
|
476 |
+
|
477 |
+
io_start = time.time()
|
478 |
+
if frame_detections:
|
479 |
+
detection_frame_count += 1
|
480 |
+
if detection_frame_count % SAVE_IMAGE_INTERVAL == 0:
|
481 |
+
captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"detected_{frame_count:06d}.jpg")
|
482 |
+
if cv2.imwrite(captured_frame_path, annotated_frame):
|
483 |
+
if write_geotag(captured_frame_path, gps_coord):
|
484 |
+
detected_issues.append(captured_frame_path)
|
485 |
+
if len(detected_issues) > MAX_IMAGES:
|
486 |
+
os.remove(detected_issues.pop(0))
|
487 |
+
else:
|
488 |
+
log_entries.append(f"Frame {frame_count}: Geotagging failed")
|
489 |
+
else:
|
490 |
+
log_entries.append(f"Error: Failed to save frame at {captured_frame_path}")
|
491 |
+
write_flight_log(frame_count, gps_coord, timestamp_str)
|
492 |
+
|
493 |
+
io_times.append((time.time() - io_start) * 1000)
|
494 |
+
|
495 |
+
out.write(annotated_frame)
|
496 |
+
output_frame_count += 1
|
497 |
+
last_annotated_frame = annotated_frame
|
498 |
+
if frame_skip > 1:
|
499 |
+
for _ in range(frame_skip - 1):
|
500 |
+
out.write(annotated_frame)
|
501 |
+
output_frame_count += 1
|
502 |
+
|
503 |
+
detected_counts.append(len(frame_detections))
|
504 |
+
all_detections.extend(frame_detections)
|
505 |
+
for detection in frame_detections:
|
506 |
+
log_entries.append(f"Frame {frame_count} at {timestamp_str}: Detected {detection['label']} (Model: {detection['model']}) with confidence {detection['conf']:.2f}")
|
507 |
+
|
508 |
+
frame_times.append((time.time() - frame_start) * 1000)
|
509 |
+
if len(log_entries) > 50:
|
510 |
+
log_entries.pop(0)
|
511 |
+
|
512 |
+
if time.time() - start_time > 600:
|
513 |
+
log_entries.append("Error: Processing timeout after 600 seconds")
|
514 |
+
break
|
515 |
+
|
516 |
+
while output_frame_count < total_frames and last_annotated_frame is not None:
|
517 |
+
out.write(last_annotated_frame)
|
518 |
+
output_frame_count += 1
|
519 |
+
|
520 |
+
last_metrics = update_metrics(all_detections)
|
521 |
|
|
|
522 |
out.release()
|
523 |
+
cap.release()
|
524 |
+
|
525 |
+
cap = cv2.VideoCapture(output_path)
|
526 |
+
if not cap.isOpened():
|
527 |
+
log_entries.append("Error: Failed to open output video for verification")
|
528 |
+
output_path = None
|
529 |
+
else:
|
530 |
+
output_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
531 |
+
output_fps = cap.get(cv2.CAP_PROP_FPS)
|
532 |
+
output_duration = output_frames / output_fps if output_fps > 0 else 0
|
533 |
+
cap.release()
|
534 |
+
log_entries.append(f"Output video: {output_frames} frames, {output_fps:.2f} FPS, {output_duration:.2f} seconds")
|
535 |
+
|
536 |
+
total_time = time.time() - start_time
|
537 |
+
log_entries.append(f"Processing completed in {total_time:.2f} seconds")
|
538 |
+
|
539 |
+
chart_path = generate_line_chart()
|
540 |
+
map_path = generate_map(gps_coordinates[-5:], all_detections)
|
541 |
+
report_path = generate_report(
|
542 |
+
last_metrics,
|
543 |
+
detected_issues,
|
544 |
+
gps_coordinates,
|
545 |
+
all_detections,
|
546 |
+
frame_count,
|
547 |
+
total_time,
|
548 |
+
output_frames,
|
549 |
+
output_fps,
|
550 |
+
output_duration,
|
551 |
+
detection_frame_count,
|
552 |
+
chart_path,
|
553 |
+
map_path,
|
554 |
+
frame_times,
|
555 |
+
resize_times,
|
556 |
+
inference_times,
|
557 |
+
io_times
|
558 |
+
)
|
559 |
+
output_zip_path = zip_all_outputs(report_path, output_path, chart_path, map_path)
|
560 |
+
|
561 |
+
return (
|
562 |
+
output_path,
|
563 |
+
json.dumps(last_metrics, indent=2),
|
564 |
+
"\n".join(log_entries[-10:]),
|
565 |
+
detected_issues,
|
566 |
+
chart_path,
|
567 |
+
map_path,
|
568 |
+
output_zip_path
|
569 |
+
)
|
570 |
+
|
571 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
572 |
+
gr.Markdown("# NHAI Road Defect Detection Dashboard")
|
573 |
+
with gr.Row():
|
574 |
+
with gr.Column(scale=3):
|
575 |
+
video_input = gr.Video(label="Upload Video")
|
576 |
+
model_dropdown = gr.Dropdown(
|
577 |
+
choices=["All"] + list(model_paths.keys()),
|
578 |
+
label="Select YOLO Model(s)",
|
579 |
+
value="All"
|
580 |
+
)
|
581 |
+
width_slider = gr.Slider(320, 1920, value=1920, label="Output Width", step=1)
|
582 |
+
height_slider = gr.Slider(240, 1080, value=1080, label="Output Height", step=1)
|
583 |
+
skip_slider = gr.Slider(1, 20, value=10, label="Frame Skip", step=1)
|
584 |
+
process_btn = gr.Button("Process Video", variant="primary")
|
585 |
+
with gr.Column(scale=1):
|
586 |
+
metrics_output = gr.Textbox(label="Detection Metrics", lines=5, interactive=False)
|
587 |
+
with gr.Row():
|
588 |
+
video_output = gr.Video(label="Processed Video")
|
589 |
+
issue_gallery = gr.Gallery(label="Detected Issues", columns=4, height="auto", object_fit="contain")
|
590 |
+
with gr.Row():
|
591 |
+
chart_output = gr.Image(label="Detection Trend")
|
592 |
+
map_output = gr.Image(label="Issue Locations Map")
|
593 |
+
with gr.Row():
|
594 |
+
logs_output = gr.Textbox(label="Logs", lines=5, interactive=False)
|
595 |
+
with gr.Row():
|
596 |
+
gr.Markdown("## Download Results")
|
597 |
+
with gr.Row():
|
598 |
+
output_zip_download = gr.File(label="Download All Outputs (ZIP)")
|
599 |
+
|
600 |
+
process_btn.click(
|
601 |
+
fn=process_video,
|
602 |
+
inputs=[video_input, model_dropdown, width_slider, height_slider, skip_slider],
|
603 |
+
outputs=[
|
604 |
+
video_output,
|
605 |
+
metrics_output,
|
606 |
+
logs_output,
|
607 |
+
issue_gallery,
|
608 |
+
chart_output,
|
609 |
+
map_output,
|
610 |
+
output_zip_download
|
611 |
+
]
|
612 |
+
)
|
613 |
|
614 |
+
if __name__ == "__main__":
|
615 |
+
iface.launch()
|