lokesh341 commited on
Commit
2f6c98b
·
verified ·
1 Parent(s): 800c95b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -21
app.py CHANGED
@@ -243,6 +243,7 @@ def monitor_feed() -> Tuple[
243
  metrics = last_metrics.copy()
244
  else:
245
  max_retries = 3
 
246
  for attempt in range(max_retries):
247
  try:
248
  if is_video:
@@ -282,6 +283,9 @@ def monitor_feed() -> Tuple[
282
  None
283
  )
284
 
 
 
 
285
  all_detected_items: List[Dict[str, Any]] = []
286
  shadow_issue = False
287
  thermal_flag = False
@@ -289,33 +293,33 @@ def monitor_feed() -> Tuple[
289
  try:
290
  # Process frame based on active service
291
  if active_service == "under_construction":
292
- earthwork_dets, frame = process_earthwork(frame)
293
- culvert_dets, frame = process_culverts(frame)
294
- bridge_pier_dets, frame = process_bridge_piers(frame)
295
  all_detected_items.extend(earthwork_dets + culvert_dets + bridge_pier_dets)
296
  elif active_service == "operations_maintenance":
297
- crack_hole_dets, frame = detect_cracks_and_holes(frame)
298
- pothole_dets, frame = process_potholes(frame)
299
- signage_dets, frame = process_signages(frame)
300
  all_detected_items.extend(crack_hole_dets + pothole_dets + signage_dets)
301
  elif active_service == "road_safety":
302
- barrier_dets, frame = process_barriers(frame)
303
- lighting_dets, frame = process_lighting(frame)
304
- accident_dets, frame = process_accident_spots(frame)
305
- pothole_crack_dets, frame = detect_potholes_and_cracks(frame)
306
  all_detected_items.extend(barrier_dets + lighting_dets + accident_dets + pothole_crack_dets)
307
  elif active_service == "plantation":
308
- plant_dets, frame = process_plants(frame)
309
- health_dets, frame = process_plant_health(frame)
310
- missing_dets, frame = process_missing_patches(frame)
311
  all_detected_items.extend(plant_dets + health_dets + missing_dets)
312
  else:
313
- generic_dets, frame = process_generic(frame)
314
  all_detected_items.extend(generic_dets)
315
 
316
  # Apply shadow detection
317
  try:
318
- cv2.imwrite(TEMP_IMAGE_PATH, frame)
319
  shadow_issue = detect_shadow_coverage(TEMP_IMAGE_PATH)
320
  except Exception as e:
321
  log_entries.append(f"Error saving temp image for shadow detection: {str(e)}")
@@ -323,14 +327,28 @@ def monitor_feed() -> Tuple[
323
  shadow_issue = False
324
 
325
  # Apply thermal processing if frame is grayscale
326
- if len(frame.shape) == 2:
327
- thermal_results = process_thermal(frame)
328
  thermal_dets = thermal_results["detections"]
329
- frame = thermal_results["frame"]
330
  all_detected_items.extend(thermal_dets)
331
  thermal_flag = bool(thermal_dets)
332
 
333
- # Overlay detections with distinct colors
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  for item in all_detected_items:
335
  box = item.get("box", [])
336
  if not box:
@@ -372,6 +390,10 @@ def monitor_feed() -> Tuple[
372
  gps_coord = [17.385044 + random.uniform(-0.001, 0.001), 78.486671 + frame_count * 0.0001]
373
  gps_coordinates.append(gps_coord)
374
 
 
 
 
 
375
  # Save frame if detections are present
376
  detection_types = {item.get("type") for item in all_detected_items if "type" in item}
377
  if detection_types:
@@ -436,6 +458,7 @@ def monitor_feed() -> Tuple[
436
  detected_counts.append(plant_detected + crack_detected + hole_detected + missing_detected)
437
 
438
  # Log frame processing details in the requested format
 
439
  detection_summary = {
440
  "timestamp": last_timestamp,
441
  "frame": frame_count,
@@ -443,7 +466,8 @@ def monitor_feed() -> Tuple[
443
  "cracks": crack_detected,
444
  "holes": hole_detected,
445
  "missing_patches": missing_detected,
446
- "gps": gps_coord
 
447
  }
448
  log_message = json.dumps(detection_summary, indent=2)
449
  log_entries.append(log_message)
@@ -613,4 +637,4 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="green"))
613
  app.load(streaming_loop, outputs=[media_output, metrics_output, logs_output, plant_images, issue_images, chart_output, map_output])
614
 
615
  if __name__ == "__main__":
616
- app.launch(share=False)
 
243
  metrics = last_metrics.copy()
244
  else:
245
  max_retries = 3
246
+ start_time = time.time()
247
  for attempt in range(max_retries):
248
  try:
249
  if is_video:
 
283
  None
284
  )
285
 
286
+ # Resize frame for faster detection (320x512)
287
+ detection_frame = cv2.resize(frame, (512, 320))
288
+
289
  all_detected_items: List[Dict[str, Any]] = []
290
  shadow_issue = False
291
  thermal_flag = False
 
293
  try:
294
  # Process frame based on active service
295
  if active_service == "under_construction":
296
+ earthwork_dets, detection_frame = process_earthwork(detection_frame)
297
+ culvert_dets, detection_frame = process_culverts(detection_frame)
298
+ bridge_pier_dets, detection_frame = process_bridge_piers(detection_frame)
299
  all_detected_items.extend(earthwork_dets + culvert_dets + bridge_pier_dets)
300
  elif active_service == "operations_maintenance":
301
+ crack_hole_dets, detection_frame = detect_cracks_and_holes(detection_frame)
302
+ pothole_dets, detection_frame = process_potholes(detection_frame)
303
+ signage_dets, detection_frame = process_signages(detection_frame)
304
  all_detected_items.extend(crack_hole_dets + pothole_dets + signage_dets)
305
  elif active_service == "road_safety":
306
+ barrier_dets, detection_frame = process_barriers(detection_frame)
307
+ lighting_dets, detection_frame = process_lighting(detection_frame)
308
+ accident_dets, detection_frame = process_accident_spots(detection_frame)
309
+ pothole_crack_dets, detection_frame = detect_potholes_and_cracks(detection_frame)
310
  all_detected_items.extend(barrier_dets + lighting_dets + accident_dets + pothole_crack_dets)
311
  elif active_service == "plantation":
312
+ plant_dets, detection_frame = process_plants(detection_frame)
313
+ health_dets, detection_frame = process_plant_health(detection_frame)
314
+ missing_dets, detection_frame = process_missing_patches(detection_frame)
315
  all_detected_items.extend(plant_dets + health_dets + missing_dets)
316
  else:
317
+ generic_dets, detection_frame = process_generic(detection_frame)
318
  all_detected_items.extend(generic_dets)
319
 
320
  # Apply shadow detection
321
  try:
322
+ cv2.imwrite(TEMP_IMAGE_PATH, detection_frame)
323
  shadow_issue = detect_shadow_coverage(TEMP_IMAGE_PATH)
324
  except Exception as e:
325
  log_entries.append(f"Error saving temp image for shadow detection: {str(e)}")
 
327
  shadow_issue = False
328
 
329
  # Apply thermal processing if frame is grayscale
330
+ if len(detection_frame.shape) == 2:
331
+ thermal_results = process_thermal(detection_frame)
332
  thermal_dets = thermal_results["detections"]
333
+ detection_frame = thermal_results["frame"]
334
  all_detected_items.extend(thermal_dets)
335
  thermal_flag = bool(thermal_dets)
336
 
337
+ # Scale bounding boxes back to original frame size
338
+ orig_h, orig_w = frame.shape[:2]
339
+ det_h, det_w = detection_frame.shape[:2]
340
+ scale_x, scale_y = orig_w / det_w, orig_h / det_h
341
+ for item in all_detected_items:
342
+ if "box" in item:
343
+ box = item["box"]
344
+ item["box"] = [
345
+ int(box[0] * scale_x),
346
+ int(box[1] * scale_y),
347
+ int(box[2] * scale_x),
348
+ int(box[3] * scale_y)
349
+ ]
350
+
351
+ # Overlay detections on the original frame
352
  for item in all_detected_items:
353
  box = item.get("box", [])
354
  if not box:
 
390
  gps_coord = [17.385044 + random.uniform(-0.001, 0.001), 78.486671 + frame_count * 0.0001]
391
  gps_coordinates.append(gps_coord)
392
 
393
+ # Add GPS to detected items for mapping
394
+ for item in all_detected_items:
395
+ item["gps"] = gps_coord
396
+
397
  # Save frame if detections are present
398
  detection_types = {item.get("type") for item in all_detected_items if "type" in item}
399
  if detection_types:
 
458
  detected_counts.append(plant_detected + crack_detected + hole_detected + missing_detected)
459
 
460
  # Log frame processing details in the requested format
461
+ processing_time = time.time() - start_time
462
  detection_summary = {
463
  "timestamp": last_timestamp,
464
  "frame": frame_count,
 
466
  "cracks": crack_detected,
467
  "holes": hole_detected,
468
  "missing_patches": missing_detected,
469
+ "gps": gps_coord,
470
+ "processing_time_ms": processing_time * 1000
471
  }
472
  log_message = json.dumps(detection_summary, indent=2)
473
  log_entries.append(log_message)
 
637
  app.load(streaming_loop, outputs=[media_output, metrics_output, logs_output, plant_images, issue_images, chart_output, map_output])
638
 
639
  if __name__ == "__main__":
640
+ app.launch(share=True) # Set share=True to create a public link