Coming soon..Sam2 https://github.com/facebookresearch/sam2/tree/main/sam2
https://github.com/facebookresearch/sam2/tree/main/sam2 This repository contains a Detectron2-based pipeline for segmenting floors in an image and subsequently detecting “blackspots” (dark rug, mat, or spill) only if they lie on top of the floor area. This can be especially useful for:
- Hazard detection: identifying wet or slippery areas on floors.
- Robotics: detecting irregular floor surfaces or potential obstacles.
- Assisted living: noticing spills or trip hazards in real time.
-
Two-Category Segmentation
- Floors (ID=0)
- blackspot (ID=1)
-
Unified Floor Category
If your dataset had multiple “floor” categories, we unify them into a single one. This ensures a single mask for all floor instances. -
Blackspot-on-Floor Overlap
After detecting floors and blackspots, the code checks for overlaps. Only blackspots overlapping with the floor region get flagged visually (drawn with bounding boxes). -
Mask R-CNN
- A powerful, state-of-the-art instance segmentation model (Detectron2).
- Outputs bounding boxes and segmentation masks.
-
COCO-Style Evaluation
- Uses mAP, AR, and other standard metrics via COCOEvaluator.
- Provides both bounding box and mask (segmentation) evaluation results.
Below is an example detection result.
A red bounding box highlights a “blackspot” region that overlaps with the floor:
(In this image, the black rug on the floor is automatically detected, and the bounding box is drawn only if the blackspot mask overlaps with the floor mask.)
-
Example.py(orUntitled.ipynb): Contains the entire pipeline to:- Load and unify the dataset (merging duplicate floor categories).
- Register the dataset in Detectron2.
- Configure and train Mask R-CNN on the new categories.
- Evaluate the model with COCO metrics.
- Run inference on a sample test image.
-
PostProcess.ipynb: Demonstrates how to post-process the model outputs to obtain bounding boxes, masks, etc. -
train/,valid/,test/: Example dataset folders containing images and COCO-format JSON annotations (_annotations.coco.json). -
output_floor_blackspot/: The default output directory where training artifacts (model checkpoints, logs, etc.) are saved. -
utils/: Utility scripts for data loading, annotation unification, etc. -
Floor Blackspot Detect.v3i.coco-segmentation.zip: The dataset (COCO segmentation style) if included.
-
Focus on Overlap
Many segmentation models can find floors and blackspots separately. Here, we specifically highlight blackspots only if they lie on a floor. This post-processing step (merging masks) reduces false positives (black areas not on the floor). -
Mask R-CNN’s Versatility
- It handles complex shapes (like irregular floor boundaries).
- The separate mask head provides higher accuracy for segmenting distinct objects (e.g., black rugs, spills).
-
Unified Floor Category
- Some datasets label floor categories inconsistently (e.g., “Wood Floor” = 0, “Tiled Floor” = 1).
- We merge them into one “Floors” category, simplifying training and inference.
-
COCO Metrics
- Standard, well-known metrics for measuring bounding box and mask AP across different IoU thresholds.
- Quick insight into how well the model generalizes to new floor types and blackspots.
-
Clone the Repository
git clone https://github.com/yourusername/BlackSpot_FloorDetection.git cd BlackSpot_FloorDetection -
Create (and activate) a Conda or Python Virtual Environment
conda create -n floor_blackspot python=3.10 -y conda activate floor_blackspot # or: python -m venv venv && source venv/bin/activate -
Install Dependencies
pip install -r requirements.txt
Ensure that Detectron2 is installed. If needed:
python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' -
Check GPU Access (optional)
If you have a CUDA-enabled device, verify with:python -c "import torch; print(torch.cuda.is_available())"If
True, the code will run withcfg.MODEL.DEVICE = "cuda:0".
-
COCO-Style Dataset
- Folders named
train/,valid/,test/with images inside. - Corresponding
_annotations.coco.jsonin each folder describing polygons (segmentation) for floors and blackspots.
- Folders named
-
Merging Floor Categories
- If your dataset has multiple floor IDs (e.g., 0 and 1 both labeled “Floors”), the scripts unify them into a single ID=0.
- The “blackspot” category gets ID=1.
-
Registering
register_unified_floors_dataset(name, json_file, image_root)calls an internal function that:- Loads your original JSON.
- Re-maps floor categories to ID=0 and blackspot to ID=1.
- Registers it with Detectron2’s
DatasetCatalog.
(All of this logic is in the example script.)
-
Set the Model Config
InExample.py(orUntitled.ipynb), we do:cfg = get_cfg() cfg.merge_from_file( model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") ) cfg.MODEL.ROI_HEADS.NUM_CLASSES = 2 # [Floors, blackspot] cfg.DATASETS.TRAIN = ("floors_train",) cfg.DATASETS.TEST = ("floors_val",) cfg.MODEL.DEVICE = "cuda:0" # or "cpu" ... trainer = DefaultTrainer(cfg) trainer.resume_or_load(resume=False) trainer.train()
-
Monitor Training
- Output logs appear in the console.
- By default, final weights get saved in
./output_floor_blackspot/.
-
COCOEvaluator
evaluator = COCOEvaluator("floors_test", cfg, False, output_dir=cfg.OUTPUT_DIR) test_loader = build_detection_test_loader(cfg, "floors_test") test_results = inference_on_dataset(predictor.model, test_loader, evaluator) print("Test metrics:", test_results)
- Gives bounding box AP, mask AP, and per-category results (Floors vs. blackspot).
-
Sample Output (truncated):
Average Precision (AP) [bbox] = 0.354 Average Precision (AP) [segm] = 0.353 ... Per-category AP for [bbox]: Floors: 55.50 blackspot: 15.39 ...This indicates floors are detected quite reliably, while blackspot detection AP is around 15–20%. Further data or training can improve blackspot performance.
Use the final trained model to detect blackspots over floors in any new image:
# Once training is done, load the final weights:
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # confidence threshold
predictor = DefaultPredictor(cfg)
# Inference on a test image
im = cv2.imread("path/to/your_image.jpg")
outputs = predictor(im)
instances = outputs["instances"]
# Separate floor masks vs. blackspot masks
floor_masks = []
blackspot_masks = []
for cls_id, m in zip(instances.pred_classes.cpu().numpy(),
instances.pred_masks.cpu().numpy()):
if cls_id == 0: # Floors
floor_masks.append(m)
else: # blackspot
blackspot_masks.append(m)
# Combine floor masks into one big floor region
combined_floor_mask = np.any(floor_masks, axis=0) if floor_masks else None
# Identify blackspot bounding boxes only where overlap with floor occurs
# (Implementation in the example script)
...When you display the result (using OpenCV or matplotlib), you’ll see bounding boxes only on blackspot regions that lie on top of the detected floor.
- Safety: Promptly detect unusual floor conditions in real-world environments.
- Accuracy: Mask R-CNN’s segmentation approach outperforms simple bounding-box detectors for tasks like mat/spill detection.
- Scalability: You can add more categories (like “objects on the floor”) if needed, but the pipeline stays the same.
-
Check Image Paths
- Make sure your
train/,valid/,test/folder structure and*_annotations.coco.jsonreference the correct image filenames.
- Make sure your
-
Multiple Floor Categories
- Confirm that your original annotation IDs for “Floors” unify into ID=0 (the script does this automatically).
-
Low Blackspot AP?
- You may need more training images with varied blackspots (lighting conditions, shapes, colors).
- Adjust
cfg.SOLVER.MAX_ITER, or tweak the learning rate and data augmentation.
-
GPU vs. CPU
- GPU is recommended (NVIDIA CUDA).
- If you only have CPU, training will be slower—make sure you change
cfg.MODEL.DEVICE = "cpu".
With this repository, you can:
- Train a Mask R-CNN model to identify floors vs. blackspots in a single pass.
- Highlight blackspots only if they lie on top of floors.
- Use standard COCO metrics to measure performance.

