Serverless removal of images backgrounds with OpenCV, using an AWS Lambda.
Sample Human Image Input:
Sample Human Image Output:
OpenCV Background Removal on AWS Lambda uses a three step method to remove the background.
First, the python lambda function uses OpenCV's deep neural network (DNN) to identify areas of interest in the image. These areas are given as probability of being part of an object, a person or a dog for example. This results in a highly pixelated image if viewed.
Second, the area probabilities are inputed into the OpenCV GrabCut algorithm. GrabCut looks for edges to make more realistic cuts between the object and the background.
Finally, the image is smoothed using a Gaussian Blur.
Sample Dog Image Input:
Sample Dog Image Output:
- Click the "Image Background Removal Launch Stack" button:
This will bring you to either the Cloudformation UI or the AWS console if you are not signed in. Sign in, if you are not already. From the Cloudformation UI, click "Next" at the bottom of the screen. Repeat clicking "Next" on the two following pages. You will reach a page with this towards the bottom:
Checkmark the three "I acknowledgement" statements and select "Create Stack." This will start building the CloudFormation stack. It may take several minutes for CloudFormation to finish.
- Navigate to S3. You should see a bucket named "mask-rcnn-demo-xxxxxxxx", where "xxxxxxxx" is the unique stack identifier from CloudFormation. Open this bucket. You will see an "index.html" file. Open this file. This webpage will appear:
If you select the "Object Mask Options", you can control which object classes are used. The "Output Visualization Options" allows you to set a minimum confidence threshold for the OpenCV DNN model.
- Select a photo. Either select one of the stock photos or upload your own image. Select "Detect," if you uploaded an image or changed the image detection options.
- Congratulations! It's that easy.
OpenCV Background Removal on AWS Lambda returns a 32 bit png file. The png alpha channel is marked either transparent as 0 or visible as 255. The original image is stored in the RGB channels of the png file. This means you can visually restore sections of image by editing the alpha channel.
Any questions or suggestions, just add an "Issues" submission to this repository. Thanks.
Happy Coding!