lokesh341 commited on
Commit
9ef90b7
·
1 Parent(s): c374261

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -25
app.py CHANGED
@@ -4,22 +4,12 @@ import time
4
  import os
5
  import json
6
  from datetime import datetime
7
- from services.video_service import get_next_video_frame, reset_video_index, preload_video
8
  # Under Construction services
9
  from services.under_construction.earthwork_detection import process_earthwork
10
  from services.under_construction.culvert_check import process_culverts
11
  from services.under_construction.bridge_pier_check import process_bridge_piers
12
- # Comment out other services
13
- # from services.operations_maintenance.pothole_detection import process_potholes
14
- # from services.operations_maintenance.crack_detection import process_cracks
15
- # from services.operations_maintenance.signage_check import process_signages
16
- # from services.road_safety.barrier_check import process_barriers
17
- # from services.road_safety.lighting_check import process_lighting
18
- # from services.road_safety.accident_spot_check import process_accident_spots
19
- # from services.plantation.plant_count import process_plants
20
- # from services.plantation.plant_health import process_plant_health
21
- # from services.plantation.missing_patch_check import process_missing_patches
22
- # Original services (not used in this mode but imported for potential future use)
23
  from services.detection_service import process_frame as process_generic
24
  from services.metrics_service import compute_metrics
25
  from services.overlay_service import add_overlay
@@ -27,12 +17,6 @@ from services.salesforce_dispatcher import dispatch_to_salesforce
27
  from services.shadow_detection import detect_shadows
28
  from services.thermal_service import process_thermal
29
 
30
- # Preload video
31
- try:
32
- preload_video()
33
- except Exception as e:
34
- print(f"Error preloading video: {str(e)}")
35
-
36
  # Globals
37
  paused = False
38
  frame_rate = 0.5 # Process every 0.5 seconds for real-time feel
@@ -41,17 +25,44 @@ log_entries = []
41
  last_frame = None
42
  last_detections = {}
43
  last_timestamp = ""
 
44
 
45
  # Constants
 
46
  TEMP_IMAGE_PATH = "temp.jpg"
47
  OUTPUT_DIR = "outputs"
48
  os.makedirs(OUTPUT_DIR, exist_ok=True)
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def monitor_feed():
51
  """
52
  Main function to process video frames in real-time.
53
  """
54
- global paused, frame_count, last_frame, last_detections, last_timestamp
 
 
 
 
55
 
56
  if paused and last_frame is not None:
57
  frame = last_frame.copy()
@@ -110,7 +121,13 @@ def monitor_feed():
110
  with gr.Blocks(theme=gr.themes.Soft()) as app:
111
  gr.Markdown("# 🛡️ NHAI Drone Analytics Monitoring System - Under Construction")
112
 
113
- status_text = gr.Markdown("**Status:** 🟢 Running")
 
 
 
 
 
 
114
 
115
  with gr.Row():
116
  with gr.Column(scale=3):
@@ -134,23 +151,37 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
134
  def toggle_resume():
135
  global paused
136
  paused = False
137
- return "**Status:** 🟢 Running"
138
 
139
  def set_frame_rate(val):
140
  global frame_rate
141
  frame_rate = val
142
 
 
 
 
 
 
 
 
 
 
 
143
  pause_btn.click(toggle_pause, outputs=status_text)
144
  resume_btn.click(toggle_resume, outputs=status_text)
145
  frame_slider.change(set_frame_rate, inputs=[frame_slider])
146
 
147
  def streaming_loop():
 
148
  while True:
149
- frame, detections, logs = monitor_feed()
150
- if frame is None:
151
- yield None, detections, logs
152
  else:
153
- yield frame, detections, logs
 
 
 
 
154
  time.sleep(frame_rate)
155
 
156
  app.load(streaming_loop, outputs=[video_output, detections_output, logs_output])
 
4
  import os
5
  import json
6
  from datetime import datetime
7
+ from services.video_service import get_next_video_frame, reset_video_index, preload_video, release_video
8
  # Under Construction services
9
  from services.under_construction.earthwork_detection import process_earthwork
10
  from services.under_construction.culvert_check import process_culverts
11
  from services.under_construction.bridge_pier_check import process_bridge_piers
