lokesh341 commited on
Commit
5ba778e
1 Parent(s): 48866b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -26,7 +26,8 @@ crack_severity_all = []
26
  last_frame = None
27
  last_metrics = {}
28
  last_timestamp = ""
29
- last_detected_images = [] # Store up to 100+ crack/hole images
 
30
  gps_coordinates = []
31
 
32
  # Constants
@@ -36,7 +37,7 @@ os.makedirs(CAPTURED_FRAMES_DIR, exist_ok=True)
36
 
37
  # Core monitor function
38
  def monitor_feed():
39
- global paused, frame_count, last_frame, last_metrics, last_timestamp, gps_coordinates, last_detected_images
40
 
41
  if paused and last_frame is not None:
42
  frame = last_frame.copy()
@@ -46,7 +47,7 @@ def monitor_feed():
46
  frame = get_next_video_frame()
47
  except RuntimeError as e:
48
  log_entries.append(f"Error: {str(e)}")
49
- return None, last_metrics, "\n".join(log_entries[-10:]), None, None, last_detected_images, None
50
 
51
  detected_items = detect_cracks_and_holes(frame) # Includes cracks and holes
52
  frame = overlay_boxes(frame, detected_items)
@@ -58,12 +59,19 @@ def monitor_feed():
58
  gps_coord = [17.385044 + random.uniform(-0.001, 0.001), 78.486671 + frame_count * 0.0001]
59
  gps_coordinates.append(gps_coord)
60
 
61
- if any(item['type'] in ['crack', 'hole'] for item in detected_items):
 
62
  captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"detected_{frame_count}.jpg")
63
  cv2.imwrite(captured_frame_path, frame)
64
- last_detected_images.append(captured_frame_path)
65
- if len(last_detected_images) > 100: # Store up to 100 images
66
- last_detected_images.pop(0)
 
 
 
 
 
 
67
 
68
  last_frame = frame.copy()
69
  last_metrics = metrics.copy()
@@ -93,7 +101,7 @@ def monitor_feed():
93
 
94
  map_path = generate_map(gps_coordinates[-5:], [item for item in last_metrics.get('items', []) if item['type'] in ['crack', 'hole']])
95
 
96
- return frame[:, :, ::-1], last_metrics, "\n".join(log_entries[-10:]), generate_line_chart(), generate_pie_chart(), last_detected_images, map_path
97
 
98
  # Line chart
99
  def generate_line_chart():
@@ -144,7 +152,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
144
 
145
  with gr.Row():
146
  map_output = gr.Image(label="Crack/Hole Locations Map")
147
- captured_images = gr.Gallery(label="Detected Cracks/Holes (Last 100+)", columns=4, rows=25)
 
 
148
 
149
  with gr.Row():
150
  pause_btn = gr.Button("鈴革笍 Pause")
@@ -171,14 +181,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
171
 
172
  def streaming_loop():
173
  while True:
174
- frame, metrics, logs, chart, pie, captured, map_path = monitor_feed()
175
  if frame is None:
176
- yield None, str(metrics), logs, chart, pie, captured, map_path
177
  else:
178
- yield frame, str(metrics), logs, chart, pie, captured, map_path
179
  time.sleep(frame_rate)
180
 
181
- app.load(streaming_loop, outputs=[video_output, metrics_output, logs_output, chart_output, pie_output, captured_images, map_output])
182
 
183
  if __name__ == "__main__":
184
  app.launch(share=True)
 
26
  last_frame = None
27
  last_metrics = {}
28
  last_timestamp = ""
29
+ last_detected_cracks = [] # Store up to 100+ crack images
30
+ last_detected_holes = [] # Store up to 100+ hole images
31
  gps_coordinates = []
32
 
33
  # Constants
 
37
 
38
  # Core monitor function
39
  def monitor_feed():
40
+ global paused, frame_count, last_frame, last_metrics, last_timestamp, gps_coordinates, last_detected_cracks, last_detected_holes
41
 
42
  if paused and last_frame is not None:
43
  frame = last_frame.copy()
 
47
  frame = get_next_video_frame()
48
  except RuntimeError as e:
49
  log_entries.append(f"Error: {str(e)}")
50
+ return None, last_metrics, "\n".join(log_entries[-10:]), None, None, last_detected_cracks, last_detected_holes, None
51
 
52
  detected_items = detect_cracks_and_holes(frame) # Includes cracks and holes
53
  frame = overlay_boxes(frame, detected_items)
 
59
  gps_coord = [17.385044 + random.uniform(-0.001, 0.001), 78.486671 + frame_count * 0.0001]
60
  gps_coordinates.append(gps_coord)
61
 
62
+ # Save detected cracks and holes separately
63
+ if detected_items:
64
  captured_frame_path = os.path.join(CAPTURED_FRAMES_DIR, f"detected_{frame_count}.jpg")
65
  cv2.imwrite(captured_frame_path, frame)
66
+ for item in detected_items:
67
+ if item['type'] == 'crack':
68
+ last_detected_cracks.append(captured_frame_path)
69
+ if len(last_detected_cracks) > 100:
70
+ last_detected_cracks.pop(0)
71
+ elif item['type'] == 'hole':
72
+ last_detected_holes.append(captured_frame_path)
73
+ if len(last_detected_holes) > 100:
74
+ last_detected_holes.pop(0)
75
 
76
  last_frame = frame.copy()
77
  last_metrics = metrics.copy()
 
101
 
102
  map_path = generate_map(gps_coordinates[-5:], [item for item in last_metrics.get('items', []) if item['type'] in ['crack', 'hole']])
103
 
104
+ return frame[:, :, ::-1], last_metrics, "\n".join(log_entries[-10:]), generate_line_chart(), generate_pie_chart(), last_detected_cracks, last_detected_holes, map_path
105
 
106
  # Line chart
107
  def generate_line_chart():
 
152
 
153
  with gr.Row():
154
  map_output = gr.Image(label="Crack/Hole Locations Map")
155
+ with gr.Column():
156
+ crack_images = gr.Gallery(label="Detected Cracks (Last 100+)", columns=4, rows=13)
157
+ hole_images = gr.Gallery(label="Detected Holes (Last 100+)", columns=4, rows=13)
158
 
159
  with gr.Row():
160
  pause_btn = gr.Button("鈴革笍 Pause")
 
181
 
182
  def streaming_loop():
183
  while True:
184
+ frame, metrics, logs, chart, pie, cracks, holes, map_path = monitor_feed()
185
  if frame is None:
186
+ yield None, str(metrics), logs, chart, pie, cracks, holes, map_path
187
  else:
188
+ yield frame, str(metrics), logs, chart, pie, cracks, holes, map_path
189
  time.sleep(frame_rate)
190
 
191
+ app.load(streaming_loop, outputs=[video_output, metrics_output, logs_output, chart_output, pie_output, crack_images, hole_images, map_output])
192
 
193
  if __name__ == "__main__":
194
  app.launch(share=True)