# BackgroundRemover
![Background Remover](https://raw.githubusercontent.com/nadermx/backgroundremover/main/examplefiles/backgroundremoverexample.png)
<img alt="background remover video" src="https://raw.githubusercontent.com/nadermx/backgroundremover/main/examplefiles/backgroundremoverprocessed.gif" height="200" /><br>
BackgroundRemover is a command line tool to remove background from [image](https://github.com/nadermx/backgroundremover#image) and [video](https://github.com/nadermx/backgroundremover#video) using AI, made by [nadermx](https://john.nader.mx) to power [https://BackgroundRemoverAI.com](https://backgroundremoverai.com). If you wonder why it was made read this [short blog post](https://johnathannader.com/my-first-open-source-project/).<br>
### Requirements
* python >= 3.6
* python3.6-dev #or what ever version of python you use
* torch and torchvision stable version (https://pytorch.org)
* ffmpeg 4.4+
* To clarify, you must install both python and whatever dev version of python you installed. IE; python3.10-dev with python3.10 or python3.8-dev with python3.8
#### How to install torch and ffmpeg
Go to https://pytorch.org and scroll down to `INSTALL PYTORCH` section and follow the instructions.
For example:
```
PyTorch Build: Stable (1.7.1)
Your OS: Windows
Package: Pip
Language: Python
CUDA: None
```
To install ffmpeg and python-dev
```
sudo apt install ffmpeg python3.6-dev
```
### Installation
To Install backgroundremover, install it from pypi
```bash
pip install --upgrade pip
pip install backgroundremover
```
Please note that when you first run the program, it will check to see if you have the u2net models, if you do not, it will pull them from this repo
It is also possible to run this without installing it via pip, just clone the git to local start a virtual env and install requirements and run
```bash
python -m backgroundremover.cmd.cli -i "video.mp4" -mk -o "output.mov"
```
and for windows
```bash
python.exe -m backgroundremover.cmd.cli -i "video.mp4" -mk -o "output.mov"
```
### Installation using Docker
```bash
git clone https://github.com/nadermx/backgroundremover.git
cd backgroundremover
docker build -t bgremover .
alias backgroundremover='docker run -it --rm -v "$(pwd):/tmp" bgremover:latest'
```
### Usage as a cli
## Image
Remove the background from a local file image
```bash
backgroundremover -i "/path/to/image.jpeg" -o "output.png"
```
### Advance usage for image background removal
Sometimes it is possible to achieve better results by turning on alpha matting. Example:
```bash
backgroundremover -i "/path/to/image.jpeg" -a -ae 15 -o "output.png"
```
change the model for different background removal methods between `u2netp`, `u2net`, or `u2net_human_seg`
```bash
backgroundremover -i "/path/to/image.jpeg" -m "u2net_human_seg" -o "output.png"
```
## Video
### remove background from video and make transparent mov
```bash
backgroundremover -i "/path/to/video.mp4" -tv -o "output.mov"
```
### remove background from local video and overlay it over other video
```bash
backgroundremover -i "/path/to/video.mp4" -tov "/path/to/videtobeoverlayed.mp4" -o "output.mov"
```
### remove background from local video and overlay it over an image
```bash
backgroundremover -i "/path/to/video.mp4" -toi "/path/to/videtobeoverlayed.mp4" -o "output.mov"
```
### remove background from video and make transparent gif
```bash
backgroundremover -i "/path/to/video.mp4" -tg -o "output.gif"
```
### Make matte key file (green screen overlay)
Make a matte file for premiere
```bash
backgroundremover -i "/path/to/video.mp4" -mk -o "output.matte.mp4"
```
### Advance usage for video
Change the framerate of the video (default is set to 30)
```bash
backgroundremover -i "/path/to/video.mp4" -fr 30 -tv -o "output.mov"
```
Set total number of frames of the video (default is set to -1, ie the remove background from full video)
```bash
backgroundremover -i "/path/to/video.mp4" -fl 150 -tv -o "output.mov"
```
Change the gpu batch size of the video (default is set to 1)
```bash
backgroundremover -i "/path/to/video.mp4" -gb 4 -tv -o "output.mov"
```
Change the number of workers working on video (default is set to 1)
```bash
backgroundremover -i "/path/to/video.mp4" -wn 4 -tv -o "output.mov"
```
change the model for different background removal methods between `u2netp`, `u2net`, or `u2net_human_seg` and limit the frames to 150
```bash
backgroundremover -i "/path/to/video.mp4" -m "u2net_human_seg" -fl 150 -tv -o "output.mov"
```
## As a library
### Remove background image
```
from backgroundremover.bg import remove
def remove_bg(src_img_path, out_img_path):
model_choices = ["u2net", "u2net_human_seg", "u2netp"]
f = open(src_img_path, "rb")
data = f.read()
img = remove(data, model_name=model_choices[0],
alpha_matting=True,
alpha_matting_foreground_threshold=240,
alpha_matting_background_threshold=10,
alpha_matting_erode_structure_size=10,
alpha_matting_base_size=1000)
f.close()
f = open(out_img_path, "wb")
f.write(img)
f.close()
```
## Todo
- convert logic from video to image to utilize more GPU on image removal
- clean up documentation a bit more
- add ability to adjust and give feedback images or videos to datasets
- add ability to realtime background removal for videos, for streaming
- finish flask server api
- add ability to use other models than u2net, ie your own
- other
### Pull requests
Accepted
### If you like this library
Give a link to our project [BackgroundRemoverAI.com](https://backgroundremoverai.com) or this git, telling people that you like it or use it.
### Reason for project
We made it our own package after merging together parts of others, adding in a few features of our own via posting parts as bounty questions on superuser, etc. As well as asked on hackernews earlier to open source the image part, so decided to add in video, and a bit more.
### References
- https://arxiv.org/pdf/2005.09007.pdf
- https://github.com/NathanUA/U-2-Net
- https://github.com/pymatting/pymatting
- https://github.com/danielgatis/rembg
- https://github.com/ecsplendid/rembg-greenscreen
- https://superuser.com/questions/1647590/have-ffmpeg-merge-a-matte-key-file-over-the-normal-video-file-removing-the-backg
- https://superuser.com/questions/1648680/ffmpeg-alphamerge-two-videos-into-a-gif-with-transparent-background/1649339?noredirect=1#comment2522687_1649339
- https://superuser.com/questions/1649817/ffmpeg-overlay-a-video-after-alphamerging-two-others/1649856#1649856
### License
- Copyright (c) 2021-present [Johnathan Nader](https://github.com/nadermx)
- Copyright (c) 2020-present [Lucas Nestler](https://github.com/ClashLuke)
- Copyright (c) 2020-present [Dr. Tim Scarfe](https://github.com/ecsplendid)
- Copyright (c) 2020-present [Daniel Gatis](https://github.com/danielgatis)
Code Licensed under [MIT License](./LICENSE.txt)
Models Licensed under [Apache License 2.0](./models/license)
Raw data
{
"_id": null,
"home_page": "https://github.com/nadermx/backgroundremover",
"name": "backgroundremover",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.6",
"maintainer_email": null,
"keywords": "remove, background, u2net, remove background, background remover",
"author": "Johnathan Nader",
"author_email": "john@nader.mx",
"download_url": "https://files.pythonhosted.org/packages/4d/07/7b8ddc1cc56ffe0ab7519faf018b3d0d66753ccf306810b595d09def0c0a/backgroundremover-0.2.8.tar.gz",
"platform": null,
"description": "# BackgroundRemover\n![Background Remover](https://raw.githubusercontent.com/nadermx/backgroundremover/main/examplefiles/backgroundremoverexample.png)\n<img alt=\"background remover video\" src=\"https://raw.githubusercontent.com/nadermx/backgroundremover/main/examplefiles/backgroundremoverprocessed.gif\" height=\"200\" /><br>\nBackgroundRemover is a command line tool to remove background from [image](https://github.com/nadermx/backgroundremover#image) and [video](https://github.com/nadermx/backgroundremover#video) using AI, made by [nadermx](https://john.nader.mx) to power [https://BackgroundRemoverAI.com](https://backgroundremoverai.com). If you wonder why it was made read this [short blog post](https://johnathannader.com/my-first-open-source-project/).<br>\n\n\n### Requirements\n\n* python >= 3.6\n* python3.6-dev #or what ever version of python you use\n* torch and torchvision stable version (https://pytorch.org)\n* ffmpeg 4.4+\n\n* To clarify, you must install both python and whatever dev version of python you installed. IE; python3.10-dev with python3.10 or python3.8-dev with python3.8\n\n#### How to install torch and ffmpeg\n\nGo to https://pytorch.org and scroll down to `INSTALL PYTORCH` section and follow the instructions.\n\nFor example:\n\n```\nPyTorch Build: Stable (1.7.1)\nYour OS: Windows\nPackage: Pip\nLanguage: Python\nCUDA: None\n```\n\nTo install ffmpeg and python-dev\n\n```\nsudo apt install ffmpeg python3.6-dev\n```\n\n### Installation\nTo Install backgroundremover, install it from pypi\n\n```bash\npip install --upgrade pip\npip install backgroundremover\n```\nPlease note that when you first run the program, it will check to see if you have the u2net models, if you do not, it will pull them from this repo\n\nIt is also possible to run this without installing it via pip, just clone the git to local start a virtual env and install requirements and run\n```bash\npython -m backgroundremover.cmd.cli -i \"video.mp4\" -mk -o \"output.mov\"\n```\nand for windows\n```bash\npython.exe -m backgroundremover.cmd.cli -i \"video.mp4\" -mk -o \"output.mov\"\n```\n### Installation using Docker\n```bash\ngit clone https://github.com/nadermx/backgroundremover.git\ncd backgroundremover\ndocker build -t bgremover .\nalias backgroundremover='docker run -it --rm -v \"$(pwd):/tmp\" bgremover:latest'\n```\n### Usage as a cli\n## Image\n\nRemove the background from a local file image\n\n```bash\nbackgroundremover -i \"/path/to/image.jpeg\" -o \"output.png\"\n```\n\n### Advance usage for image background removal\n\nSometimes it is possible to achieve better results by turning on alpha matting. Example:\n\n```bash\nbackgroundremover -i \"/path/to/image.jpeg\" -a -ae 15 -o \"output.png\"\n```\nchange the model for different background removal methods between `u2netp`, `u2net`, or `u2net_human_seg`\n```bash\nbackgroundremover -i \"/path/to/image.jpeg\" -m \"u2net_human_seg\" -o \"output.png\"\n```\n## Video\n\n### remove background from video and make transparent mov\n\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -tv -o \"output.mov\"\n```\n### remove background from local video and overlay it over other video\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -tov \"/path/to/videtobeoverlayed.mp4\" -o \"output.mov\"\n```\n### remove background from local video and overlay it over an image\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -toi \"/path/to/videtobeoverlayed.mp4\" -o \"output.mov\"\n```\n\n### remove background from video and make transparent gif\n\n\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -tg -o \"output.gif\"\n```\n### Make matte key file (green screen overlay)\n\nMake a matte file for premiere\n\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -mk -o \"output.matte.mp4\"\n```\n\n### Advance usage for video\n\nChange the framerate of the video (default is set to 30)\n\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -fr 30 -tv -o \"output.mov\"\n```\n\nSet total number of frames of the video (default is set to -1, ie the remove background from full video)\n\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -fl 150 -tv -o \"output.mov\"\n```\n\nChange the gpu batch size of the video (default is set to 1)\n\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -gb 4 -tv -o \"output.mov\"\n```\n\nChange the number of workers working on video (default is set to 1)\n\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -wn 4 -tv -o \"output.mov\"\n```\nchange the model for different background removal methods between `u2netp`, `u2net`, or `u2net_human_seg` and limit the frames to 150\n```bash\nbackgroundremover -i \"/path/to/video.mp4\" -m \"u2net_human_seg\" -fl 150 -tv -o \"output.mov\"\n```\n\n## As a library\n### Remove background image\n\n```\nfrom backgroundremover.bg import remove\ndef remove_bg(src_img_path, out_img_path):\n model_choices = [\"u2net\", \"u2net_human_seg\", \"u2netp\"]\n f = open(src_img_path, \"rb\")\n data = f.read()\n img = remove(data, model_name=model_choices[0],\n alpha_matting=True,\n alpha_matting_foreground_threshold=240,\n alpha_matting_background_threshold=10,\n alpha_matting_erode_structure_size=10,\n alpha_matting_base_size=1000)\n f.close()\n f = open(out_img_path, \"wb\")\n f.write(img)\n f.close()\n```\n\n## Todo\n\n- convert logic from video to image to utilize more GPU on image removal\n- clean up documentation a bit more\n- add ability to adjust and give feedback images or videos to datasets\n- add ability to realtime background removal for videos, for streaming\n- finish flask server api\n- add ability to use other models than u2net, ie your own\n- other\n\n### Pull requests\n\nAccepted\n\n### If you like this library\n\nGive a link to our project [BackgroundRemoverAI.com](https://backgroundremoverai.com) or this git, telling people that you like it or use it.\n\n### Reason for project\n\nWe made it our own package after merging together parts of others, adding in a few features of our own via posting parts as bounty questions on superuser, etc. As well as asked on hackernews earlier to open source the image part, so decided to add in video, and a bit more.\n\n\n\n### References\n\n- https://arxiv.org/pdf/2005.09007.pdf\n- https://github.com/NathanUA/U-2-Net\n- https://github.com/pymatting/pymatting\n- https://github.com/danielgatis/rembg\n- https://github.com/ecsplendid/rembg-greenscreen\n- https://superuser.com/questions/1647590/have-ffmpeg-merge-a-matte-key-file-over-the-normal-video-file-removing-the-backg\n- https://superuser.com/questions/1648680/ffmpeg-alphamerge-two-videos-into-a-gif-with-transparent-background/1649339?noredirect=1#comment2522687_1649339\n- https://superuser.com/questions/1649817/ffmpeg-overlay-a-video-after-alphamerging-two-others/1649856#1649856\n\n### License\n\n- Copyright (c) 2021-present [Johnathan Nader](https://github.com/nadermx)\n- Copyright (c) 2020-present [Lucas Nestler](https://github.com/ClashLuke)\n- Copyright (c) 2020-present [Dr. Tim Scarfe](https://github.com/ecsplendid)\n- Copyright (c) 2020-present [Daniel Gatis](https://github.com/danielgatis)\n\nCode Licensed under [MIT License](./LICENSE.txt)\nModels Licensed under [Apache License 2.0](./models/license)\n",
"bugtrack_url": null,
"license": null,
"summary": "Background remover from image and video using AI",
"version": "0.2.8",
"project_urls": {
"Homepage": "https://github.com/nadermx/backgroundremover"
},
"split_keywords": [
"remove",
" background",
" u2net",
" remove background",
" background remover"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "4d077b8ddc1cc56ffe0ab7519faf018b3d0d66753ccf306810b595d09def0c0a",
"md5": "efbbe90071b0aed3f9354ffc4fe97cec",
"sha256": "95f3a1aac39abf5f3280bf2888716dd688429c3e318ea920967279a99fd7c694"
},
"downloads": -1,
"filename": "backgroundremover-0.2.8.tar.gz",
"has_sig": false,
"md5_digest": "efbbe90071b0aed3f9354ffc4fe97cec",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.6",
"size": 19494,
"upload_time": "2024-05-01T21:44:33",
"upload_time_iso_8601": "2024-05-01T21:44:33.781742Z",
"url": "https://files.pythonhosted.org/packages/4d/07/7b8ddc1cc56ffe0ab7519faf018b3d0d66753ccf306810b595d09def0c0a/backgroundremover-0.2.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-01 21:44:33",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nadermx",
"github_project": "backgroundremover",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "backgroundremover"
}