12
+ # Original services
 
 
 
 
 
 
 
 
 
 
13
  from services.detection_service import process_frame as process_generic
14
  from services.metrics_service import compute_metrics
15
  from services.overlay_service import add_overlay
 
17
  from services.shadow_detection import detect_shadows
18
  from services.thermal_service import process_thermal
19
 
 
 
 
 
 
 
20
  # Globals
21
  paused = False
22
  frame_rate = 0.5 # Process every 0.5 seconds for real-time feel
 
25
  last_frame = None
26
  last_detections = {}
27
  last_timestamp = ""
28
+ video_loaded = False
29
 
30
  # Constants
31
+ DEFAULT_VIDEO_PATH = "sample.mp4"
32
  TEMP_IMAGE_PATH = "temp.jpg"
33
  OUTPUT_DIR = "outputs"
34
  os.makedirs(OUTPUT_DIR, exist_ok=True)
35
 
36
+ def initialize_video(video_file=None):
37
+ """
38
+ Initialize the video with the provided file or default path.
39
+ Args:
40
+ video_file: Uploaded video file (Gradio File object) or None.
41
+ Returns:
42
+ str: Status message
43
+ """
44
+ global video_loaded, log_entries
45
+ release_video() # Release any existing video capture
46
+ video_path = DEFAULT_VIDEO_PATH
47
+
48
+ if video_file is not None:
49
+ video_path = video_file.name # Gradio File object has a 'name' attribute with the temp path
50
+ log_entries.append(f"Using uploaded video: {video_path}")
51
+
52
+ status = preload_video(video_path)
53
+ video_loaded = "Error" not in status
54
+ log_entries.append(status)
55
+ return status
56
+
57
  def monitor_feed():
58
  """
59
  Main function to process video frames in real-time.
60
  """
61
+ global paused, frame_count, last_frame, last_detections, last_timestamp, video_loaded
62
+
63
+ if not video_loaded:
64
+ log_entries.append("Cannot start streaming: Video not loaded successfully.")
65
+ return None, json.dumps({"error": "Video not loaded. Please upload a video file."}, indent=2), "\n".join(log_entries[-10:])
66
 
67
  if paused and last_frame is not None:
68
  frame = last_frame.copy()
 
121
  with gr.Blocks(theme=gr.themes.Soft()) as app:
122
  gr.Markdown("# 🛡️ NHAI Drone Analytics Monitoring System - Under Construction")
123
 
124
+ # Video upload section
125
+ with gr.Row():
126
+ video_input = gr.File(label="Upload Video File (e.g., sample.mp4)", file_types=["video"])
127
+ load_button = gr.Button("Load Video")
128
+ video_status = gr.Textbox(label="Video Load Status", value="Please upload a video file or ensure 'sample.mp4' exists in the root directory.")
129
+
130
+ status_text = gr.Markdown("**Status:** 🟢 Ready (Upload a video to start)")
131
 
132
  with gr.Row():
133
  with gr.Column(scale=3):
 
151
  def toggle_resume():
152
  global paused
153
  paused = False
154
+ return "**Status:** 🟢 Streaming"
155
 
156
  def set_frame_rate(val):
157
  global frame_rate
158
  frame_rate = val
159
 
160
+ # Initialize video on app load (try default path)
161
+ video_status.value = initialize_video()
162
+
163
+ # Load video when user uploads a file
164
+ load_button.click(
165
+ initialize_video,
166
+ inputs=[video_input],
167
+ outputs=[video_status]
168
+ )
169
+
170
  pause_btn.click(toggle_pause, outputs=status_text)
171
  resume_btn.click(toggle_resume, outputs=status_text)
172
  frame_slider.change(set_frame_rate, inputs=[frame_slider])
173
 
174
  def streaming_loop():
175
+ global video_loaded
176
  while True:
177
+ if not video_loaded:
178
+ yield None, json.dumps({"error": "Video not loaded. Please upload a video file."}, indent=2), "\n".join(log_entries[-10:])
 
179
  else:
180
+ frame, detections, logs = monitor_feed()
181
+ if frame is None:
182
+ yield None, detections, logs
183
+ else:
184
+ yield frame, detections, logs
185
  time.sleep(frame_rate)
186
 
187
  app.load(streaming_loop, outputs=[video_output, detections_output, logs_output])