Skip to content

About

Detects objects with a pretrained Mask R-CNN (COCO), cuts each one out and removes the background.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Object Detection and Background Removal with Mask R-CNN

Detects objects in pictures with a pretrained Mask R-CNN model, cuts each object out of its image and removes the background.

Results

detections.png is the output of the notebook run on the sample image 2516944023_d00345997d_z.jpg that comes with Mask_RCNN-TF2: five dogs (scores 0.998 to 1.000), two people (0.999 and 0.997) and a bench (0.917), each with a mask. The Screenshot *.png files are outputs from the original Colab run.

Detections

Run it

The code needs the older Keras 2 that ships with TensorFlow 2.12 or earlier. On TensorFlow 2.16 and later (Keras 3), it does not work, so on Colab, where TensorFlow is newer, you need a runtime with Python 3.10 or an older TensorFlow. The simplest route is a local Python 3.10 environment:

python3.10 -m venv venv && source venv/bin/activate
pip install -r requirements.txt
jupyter notebook Ahmed_Ramy_Maher.ipynb

Run the cells in order. The notebook clones Mask_RCNN-TF2, applies patch_mrcnn.py to it, downloads the COCO weights (about 257 MB), detects objects in the sample image, cuts each object out and saves detections.png.

What changed

Mask_RCNN-TF2 loads the weights through tensorflow.python.keras, but TensorFlow 2.11 and later build the layers with the separate keras package. The load silently did nothing, so the network ran with random weights and returned 100 detections, all class 79 with score 1.0. patch_mrcnn.py replaces that call with a direct read of the weights file. The notebook also had Colab-only /content paths and a google.colab import, which are fixed.

Requirements

Python 3.10, TensorFlow 2.12 and NumPy 1.23 (pinned in requirements.txt). A GPU is optional: on one CPU core the whole notebook, including the 257 MB weights download, takes about 30 seconds on the sample image.

Built with

Python, TensorFlow, OpenCV, scikit-image, Matplotlib, Mask R-CNN (mrcnn) with COCO weights.

License

MIT. See LICENSE.

About

Detects objects with a pretrained Mask R-CNN (COCO), cuts each one out and removes the background.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages