Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,14 @@ from services.crack_detection_service import detect_cracks_and_objects
|
|
11 |
from services.overlay_service import overlay_boxes
|
12 |
from services.metrics_service import update_metrics
|
13 |
from services.salesforce_dispatcher import dispatch_to_salesforce
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Globals
|
16 |
paused = False
|
@@ -25,6 +33,7 @@ last_timestamp = ""
|
|
25 |
last_detected_images = [] # Store up to 100+ crack images
|
26 |
gps_coordinates = []
|
27 |
video_loaded = False
|
|
|
28 |
|
29 |
# Constants
|
30 |
DEFAULT_VIDEO_PATH = "sample.mp4"
|
@@ -51,6 +60,15 @@ def initialize_video(video_file=None):
|
|
51 |
log_entries.append(status)
|
52 |
return status
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
def monitor_feed():
|
55 |
"""
|
56 |
Main function to process video frames in real-time.
|
@@ -71,18 +89,49 @@ def monitor_feed():
|
|
71 |
log_entries.append(f"Error: {str(e)}")
|
72 |
return None, json.dumps(last_detections, indent=2), "\n".join(log_entries[-10:]), None, None, last_detected_images
|
73 |
|
74 |
-
#
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
cv2.imwrite(TEMP_IMAGE_PATH, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
|
78 |
-
metrics = update_metrics(
|
79 |
|
80 |
# Simulate GPS coordinates
|
81 |
gps_coord = [17.385044 + random.uniform(-0.001, 0.001), 78.486671 + frame_count * 0.0001]
|
82 |
gps_coordinates.append(gps_coord)
|
83 |
|
84 |
# Save frame if cracks are detected
|
85 |
-
if any(item['type'] == 'crack' for item in
|
86 |
captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"crack_{frame_count}.jpg")
|
87 |
cv2.imwrite(captured_frame_path, frame)
|
88 |
last_detected_images.append(captured_frame_path)
|
@@ -91,7 +140,7 @@ def monitor_feed():
|
|
91 |
|
92 |
# Combine detections for Salesforce
|
93 |
all_detections = {
|
94 |
-
"items":
|
95 |
"metrics": metrics,
|
96 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
97 |
"frame_count": frame_count,
|
@@ -111,14 +160,14 @@ def monitor_feed():
|
|
111 |
last_detections = metrics
|
112 |
|
113 |
# Update logs and stats
|
114 |
-
crack_detected = len([item for item in
|
115 |
crack_severity_all.extend([
|
116 |
item['severity']
|
117 |
-
for item in
|
118 |
if item['type'] == 'crack' and 'severity' in item
|
119 |
])
|
120 |
|
121 |
-
log_entries.append(f"{last_timestamp} - Frame {frame_count} - Cracks: {crack_detected} - GPS: {gps_coord} - Avg Conf: {metrics['avg_confidence']:.2f}")
|
122 |
crack_counts.append(crack_detected)
|
123 |
|
124 |
if len(log_entries) > 100:
|
@@ -236,13 +285,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
236 |
load_button = gr.Button("Load Video")
|
237 |
video_status = gr.Textbox(label="Video Load Status", value="Please upload a video file or ensure 'sample.mp4' exists in the root directory.")
|
238 |
|
|
|
|
|
|
|
|
|
|
|
239 |
status_text = gr.Markdown("**Status:** 🟢 Ready (Upload a video to start)")
|
240 |
|
241 |
with gr.Row():
|
242 |
with gr.Column(scale=3):
|
243 |
video_output = gr.Image(label="Live Drone Feed", width=640, height=480)
|
244 |
with gr.Column(scale=1):
|
245 |
-
detections_output = gr.Textbox(label="
|
246 |
|
247 |
with gr.Row():
|
248 |
logs_output = gr.Textbox(label="Live Logs", lines=8)
|
@@ -281,6 +335,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
281 |
outputs=[video_status]
|
282 |
)
|
283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
284 |
pause_btn.click(toggle_pause, outputs=status_text)
|
285 |
resume_btn.click(toggle_resume, outputs=status_text)
|
286 |
frame_slider.change(set_frame_rate, inputs=[frame_slider])
|
|
|
11 |
from services.overlay_service import overlay_boxes
|
12 |
from services.metrics_service import update_metrics
|
13 |
from services.salesforce_dispatcher import dispatch_to_salesforce
|
14 |
+
# Under Construction services
|
15 |
+
from services.under_construction.earthwork_detection import process_earthwork
|
16 |
+
from services.under_construction.culvert_check import process_culverts
|
17 |
+
from services.under_construction.bridge_pier_check import process_bridge_piers
|
18 |
+
# Original services
|
19 |
+
from services.detection_service import process_frame as process_generic
|
20 |
+
from services.shadow_detection import detect_shadows
|
21 |
+
from services.thermal_service import process_thermal
|
22 |
|
23 |
# Globals
|
24 |
paused = False
|
|
|
33 |
last_detected_images = [] # Store up to 100+ crack images
|
34 |
gps_coordinates = []
|
35 |
video_loaded = False
|
36 |
+
enable_under_construction = False # Toggle for under_construction services
|
37 |
|
38 |
# Constants
|
39 |
DEFAULT_VIDEO_PATH = "sample.mp4"
|
|
|
60 |
log_entries.append(status)
|
61 |
return status
|
62 |
|
63 |
+
def toggle_under_construction(value):
|
64 |
+
"""
|
65 |
+
Toggle the under_construction services.
|
66 |
+
"""
|
67 |
+
global enable_under_construction
|
68 |
+
enable_under_construction = value
|
69 |
+
log_entries.append(f"Under Construction Services {'Enabled' if value else 'Disabled'}")
|
70 |
+
return f"Under Construction Services: {'Enabled' if value else 'Disabled'}"
|
71 |
+
|
72 |
def monitor_feed():
|
73 |
"""
|
74 |
Main function to process video frames in real-time.
|
|
|
89 |
log_entries.append(f"Error: {str(e)}")
|
90 |
return None, json.dumps(last_detections, indent=2), "\n".join(log_entries[-10:]), None, None, last_detected_images
|
91 |
|
92 |
+
# Initialize detected items list
|
93 |
+
all_detected_items = []
|
94 |
+
|
95 |
+
# Always run crack detection (primary focus)
|
96 |
+
crack_items = detect_cracks_and_objects(frame)
|
97 |
+
frame = overlay_boxes(frame, crack_items)
|
98 |
+
all_detected_items.extend(crack_items)
|
99 |
+
|
100 |
+
# Run Under Construction services if enabled
|
101 |
+
if enable_under_construction:
|
102 |
+
earthwork_dets, frame = process_earthwork(frame)
|
103 |
+
culvert_dets, frame = process_culverts(frame)
|
104 |
+
bridge_pier_dets, frame = process_bridge_piers(frame)
|
105 |
+
all_detected_items.extend(earthwork_dets + culvert_dets + bridge_pier_dets)
|
106 |
+
|
107 |
+
# Fallback: Run generic detection if no items detected
|
108 |
+
if not all_detected_items:
|
109 |
+
generic_dets, frame = process_generic(frame)
|
110 |
+
all_detected_items.extend(generic_dets)
|
111 |
+
|
112 |
+
# Optional: Run shadow detection (only log results for now)
|
113 |
+
shadow_results = detect_shadows(frame)
|
114 |
+
shadow_dets = shadow_results["detections"]
|
115 |
+
frame = shadow_results["frame"]
|
116 |
+
all_detected_items.extend(shadow_dets)
|
117 |
+
|
118 |
+
# Optional: Run thermal processing if frame is grayscale (simulated check)
|
119 |
+
if len(frame.shape) == 2: # Grayscale frame (simulating thermal input)
|
120 |
+
thermal_results = process_thermal(frame)
|
121 |
+
thermal_dets = thermal_results["detections"]
|
122 |
+
frame = thermal_results["frame"]
|
123 |
+
all_detected_items.extend(thermal_dets)
|
124 |
+
|
125 |
+
# Save frame with overlays
|
126 |
cv2.imwrite(TEMP_IMAGE_PATH, frame, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
|
127 |
+
metrics = update_metrics(all_detected_items)
|
128 |
|
129 |
# Simulate GPS coordinates
|
130 |
gps_coord = [17.385044 + random.uniform(-0.001, 0.001), 78.486671 + frame_count * 0.0001]
|
131 |
gps_coordinates.append(gps_coord)
|
132 |
|
133 |
# Save frame if cracks are detected
|
134 |
+
if any(item['type'] == 'crack' for item in crack_items):
|
135 |
captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"crack_{frame_count}.jpg")
|
136 |
cv2.imwrite(captured_frame_path, frame)
|
137 |
last_detected_images.append(captured_frame_path)
|
|
|
140 |
|
141 |
# Combine detections for Salesforce
|
142 |
all_detections = {
|
143 |
+
"items": all_detected_items,
|
144 |
"metrics": metrics,
|
145 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
146 |
"frame_count": frame_count,
|
|
|
160 |
last_detections = metrics
|
161 |
|
162 |
# Update logs and stats
|
163 |
+
crack_detected = len([item for item in crack_items if item['type'] == 'crack'])
|
164 |
crack_severity_all.extend([
|
165 |
item['severity']
|
166 |
+
for item in crack_items
|
167 |
if item['type'] == 'crack' and 'severity' in item
|
168 |
])
|
169 |
|
170 |
+
log_entries.append(f"{last_timestamp} - Frame {frame_count} - Cracks: {crack_detected} - Total Detections: {len(all_detected_items)} - GPS: {gps_coord} - Avg Conf: {metrics['avg_confidence']:.2f}")
|
171 |
crack_counts.append(crack_detected)
|
172 |
|
173 |
if len(log_entries) > 100:
|
|
|
285 |
load_button = gr.Button("Load Video")
|
286 |
video_status = gr.Textbox(label="Video Load Status", value="Please upload a video file or ensure 'sample.mp4' exists in the root directory.")
|
287 |
|
288 |
+
# Toggle for Under Construction services
|
289 |
+
with gr.Row():
|
290 |
+
under_construction_toggle = gr.Checkbox(label="Enable Under Construction Services", value=False)
|
291 |
+
toggle_status = gr.Textbox(label="Toggle Status", value="Under Construction Services: Disabled")
|
292 |
+
|
293 |
status_text = gr.Markdown("**Status:** 🟢 Ready (Upload a video to start)")
|
294 |
|
295 |
with gr.Row():
|
296 |
with gr.Column(scale=3):
|
297 |
video_output = gr.Image(label="Live Drone Feed", width=640, height=480)
|
298 |
with gr.Column(scale=1):
|
299 |
+
detections_output = gr.Textbox(label="Detection Metrics", lines=4)
|
300 |
|
301 |
with gr.Row():
|
302 |
logs_output = gr.Textbox(label="Live Logs", lines=8)
|
|
|
335 |
outputs=[video_status]
|
336 |
)
|
337 |
|
338 |
+
under_construction_toggle.change(
|
339 |
+
toggle_under_construction,
|
340 |
+
inputs=[under_construction_toggle],
|
341 |
+
outputs=[toggle_status]
|
342 |
+
)
|
343 |
+
|
344 |
pause_btn.click(toggle_pause, outputs=status_text)
|
345 |
resume_btn.click(toggle_resume, outputs=status_text)
|
346 |
frame_slider.change(set_frame_rate, inputs=[frame_slider])
|