lolout1 commited on
Commit
ff5d871
·
1 Parent(s): 1637264

updating gradio_test imports

Browse files
Files changed (1) hide show
  1. gradio_test.py +5 -51
gradio_test.py CHANGED
@@ -208,7 +208,6 @@ class ImprovedBlackspotDetector:
208
  # Check overlap with floor mask
209
  overlap = blackspot_mask & floor_mask
210
  overlap_ratio = np.sum(overlap) / np.sum(blackspot_mask)
211
-
212
  if overlap_ratio < overlap_threshold:
213
  return False
214
 
@@ -217,14 +216,12 @@ class ImprovedBlackspotDetector:
217
  if len(blackspot_pixels) == 0:
218
  return False
219
 
220
- # Check if majority of pixels are floor-related classes
221
  unique_classes, counts = np.unique(blackspot_pixels, return_counts=True)
222
  floor_pixel_count = sum(
223
  counts[unique_classes == cls] for cls in self.floor_classes if cls in unique_classes
224
  )
225
  floor_ratio = floor_pixel_count / len(blackspot_pixels)
226
-
227
- return floor_ratio > 0.7 # At least 70% of blackspot should be on floor classes
228
 
229
  def filter_non_floor_blackspots(
230
  self,
@@ -251,10 +248,8 @@ class ImprovedBlackspotDetector:
251
  if self.predictor is None:
252
  raise RuntimeError("Blackspot detector not initialized")
253
 
254
- # Get original image dimensions
255
  original_h, original_w = image.shape[:2]
256
 
257
- # Ensure all masks have same dimensions
258
  if floor_prior is not None and floor_prior.shape != (original_h, original_w):
259
  floor_prior = cv2.resize(
260
  floor_prior.astype(np.uint8),
@@ -269,7 +264,6 @@ class ImprovedBlackspotDetector:
269
  interpolation=cv2.INTER_NEAREST
270
  )
271
 
272
- # Run detection
273
  try:
274
  outputs = self.predictor(image)
275
  instances = outputs["instances"].to("cpu")
@@ -280,17 +274,14 @@ class ImprovedBlackspotDetector:
280
  if len(instances) == 0:
281
  return self._empty_results(image)
282
 
283
- # Process results
284
  pred_classes = instances.pred_classes.numpy()
285
  pred_masks = instances.pred_masks.numpy()
286
  scores = instances.scores.numpy()
287
 
288
- # Separate floor and blackspot masks
289
  blackspot_indices = pred_classes == 1
290
  blackspot_masks = pred_masks[blackspot_indices] if np.any(blackspot_indices) else []
291
  blackspot_scores = scores[blackspot_indices] if np.any(blackspot_indices) else []
292
 
293
- # Create or use floor mask
294
  if floor_prior is not None:
295
  floor_mask = floor_prior
296
  else:
@@ -298,20 +289,16 @@ class ImprovedBlackspotDetector:
298
  for cls in self.floor_classes:
299
  floor_mask |= (segmentation == cls)
300
 
301
- # Filter blackspots to only those on floor surfaces
302
  filtered_blackspot_masks = self.filter_non_floor_blackspots(
303
  blackspot_masks, segmentation, floor_mask
304
  )
305
 
306
- # Combine filtered masks
307
  combined_blackspot = np.zeros(image.shape[:2], dtype=bool)
308
  for mask in filtered_blackspot_masks:
309
  combined_blackspot |= mask
310
 
311
- # Create visualizations
312
  visualization = self.create_visualization(image, floor_mask, combined_blackspot)
313
 
314
- # Calculate statistics
315
  floor_area = int(np.sum(floor_mask))
316
  blackspot_area = int(np.sum(combined_blackspot))
317
  coverage_percentage = (blackspot_area / floor_area * 100) if floor_area > 0 else 0
@@ -336,15 +323,12 @@ class ImprovedBlackspotDetector:
336
  """Create clear visualization of blackspots on floors only"""
337
  vis = image.copy()
338
 
339
- # Semi-transparent green overlay for floors
340
  floor_overlay = vis.copy()
341
  floor_overlay[floor_mask] = [0, 255, 0]
342
  vis = cv2.addWeighted(vis, 0.7, floor_overlay, 0.3, 0)
343
 
344
- # Bright red for blackspots
345
  vis[blackspot_mask] = [255, 0, 0]
346
 
347
- # Add contours for clarity
348
  blackspot_contours, _ = cv2.findContours(
349
  blackspot_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
350
  )
@@ -383,10 +367,8 @@ class NeuroNestApp:
383
  """Initialize all components"""
384
  logger.info("Initializing NeuroNest application...")
385
 
386
- # Initialize OneFormer
387
  oneformer_success = self.oneformer.initialize()
388
 
389
- # Initialize blackspot detector if model exists
390
  blackspot_success = False
391
  if os.path.exists(blackspot_model_path):
392
  self.blackspot_detector = ImprovedBlackspotDetector(blackspot_model_path)
@@ -411,7 +393,6 @@ class NeuroNestApp:
411
  return {"error": "Application not properly initialized"}
412
 
413
  try:
414
- # Load and preprocess image
415
  image = cv2.imread(image_path)
416
  if image is None:
417
  return {"error": "Could not load image"}
@@ -427,26 +408,20 @@ class NeuroNestApp:
427
  'statistics': {}
428
  }
429
 
430
- # 1. Semantic Segmentation
431
  logger.info("Running semantic segmentation...")
432
  seg_mask, seg_visualization = self.oneformer.semantic_segmentation(image_rgb)
433
-
434
  results['segmentation'] = {
435
  'visualization': seg_visualization,
436
  'mask': seg_mask
437
  }
438
 
439
- # Extract floor areas
440
  floor_prior = self.oneformer.extract_floor_areas(seg_mask)
441
 
442
- # 2. Blackspot Detection (improved to only detect on floors)
443
  if enable_blackspot and self.blackspot_detector is not None:
444
  logger.info("Running blackspot detection...")
445
  try:
446
- # Resize segmentation mask to match original image if needed
447
  h_orig, w_orig = image_rgb.shape[:2]
448
  h_seg, w_seg = seg_mask.shape
449
-
450
  if (h_seg, w_seg) != (h_orig, w_orig):
451
  seg_mask_resized = cv2.resize(
452
  seg_mask.astype(np.uint8),
@@ -465,14 +440,11 @@ class NeuroNestApp:
465
  logger.error(f"Error in blackspot detection: {e}")
466
  results['blackspot'] = None
467
 
468
- # 3. Universal Contrast Analysis
469
  if enable_contrast:
470
  logger.info("Running universal contrast analysis...")
471
  try:
472
- # Resize image to match segmentation size
473
  h_seg, w_seg = seg_mask.shape
474
  image_for_contrast = cv2.resize(image_rgb, (w_seg, h_seg))
475
-
476
  contrast_results = self.contrast_analyzer.analyze_contrast(
477
  image_for_contrast, seg_mask
478
  )
@@ -482,7 +454,6 @@ class NeuroNestApp:
482
  logger.error(f"Error in contrast analysis: {e}")
483
  results['contrast'] = None
484
 
485
- # 4. Generate combined statistics
486
  stats = self._generate_statistics(results)
487
  results['statistics'] = stats
488
 
@@ -499,7 +470,6 @@ class NeuroNestApp:
499
  """Generate comprehensive statistics"""
500
  stats = {}
501
 
502
- # Segmentation stats
503
  if results['segmentation']:
504
  unique_classes = np.unique(results['segmentation']['mask'])
505
  stats['segmentation'] = {
@@ -507,7 +477,6 @@ class NeuroNestApp:
507
  'image_size': results['segmentation']['mask'].shape
508
  }
509
 
510
- # Blackspot stats
511
  if results['blackspot']:
512
  bs = results['blackspot']
513
  stats['blackspot'] = {
@@ -518,7 +487,6 @@ class NeuroNestApp:
518
  'avg_confidence': bs['avg_confidence']
519
  }
520
 
521
- # Contrast stats
522
  if results['contrast']:
523
  cs = results['contrast']['statistics']
524
  stats['contrast'] = {
@@ -540,7 +508,6 @@ class NeuroNestApp:
540
  def create_gradio_interface():
541
  """Create the Gradio interface"""
542
 
543
- # Initialize the application
544
  app = NeuroNestApp()
545
  oneformer_ok, blackspot_ok = app.initialize()
546
 
@@ -556,7 +523,7 @@ def create_gradio_interface():
556
  ):
557
  """Wrapper function for Gradio interface"""
558
  if image_path is None:
559
- return None, None, None, None, "Please upload an image"
560
 
561
  results = app.analyze_image(
562
  image_path=image_path,
@@ -567,20 +534,17 @@ def create_gradio_interface():
567
  )
568
 
569
  if "error" in results:
570
- return None, None, None, None, f"Error: {results['error']}"
571
 
572
- # Extract outputs
573
  seg_output = results['segmentation']['visualization'] if results['segmentation'] else None
574
  blackspot_output = results['blackspot']['visualization'] if results['blackspot'] else None
575
  contrast_output = results['contrast']['visualization'] if results['contrast'] else None
576
 
577
- # Generate universal contrast report
578
  if results['contrast']:
579
  contrast_report = app.contrast_analyzer.generate_report(results['contrast'])
580
  else:
581
  contrast_report = "Contrast analysis not performed."
582
 
583
- # Generate blackspot analysis report
584
  if results['blackspot']:
585
  bs = results['blackspot']
586
  blackspot_report = (
@@ -593,9 +557,7 @@ def create_gradio_interface():
593
  else:
594
  blackspot_report = "Blackspot analysis not performed."
595
 
596
- # Generate full report
597
  report = generate_comprehensive_report(results, contrast_report, blackspot_report)
598
-
599
  return seg_output, blackspot_output, contrast_output, report
600
 
601
  def generate_comprehensive_report(results: Dict, contrast_report: str, blackspot_report: str) -> str:
@@ -603,7 +565,6 @@ def create_gradio_interface():
603
  report = ["# 🧠 NeuroNest Analysis Report\n"]
604
  report.append(f"*Generated: {time.strftime('%Y-%m-%d %H:%M:%S')}*\n")
605
 
606
- # Segmentation results
607
  if results['segmentation']:
608
  stats = results['statistics'].get('segmentation', {})
609
  report.append("## 🎯 Object Segmentation")
@@ -611,17 +572,14 @@ def create_gradio_interface():
611
  report.append(f"- **Resolution:** {stats.get('image_size', 'N/A')}")
612
  report.append("")
613
 
614
- # Blackspot results
615
  report.append("## ⚫ Blackspot Analysis")
616
  report.append(blackspot_report)
617
  report.append("")
618
 
619
- # Universal contrast analysis
620
  report.append("## 🎨 Universal Contrast Analysis")
621
  report.append(contrast_report)
622
  report.append("")
623
 
624
- # Recommendations
625
  report.append("## 📋 Recommendations for Alzheimer's Care")
626
 
627
  has_issues = False
@@ -648,7 +606,6 @@ def create_gradio_interface():
648
 
649
  return "\n".join(report)
650
 
651
- # Create the interface
652
  title = "🧠 NeuroNest: AI-Powered Environment Safety Analysis"
653
  description = """
654
  **Advanced visual analysis for Alzheimer's and dementia care environments**
@@ -662,7 +619,6 @@ def create_gradio_interface():
662
  """
663
 
664
  with gr.Blocks() as interface:
665
-
666
  gr.Markdown(f"# {title}")
667
  gr.Markdown(description)
668
 
@@ -695,13 +651,13 @@ def create_gradio_interface():
695
 
696
  # Next row: image upload and analyze button
697
  with gr.Row():
698
- with gr.Column(scale=1):
699
  image_input = gr.Image(
700
  label="📸 Upload Room Image",
701
  type="filepath",
702
  height=300
703
  )
704
- with gr.Column(scale=1, min_width=150):
705
  analyze_button = gr.Button(
706
  "🔍 Analyze Environment",
707
  variant="primary"
@@ -731,7 +687,6 @@ def create_gradio_interface():
731
  value="Upload an image and click 'Analyze Environment' to begin."
732
  )
733
 
734
- # Connect the interface
735
  analyze_button.click(
736
  fn=analyze_wrapper,
737
  inputs=[
@@ -749,7 +704,6 @@ def create_gradio_interface():
749
  ]
750
  )
751
 
752
- # Footer
753
  gr.Markdown("""
754
  ---
755
  **NeuroNest** v2.0 - Enhanced with floor-only blackspot detection and universal contrast analysis
 
208
  # Check overlap with floor mask
209
  overlap = blackspot_mask & floor_mask
210
  overlap_ratio = np.sum(overlap) / np.sum(blackspot_mask)
 
211
  if overlap_ratio < overlap_threshold:
212
  return False
213
 
 
216
  if len(blackspot_pixels) == 0:
217
  return False
218
 
 
219
  unique_classes, counts = np.unique(blackspot_pixels, return_counts=True)
220
  floor_pixel_count = sum(
221
  counts[unique_classes == cls] for cls in self.floor_classes if cls in unique_classes
222
  )
223
  floor_ratio = floor_pixel_count / len(blackspot_pixels)
224
+ return floor_ratio > 0.7 # At least 70% on floor classes
 
225
 
226
  def filter_non_floor_blackspots(
227
  self,
 
248
  if self.predictor is None:
249
  raise RuntimeError("Blackspot detector not initialized")
250
 
 
251
  original_h, original_w = image.shape[:2]
252
 
 
253
  if floor_prior is not None and floor_prior.shape != (original_h, original_w):
254
  floor_prior = cv2.resize(
255
  floor_prior.astype(np.uint8),
 
264
  interpolation=cv2.INTER_NEAREST
265
  )
266
 
 
267
  try:
268
  outputs = self.predictor(image)
269
  instances = outputs["instances"].to("cpu")
 
274
  if len(instances) == 0:
275
  return self._empty_results(image)
276
 
 
277
  pred_classes = instances.pred_classes.numpy()
278
  pred_masks = instances.pred_masks.numpy()
279
  scores = instances.scores.numpy()
280
 
 
281
  blackspot_indices = pred_classes == 1
282
  blackspot_masks = pred_masks[blackspot_indices] if np.any(blackspot_indices) else []
283
  blackspot_scores = scores[blackspot_indices] if np.any(blackspot_indices) else []
284
 
 
285
  if floor_prior is not None:
286
  floor_mask = floor_prior
287
  else:
 
289
  for cls in self.floor_classes:
290
  floor_mask |= (segmentation == cls)
291
 
 
292
  filtered_blackspot_masks = self.filter_non_floor_blackspots(
293
  blackspot_masks, segmentation, floor_mask
294
  )
295
 
 
296
  combined_blackspot = np.zeros(image.shape[:2], dtype=bool)
297
  for mask in filtered_blackspot_masks:
298
  combined_blackspot |= mask
299
 
 
300
  visualization = self.create_visualization(image, floor_mask, combined_blackspot)
301
 
 
302
  floor_area = int(np.sum(floor_mask))
303
  blackspot_area = int(np.sum(combined_blackspot))
304
  coverage_percentage = (blackspot_area / floor_area * 100) if floor_area > 0 else 0
 
323
  """Create clear visualization of blackspots on floors only"""
324
  vis = image.copy()
325
 
 
326
  floor_overlay = vis.copy()
327
  floor_overlay[floor_mask] = [0, 255, 0]
328
  vis = cv2.addWeighted(vis, 0.7, floor_overlay, 0.3, 0)
329
 
 
330
  vis[blackspot_mask] = [255, 0, 0]
331
 
 
332
  blackspot_contours, _ = cv2.findContours(
333
  blackspot_mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
334
  )
 
367
  """Initialize all components"""
368
  logger.info("Initializing NeuroNest application...")
369
 
 
370
  oneformer_success = self.oneformer.initialize()
371
 
 
372
  blackspot_success = False
373
  if os.path.exists(blackspot_model_path):
374
  self.blackspot_detector = ImprovedBlackspotDetector(blackspot_model_path)
 
393
  return {"error": "Application not properly initialized"}
394
 
395
  try:
 
396
  image = cv2.imread(image_path)
397
  if image is None:
398
  return {"error": "Could not load image"}
 
408
  'statistics': {}
409
  }
410
 
 
411
  logger.info("Running semantic segmentation...")
412
  seg_mask, seg_visualization = self.oneformer.semantic_segmentation(image_rgb)
 
413
  results['segmentation'] = {
414
  'visualization': seg_visualization,
415
  'mask': seg_mask
416
  }
417
 
 
418
  floor_prior = self.oneformer.extract_floor_areas(seg_mask)
419
 
 
420
  if enable_blackspot and self.blackspot_detector is not None:
421
  logger.info("Running blackspot detection...")
422
  try:
 
423
  h_orig, w_orig = image_rgb.shape[:2]
424
  h_seg, w_seg = seg_mask.shape
 
425
  if (h_seg, w_seg) != (h_orig, w_orig):
426
  seg_mask_resized = cv2.resize(
427
  seg_mask.astype(np.uint8),
 
440
  logger.error(f"Error in blackspot detection: {e}")
441
  results['blackspot'] = None
442
 
 
443
  if enable_contrast:
444
  logger.info("Running universal contrast analysis...")
445
  try:
 
446
  h_seg, w_seg = seg_mask.shape
447
  image_for_contrast = cv2.resize(image_rgb, (w_seg, h_seg))
 
448
  contrast_results = self.contrast_analyzer.analyze_contrast(
449
  image_for_contrast, seg_mask
450
  )
 
454
  logger.error(f"Error in contrast analysis: {e}")
455
  results['contrast'] = None
456
 
 
457
  stats = self._generate_statistics(results)
458
  results['statistics'] = stats
459
 
 
470
  """Generate comprehensive statistics"""
471
  stats = {}
472
 
 
473
  if results['segmentation']:
474
  unique_classes = np.unique(results['segmentation']['mask'])
475
  stats['segmentation'] = {
 
477
  'image_size': results['segmentation']['mask'].shape
478
  }
479
 
 
480
  if results['blackspot']:
481
  bs = results['blackspot']
482
  stats['blackspot'] = {
 
487
  'avg_confidence': bs['avg_confidence']
488
  }
489
 
 
490
  if results['contrast']:
491
  cs = results['contrast']['statistics']
492
  stats['contrast'] = {
 
508
  def create_gradio_interface():
509
  """Create the Gradio interface"""
510
 
 
511
  app = NeuroNestApp()
512
  oneformer_ok, blackspot_ok = app.initialize()
513
 
 
523
  ):
524
  """Wrapper function for Gradio interface"""
525
  if image_path is None:
526
+ return None, None, None, "Please upload an image"
527
 
528
  results = app.analyze_image(
529
  image_path=image_path,
 
534
  )
535
 
536
  if "error" in results:
537
+ return None, None, None, f"Error: {results['error']}"
538
 
 
539
  seg_output = results['segmentation']['visualization'] if results['segmentation'] else None
540
  blackspot_output = results['blackspot']['visualization'] if results['blackspot'] else None
541
  contrast_output = results['contrast']['visualization'] if results['contrast'] else None
542
 
 
543
  if results['contrast']:
544
  contrast_report = app.contrast_analyzer.generate_report(results['contrast'])
545
  else:
546
  contrast_report = "Contrast analysis not performed."
547
 
 
548
  if results['blackspot']:
549
  bs = results['blackspot']
550
  blackspot_report = (
 
557
  else:
558
  blackspot_report = "Blackspot analysis not performed."
559
 
 
560
  report = generate_comprehensive_report(results, contrast_report, blackspot_report)
 
561
  return seg_output, blackspot_output, contrast_output, report
562
 
563
  def generate_comprehensive_report(results: Dict, contrast_report: str, blackspot_report: str) -> str:
 
565
  report = ["# 🧠 NeuroNest Analysis Report\n"]
566
  report.append(f"*Generated: {time.strftime('%Y-%m-%d %H:%M:%S')}*\n")
567
 
 
568
  if results['segmentation']:
569
  stats = results['statistics'].get('segmentation', {})
570
  report.append("## 🎯 Object Segmentation")
 
572
  report.append(f"- **Resolution:** {stats.get('image_size', 'N/A')}")
573
  report.append("")
574
 
 
575
  report.append("## ⚫ Blackspot Analysis")
576
  report.append(blackspot_report)
577
  report.append("")
578
 
 
579
  report.append("## 🎨 Universal Contrast Analysis")
580
  report.append(contrast_report)
581
  report.append("")
582
 
 
583
  report.append("## 📋 Recommendations for Alzheimer's Care")
584
 
585
  has_issues = False
 
606
 
607
  return "\n".join(report)
608
 
 
609
  title = "🧠 NeuroNest: AI-Powered Environment Safety Analysis"
610
  description = """
611
  **Advanced visual analysis for Alzheimer's and dementia care environments**
 
619
  """
620
 
621
  with gr.Blocks() as interface:
 
622
  gr.Markdown(f"# {title}")
623
  gr.Markdown(description)
624
 
 
651
 
652
  # Next row: image upload and analyze button
653
  with gr.Row():
654
+ with gr.Column():
655
  image_input = gr.Image(
656
  label="📸 Upload Room Image",
657
  type="filepath",
658
  height=300
659
  )
660
+ with gr.Column():
661
  analyze_button = gr.Button(
662
  "🔍 Analyze Environment",
663
  variant="primary"
 
687
  value="Upload an image and click 'Analyze Environment' to begin."
688
  )
689
 
 
690
  analyze_button.click(
691
  fn=analyze_wrapper,
692
  inputs=[
 
704
  ]
705
  )
706
 
 
707
  gr.Markdown("""
708
  ---
709
  **NeuroNest** v2.0 - Enhanced with floor-only blackspot detection and universal contrast analysis