diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..a7e35bd1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +* +!rembg +!setup.py +!setup.cfg +!requirements.txt +!requirements-cpu.txt +!requirements-gpu.txt +!versioneer.py +!README.md diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..7c939de1 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +rembg/_version.py export-subst diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..d196e755 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: [danielgatis] +custom: ["https://www.buymeacoffee.com/danielgatis"] diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..bfcd9587 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,33 @@ +--- +name: Bug report +about: Create a report to help us improve +title: "[BUG] ..." +labels: bug +assignees: "" +--- + +**Describe the bug** +A clear and concise description of what the bug is. + +**To Reproduce** +Steps to reproduce the behavior: + +1. Go to '...' +2. Click on '....' +3. Scroll down to '....' +4. See error + +**Expected behavior** +A clear and concise description of what you expected to happen. + +**Images** +Input images to reproduce. + +**OS Version:** +iOS 22 + +**Rembg version:** +v2.0.21 + +**Additional context** +Add any other context about the problem here. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..7443d876 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,19 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: "[FEATURE] ..." +labels: enhancement +assignees: "" +--- + +**Is your feature request related to a problem? Please describe.** +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** +Add any other context or screenshots about the feature request here. diff --git a/.github/workflows/close_inactive_issues.yml b/.github/workflows/close_inactive_issues.yml new file mode 100644 index 00000000..df118003 --- /dev/null +++ b/.github/workflows/close_inactive_issues.yml @@ -0,0 +1,23 @@ +name: Close inactive issues + +on: + schedule: + - cron: "30 1 * * *" + +jobs: + close-issues: + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + steps: + - uses: actions/stale@v5 + with: + days-before-issue-stale: 30 + days-before-issue-close: 14 + stale-issue-label: "stale" + stale-issue-message: "This issue is stale because it has been open for 30 days with no activity." + close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale." + days-before-pr-stale: -1 + days-before-pr-close: -1 + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/lint_python.yml b/.github/workflows/lint_python.yml new file mode 100644 index 00000000..ac8b27de --- /dev/null +++ b/.github/workflows/lint_python.yml @@ -0,0 +1,19 @@ +name: Lint + +on: [pull_request, push] + +jobs: + lint_python: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.11 + - name: Install dependencies + run: pip install .[cli,dev] + - run: mypy --install-types --non-interactive --ignore-missing-imports ./rembg + - run: bandit --recursive --skip B101,B104,B310,B311,B303,B110 --exclude ./rembg/_version.py ./rembg + - run: black --force-exclude rembg/_version.py --check --diff ./rembg + - run: flake8 ./rembg --count --ignore=B008,C901,E203,E266,E731,F401,F811,F841,W503 --max-line-length=120 --show-source --statistics --exclude ./rembg/_version.py + - run: isort --check-only --profile black ./rembg diff --git a/.github/workflows/publish_docker.yml b/.github/workflows/publish_docker.yml new file mode 100644 index 00000000..8f0e354c --- /dev/null +++ b/.github/workflows/publish_docker.yml @@ -0,0 +1,59 @@ +name: Publish Docker image + +on: + push: + branches: + - "main" + tags: + - "v*.*.*" + pull_request: + branches: + - "main" + +jobs: + push_to_registry: + name: Push Docker image to Docker Hub + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + # list of Docker images to use as base name for tags + images: | + ${{ secrets.DOCKER_HUB_USERNAME }}/rembg + # generate Docker tags based on the following events/attributes + tags: | + type=ref,event=branch + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}} + type=sha + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_HUB_USERNAME }} + password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: type=registry,ref=${{ secrets.DOCKER_HUB_USERNAME }}/rembg:buildcache + cache-to: type=registry,ref=${{ secrets.DOCKER_HUB_USERNAME }}/rembg:buildcache,mode=max diff --git a/.github/workflows/publish_pypi.yml b/.github/workflows/publish_pypi.yml new file mode 100644 index 00000000..eaaf4c92 --- /dev/null +++ b/.github/workflows/publish_pypi.yml @@ -0,0 +1,24 @@ +name: Publish to Pypi + +on: + push: + tags: + - "v*.*.*" + +jobs: + push_to_pypi: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: 3.11 + - name: Install dependencies + run: pip install .[cli,dev] + - name: Builds and uploads to PyPI + run: | + python3 setup.py sdist bdist_wheel + python3 -m twine upload dist/* + env: + TWINE_USERNAME: __token__ + TWINE_PASSWORD: ${{ secrets.PIPY_PASSWORD }} diff --git a/.github/workflows/test-install.yml b/.github/workflows/test-install.yml new file mode 100644 index 00000000..763f2a21 --- /dev/null +++ b/.github/workflows/test-install.yml @@ -0,0 +1,22 @@ +name: Test installation + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: pip install .[cli,dev] + - name: Test installation with pytest + run: | + pytest diff --git a/.gitignore b/.gitignore index e81f0842..40f8603c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,21 @@ # general things to ignore build/ dist/ +.venv/ +.direnv/ +*.spec *.egg-info/ *.egg *.py[cod] __pycache__/ *.so -*~ +*~≈ +.envrc +.python-version +.idea +.pytest_cache # due to using tox and pytest .tox .cache +.mypy_cache diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..760f494b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.10-slim + +WORKDIR /rembg + +COPY . . + +RUN python -m pip install -r requirements.txt +RUN python -m pip install ".[cli]" +RUN python -c 'from rembg.bg import download_models; download_models()' + +EXPOSE 5000 +ENTRYPOINT ["rembg"] +CMD ["s", "-t 8"] diff --git a/MANIFEST.in b/MANIFEST.in index 73b7b95f..800a885c 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,12 +1,10 @@ -include pyproject.toml - -# Include the README -include *.md - -# Include the license file +include MANIFEST.in include LICENSE.txt - -# Include the data files -recursive-include data * - +include README.md +include setup.py +include pyproject.toml include requirements.txt +include requirements-gpu.txt + +include versioneer.py +include rembg/_version.py diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..f120b6c3 --- /dev/null +++ b/Makefile @@ -0,0 +1,61 @@ +# keep help at the beginning - this will be used as default command +.description: Generate list of targets with descriptions +help: + @grep -oz '^.description: .*\n.*:' Makefile | sed 'N;s;\.description: \(.*\)\n\(.*\):;"\x1b[1\;32m make \2 \x1b[0m" "\1";' | xargs printf "%-50s - %s\n" + +# MACROS + +DOCKER = docker-compose -f docker-compose.development.yml +DOCKER_DEV = $(DOCKER) +CFLAGS='-Wno-warning' + +# use the rest as arguments for "run" +ARGS = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}` + +# COMMANDS + +# common +.description: start all services in DEVELOPMENT environment +start: + make stop backend + $(DOCKER_DEV) --profile all up -d + +.description: stop all app services. You can pass service name as an argument +stop: + $(DOCKER) --profile all stop $(call ARGS) + +.description: stop all services +down: + $(DOCKER) --profile all down + +.description: build docker images +build: + $(DOCKER_DEV) --profile all build + +.description: docker logs with follow option. You can pass service name as argument. +flogs: + $(DOCKER) logs --follow $(call ARGS) + +.description: docker logs. You can pass service name as argument. +logs: + $(DOCKER) logs $(call ARGS) + +.description: exec in the running service (pass service name and command as an argument). `make exec redis redis-cli` +exec: + $(DOCKER) exec $(call ARGS) + +.description: attaches to backend process stdin +attach: + docker attach --detach-keys=ctrl-c $$($(DOCKER_DEV) ps -q backend) + +.description: launches bash in backend container +bash: + $(DOCKER_DEV) exec backend bash + +# Ignore unknown targets +%: + @: + +# ...and turn them into do-nothing targets +_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) +$(eval $(_ARGS):dummy;@:) diff --git a/README.md b/README.md index 1efa470a..b5f695cc 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ - # Rembg -[![Downloads](https://pepy.tech/badge/rembg)](https://pepy.tech/project/rembg) -[![Downloads](https://pepy.tech/badge/rembg/month)](https://pepy.tech/project/rembg/month) -[![Downloads](https://pepy.tech/badge/rembg/week)](https://pepy.tech/project/rembg/week) +[![Downloads](https://img.shields.io/pypi/dm/rembg.svg)](https://img.shields.io/pypi/dm/rembg.svg) [![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://img.shields.io/badge/License-MIT-blue.svg) +[![Hugging Face Spaces](https://img.shields.io/badge/🤗%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces/KenjieDec/RemBG) +[![Streamlit App](https://img.shields.io/badge/🎈%20Streamlit%20Community-Cloud-blue)](https://bgremoval.streamlit.app/) + -Rembg is a tool to remove images background. That is it. +Rembg is a tool to remove images background.

@@ -35,98 +35,310 @@ Rembg is a tool to remove images background. That is it.

-### Installation +

+ + + + + + +

+ +**If this project has helped you, please consider making a [donation](https://www.buymeacoffee.com/danielgatis).** + +## Sponsor -Install it from pypi + + + + + +
+ + Unsplash + + + PhotoRoom Remove Background API +
+ https://photoroom.com/api +
+

+ Fast and accurate background remover API
+

+
+ +## Requirements + +``` +python: >3.7, <3.12 +``` + +## Installation + +CPU support: ```bash - pip install rembg +pip install rembg # for library +pip install rembg[cli] # for library + cli ``` -### Usage as a cli +GPU support: + +First of all, you need to check if your system supports the `onnxruntime-gpu`. + +Go to https://onnxruntime.ai and check the installation matrix. + +

+ +

+ +If yes, just run: -Remove the background from a remote image ```bash - curl -s http://input.png | rembg > output.png +pip install rembg[gpu] # for library +pip install rembg[gpu,cli] # for library + cli +``` + +## Usage as a cli + +After the installation step you can use rembg just typing `rembg` in your terminal window. + +The `rembg` command has 4 subcommands, one for each input type: +- `i` for files +- `p` for folders +- `s` for http server +- `b` for RGB24 pixel binary stream + +You can get help about the main command using: + +``` +rembg --help +``` + +As well, about all the subcommands using: + +``` +rembg --help +``` + +### rembg `i` + +Used when input and output are files. + +Remove the background from a remote image + +``` +curl -s http://input.png | rembg i > output.png ``` Remove the background from a local file -```bash - rembg -o path/to/output.png path/to/input.png + +``` +rembg i path/to/input.png path/to/output.png +``` + +Remove the background specifying a model + +``` +rembg i -m u2netp path/to/input.png path/to/output.png +``` + +Remove the background returning only the mask + +``` +rembg i -om path/to/input.png path/to/output.png +``` + + +Remove the background applying an alpha matting + +``` +rembg i -a path/to/input.png path/to/output.png +``` + +Passing extras parameters + +``` +rembg i -m sam -x '{"input_labels": [1], "input_points": [[100,100]]}' path/to/input.png path/to/output.png +``` + +``` +rembg i -m u2net_custom -x '{"model_path": "~/.u2net/u2net.onnx"}' path/to/input.png path/to/output.png ``` +### rembg `p` + +Used when input and output are folders. + Remove the background from all images in a folder -```bash - rembg -p path/to/inputs + +``` +rembg p path/to/input path/to/output ``` -### Usage as a server +Same as before, but watching for new/changed files to process + +``` +rembg p -w path/to/input path/to/output +``` + +### rembg `s` + +Used to start http server. + +To see the complete endpoints documentation, go to: `http://localhost:5000/api`. + +Remove the background from an image url -Start the server -```bash - rembg-server ``` +curl -s "http://localhost:5000/api/remove?url=http://input.png" -o output.png +``` + +Remove the background from an uploaded image -Open your browser to ``` - http://localhost:5000?url=http://image.png +curl -s -F file=@/path/to/input.jpg "http://localhost:5000/api/remove" -o output.png ``` -Also you can send the file as a FormData (multipart/form-data): +### rembg `b` + +Process a sequence of RGB24 images from stdin. This is intended to be used with another program, such as FFMPEG, that outputs RGB24 pixel data to stdout, which is piped into the stdin of this program, although nothing prevents you from manually typing in images at stdin. + ``` -
- - -
+rembg b image_width image_height -o output_specifier ``` -### Usage as a library +Arguments: + +- image_width : width of input image(s) +- image_height : height of input image(s) +- output_specifier: printf-style specifier for output filenames, for example if `output-%03u.png`, then output files will be named `output-000.png`, `output-001.png`, `output-002.png`, etc. Output files will be saved in PNG format regardless of the extension specified. You can omit it to write results to stdout. + +Example usage with FFMPEG: + +``` +ffmpeg -i input.mp4 -ss 10 -an -f rawvideo -pix_fmt rgb24 pipe:1 | rembg b 1280 720 -o folder/output-%03u.png +``` + +The width and height values must match the dimension of output images from FFMPEG. Note for FFMPEG, the "`-an -f rawvideo -pix_fmt rgb24 pipe:1`" part is required for the whole thing to work. + + +## Usage as a library + +Input and output as bytes + +```python +from rembg import remove + +input_path = 'input.png' +output_path = 'output.png' + +with open(input_path, 'rb') as i: + with open(output_path, 'wb') as o: + input = i.read() + output = remove(input) + o.write(output) +``` -In `app.py` +Input and output as a PIL image ```python - import sys - from rembg.bg import remove +from rembg import remove +from PIL import Image + +input_path = 'input.png' +output_path = 'output.png' - sys.stdout.buffer.write(remove(sys.stdin.buffer.read())) +input = Image.open(input_path) +output = remove(input) +output.save(output_path) ``` -Then run +Input and output as a numpy array + +```python +from rembg import remove +import cv2 + +input_path = 'input.png' +output_path = 'output.png' + +input = cv2.imread(input_path) +output = remove(input) +cv2.imwrite(output_path, output) ``` - cat input.png | python app.py > out.png + +How to iterate over files in a performatic way + +```python +from pathlib import Path +from rembg import remove, new_session + +session = new_session() + +for file in Path('path/to/folder').glob('*.png'): + input_path = str(file) + output_path = str(file.parent / (file.stem + ".out.png")) + + with open(input_path, 'rb') as i: + with open(output_path, 'wb') as o: + input = i.read() + output = remove(input, session=session) + o.write(output) ``` +To see a full list of examples on how to use rembg, go to the [examples](USAGE.md) page. +## Usage as a docker -### Advance usage +Just replace the `rembg` command for `docker run danielgatis/rembg`. -Sometimes it is possible to achieve better results by turning on alpha matting. Example: -```bash - curl -s http://input.png | rembg -a -ae 15 > output.png +Try this: + +``` +docker run danielgatis/rembg i path/to/input.png path/to/output.png ``` - - - - - - - - - - - - - - - -
OriginalWithout alpha mattingWith alpha matting (-a -ae 15)
+## Models + +All models are downloaded and saved in the user home folder in the `.u2net` directory. + +The available models are: + +- u2net ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx), [source](https://github.com/xuebinqin/U-2-Net)): A pre-trained model for general use cases. +- u2netp ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2netp.onnx), [source](https://github.com/xuebinqin/U-2-Net)): A lightweight version of u2net model. +- u2net_human_seg ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_human_seg.onnx), [source](https://github.com/xuebinqin/U-2-Net)): A pre-trained model for human segmentation. +- u2net_cloth_seg ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_cloth_seg.onnx), [source](https://github.com/levindabhi/cloth-segmentation)): A pre-trained model for Cloths Parsing from human portrait. Here clothes are parsed into 3 category: Upper body, Lower body and Full body. +- silueta ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/silueta.onnx), [source](https://github.com/xuebinqin/U-2-Net/issues/295)): Same as u2net but the size is reduced to 43Mb. +- isnet-general-use ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/isnet-general-use.onnx), [source](https://github.com/xuebinqin/DIS)): A new pre-trained model for general use cases. +- isnet-anime ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/isnet-anime.onnx), [source](https://github.com/SkyTNT/anime-segmentation)): A high-accuracy segmentation for anime character. +- sam ([download encoder](https://github.com/danielgatis/rembg/releases/download/v0.0.0/vit_b-encoder-quant.onnx), [download decoder](https://github.com/danielgatis/rembg/releases/download/v0.0.0/vit_b-decoder-quant.onnx), [source](https://github.com/facebookresearch/segment-anything)): A pre-trained model for any use cases. + +### How to train your own model -### References +If You need more fine tuned models try this: +https://github.com/danielgatis/rembg/issues/193#issuecomment-1055534289 + + +## Some video tutorials + +- https://www.youtube.com/watch?v=3xqwpXjxyMQ +- https://www.youtube.com/watch?v=dFKRGXdkGJU +- https://www.youtube.com/watch?v=Ai-BS_T7yjE +- https://www.youtube.com/watch?v=dFKRGXdkGJU +- https://www.youtube.com/watch?v=D7W-C0urVcQ + +## References - https://arxiv.org/pdf/2005.09007.pdf - https://github.com/NathanUA/U-2-Net - https://github.com/pymatting/pymatting -### License +## Buy me a coffee + +Liked some of my work? Buy me a coffee (or more likely a beer) + +Buy Me A Coffee + +## License Copyright (c) 2020-present [Daniel Gatis](https://github.com/danielgatis) diff --git a/USAGE.md b/USAGE.md new file mode 100644 index 00000000..8881f4e7 --- /dev/null +++ b/USAGE.md @@ -0,0 +1,71 @@ +# How to use the remove function + +## Load the Image +```python +from PIL import Image +from rembg import new_session, remove + +input_path = 'input.png' +output_path = 'output.png' + +input = Image.open(input_path) +``` +## Removing the background + +### Without additional arguments +This defaults to the `u2net` model. +```python +output = remove(input) +output.save(output_path) +``` + +### With a specific model +You can use the `new_session` function to create a session with a specific model. +```python +model_name = "isnet-general-use" +session = new_session(model_name) +output = remove(input, session=session) +``` + +### With alpha metting +Alpha metting is a post processing step that can be used to improve the quality of the output. +```python +output = remove(input, alpha_matting=True, alpha_matting_foreground_threshold=270,alpha_matting_background_threshold=20, alpha_matting_erode_size=11) +``` + +### Only mask +If you only want the mask, you can use the `only_mask` argument. +```python +output = remove(input, only_mask=True) +``` + +### With post processing +You can use the `post_process_mask` argument to post process the mask to get better results. +```python +output = remove(input, post_process_mask=True) +``` + +### Replacing the background color +You can use the `bgcolor` argument to replace the background color. +```python +output = remove(input, bgcolor=(255, 255, 255, 255)) +``` + +### Using input points +You can use the `input_points` and `input_labels` arguments to specify the points that should be used for the masks. This only works with the `sam` model. +```python +import numpy as np +# Define the points and labels +# The points are defined as [y, x] +input_points = np.array([[400, 350], [700, 400], [200, 400]]) +input_labels = np.array([1, 1, 2]) + +image = remove(image,session=session, input_points=input_points, input_labels=input_labels) +``` + +## Save the image +```python +output.save(output_path) +``` + + diff --git a/build-exe b/build-exe new file mode 100755 index 00000000..e791a393 --- /dev/null +++ b/build-exe @@ -0,0 +1,3 @@ +#!/bin/sh + +pyinstaller -y -p ./rembg rembg.py diff --git a/docker-compose.development.yml b/docker-compose.development.yml new file mode 100644 index 00000000..cb32af29 --- /dev/null +++ b/docker-compose.development.yml @@ -0,0 +1,12 @@ +services: + backend: + profiles: + - all + build: + context: . + tty: true + stdin_open: true + ports: + - "6100:5000" + environment: + OMP_NUM_THREADS: 4 diff --git a/examples/anime-girl-1.jpg b/examples/anime-girl-1.jpg new file mode 100644 index 00000000..5f404ece Binary files /dev/null and b/examples/anime-girl-1.jpg differ diff --git a/examples/anime-girl-1.out.png b/examples/anime-girl-1.out.png new file mode 100644 index 00000000..b4159337 Binary files /dev/null and b/examples/anime-girl-1.out.png differ diff --git a/examples/anime-girl-2.jpg b/examples/anime-girl-2.jpg new file mode 100644 index 00000000..21501d28 Binary files /dev/null and b/examples/anime-girl-2.jpg differ diff --git a/examples/anime-girl-2.out.png b/examples/anime-girl-2.out.png new file mode 100644 index 00000000..7cdae38a Binary files /dev/null and b/examples/anime-girl-2.out.png differ diff --git a/examples/anime-girl-3.jpg b/examples/anime-girl-3.jpg new file mode 100644 index 00000000..683d8f67 Binary files /dev/null and b/examples/anime-girl-3.jpg differ diff --git a/examples/anime-girl-3.out.png b/examples/anime-girl-3.out.png new file mode 100644 index 00000000..457c4d7d Binary files /dev/null and b/examples/anime-girl-3.out.png differ diff --git a/nginx.conf.sigil b/nginx.conf.sigil new file mode 100644 index 00000000..834983f3 --- /dev/null +++ b/nginx.conf.sigil @@ -0,0 +1,187 @@ +{{ range $port_map := .PROXY_PORT_MAP | split " " }} +{{ $port_map_list := $port_map | split ":" }} +{{ $scheme := index $port_map_list 0 }} +{{ $listen_port := index $port_map_list 1 }} +{{ $upstream_port := index $port_map_list 2 }} + +{{ if eq $scheme "http" }} +server { + listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }}; + listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }}; + {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; + error_log {{ $.NGINX_ERROR_LOG_PATH }}; +{{ if (and (eq $listen_port "80") ($.SSL_INUSE)) }} + include {{ $.DOKKU_ROOT }}/{{ $.APP }}/nginx.conf.d/*.conf; + location / { + return 301 https://$host:{{ $.PROXY_SSL_PORT }}$request_uri; + } +{{ else }} + location / { + + gzip on; + gzip_min_length 1100; + gzip_buffers 4 32k; + gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/wasm application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml; + gzip_vary on; + gzip_comp_level 6; + + proxy_pass http://{{ $.APP }}-{{ $upstream_port }}; + proxy_http_version 1.1; + proxy_read_timeout {{ $.PROXY_READ_TIMEOUT }}; + proxy_buffer_size {{ $.PROXY_BUFFER_SIZE }}; + proxy_buffering {{ $.PROXY_BUFFERING }}; + proxy_buffers {{ $.PROXY_BUFFERS }}; + proxy_busy_buffers_size {{ $.PROXY_BUSY_BUFFERS_SIZE }}; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $http_connection; + proxy_set_header Host $http_host; + proxy_set_header X-Forwarded-For {{ $.PROXY_X_FORWARDED_FOR }}; + proxy_set_header X-Forwarded-Port {{ $.PROXY_X_FORWARDED_PORT }}; + proxy_set_header X-Forwarded-Proto {{ $.PROXY_X_FORWARDED_PROTO }}; + proxy_set_header X-Request-Start $msec; + {{ if $.PROXY_X_FORWARDED_SSL }}proxy_set_header X-Forwarded-Ssl {{ $.PROXY_X_FORWARDED_SSL }};{{ end }} + } + + {{ if $.CLIENT_MAX_BODY_SIZE }}client_max_body_size {{ $.CLIENT_MAX_BODY_SIZE }};{{ end }} + + error_page 400 401 402 403 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 /400-error.html; + location /400-error.html { + root {{ $.DOKKU_LIB_ROOT }}/data/nginx-vhosts/dokku-errors; + internal; + } + + error_page 404 /404-error.html; + location /404-error.html { + root {{ $.DOKKU_LIB_ROOT }}/data/nginx-vhosts/dokku-errors; + internal; + } + + error_page 500 501 502 503 504 505 506 507 508 509 510 511 /500-error.html; + location /500-error.html { + root {{ $.DOKKU_LIB_ROOT }}/data/nginx-vhosts/dokku-errors; + internal; + } + include {{ $.DOKKU_ROOT }}/{{ $.APP }}/nginx.conf.d/*.conf; +{{ end }} +} +{{ else if eq $scheme "https"}} +server { + listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }} ssl {{ if eq $.HTTP2_SUPPORTED "true" }}http2{{ end }}; + listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }} ssl {{ if eq $.HTTP2_SUPPORTED "true" }}http2{{ end }}; + {{ if $.SSL_SERVER_NAME }}server_name {{ $.SSL_SERVER_NAME }}; {{ end }} + {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; + error_log {{ $.NGINX_ERROR_LOG_PATH }}; + + ssl_certificate {{ $.APP_SSL_PATH }}/server.crt; + ssl_certificate_key {{ $.APP_SSL_PATH }}/server.key; + ssl_protocols TLSv1.2 {{ if eq $.TLS13_SUPPORTED "true" }}TLSv1.3{{ end }}; + ssl_prefer_server_ciphers off; + + keepalive_timeout 70; + + location / { + + gzip on; + gzip_min_length 1100; + gzip_buffers 4 32k; + gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml application/rss+xml font/truetype application/x-font-ttf font/opentype application/vnd.ms-fontobject image/svg+xml; + gzip_vary on; + gzip_comp_level 6; + + proxy_pass http://{{ $.APP }}-{{ $upstream_port }}; + {{ if eq $.HTTP2_PUSH_SUPPORTED "true" }}http2_push_preload on; {{ end }} + proxy_http_version 1.1; + proxy_read_timeout {{ $.PROXY_READ_TIMEOUT }}; + proxy_buffer_size {{ $.PROXY_BUFFER_SIZE }}; + proxy_buffering {{ $.PROXY_BUFFERING }}; + proxy_buffers {{ $.PROXY_BUFFERS }}; + proxy_busy_buffers_size {{ $.PROXY_BUSY_BUFFERS_SIZE }}; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $http_connection; + proxy_set_header Host $http_host; + proxy_set_header X-Forwarded-For {{ $.PROXY_X_FORWARDED_FOR }}; + proxy_set_header X-Forwarded-Port {{ $.PROXY_X_FORWARDED_PORT }}; + proxy_set_header X-Forwarded-Proto {{ $.PROXY_X_FORWARDED_PROTO }}; + proxy_set_header X-Request-Start $msec; + {{ if $.PROXY_X_FORWARDED_SSL }}proxy_set_header X-Forwarded-Ssl {{ $.PROXY_X_FORWARDED_SSL }};{{ end }} + } + + {{ if $.CLIENT_MAX_BODY_SIZE }}client_max_body_size {{ $.CLIENT_MAX_BODY_SIZE }};{{ end }} + + error_page 400 401 402 403 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 /400-error.html; + location /400-error.html { + root {{ $.DOKKU_LIB_ROOT }}/data/nginx-vhosts/dokku-errors; + internal; + } + + error_page 404 /404-error.html; + location /404-error.html { + root {{ $.DOKKU_LIB_ROOT }}/data/nginx-vhosts/dokku-errors; + internal; + } + + error_page 500 501 503 504 505 506 507 508 509 510 511 /500-error.html; + location /500-error.html { + root {{ $.DOKKU_LIB_ROOT }}/data/nginx-vhosts/dokku-errors; + internal; + } + + error_page 502 /502-error.html; + location /502-error.html { + root {{ $.DOKKU_LIB_ROOT }}/data/nginx-vhosts/dokku-errors; + internal; + } + include {{ $.DOKKU_ROOT }}/{{ $.APP }}/nginx.conf.d/*.conf; +} +{{ else if eq $scheme "grpc"}} +{{ if eq $.GRPC_SUPPORTED "true"}}{{ if eq $.HTTP2_SUPPORTED "true"}} +server { + listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }} http2; + listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }} http2; + {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; + error_log {{ $.NGINX_ERROR_LOG_PATH }}; + location / { + grpc_pass grpc://{{ $.APP }}-{{ $upstream_port }}; + } + + {{ if $.CLIENT_MAX_BODY_SIZE }}client_max_body_size {{ $.CLIENT_MAX_BODY_SIZE }};{{ end }} + include {{ $.DOKKU_ROOT }}/{{ $.APP }}/nginx.conf.d/*.conf; +} +{{ end }}{{ end }} +{{ else if eq $scheme "grpcs"}} +{{ if eq $.GRPC_SUPPORTED "true"}}{{ if eq $.HTTP2_SUPPORTED "true"}} +server { + listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }} ssl http2; + listen {{ if $.NGINX_BIND_ADDRESS_IP4 }}{{ $.NGINX_BIND_ADDRESS_IP4 }}:{{end}}{{ $listen_port }} ssl http2; + {{ if $.NOSSL_SERVER_NAME }}server_name {{ $.NOSSL_SERVER_NAME }}; {{ end }} + access_log {{ $.NGINX_ACCESS_LOG_PATH }}{{ if and ($.NGINX_ACCESS_LOG_FORMAT) (ne $.NGINX_ACCESS_LOG_PATH "off") }} {{ $.NGINX_ACCESS_LOG_FORMAT }}{{ end }}; + error_log {{ $.NGINX_ERROR_LOG_PATH }}; + + ssl_certificate {{ $.APP_SSL_PATH }}/server.crt; + ssl_certificate_key {{ $.APP_SSL_PATH }}/server.key; + ssl_protocols TLSv1.2 {{ if eq $.TLS13_SUPPORTED "true" }}TLSv1.3{{ end }}; + ssl_prefer_server_ciphers off; + + location / { + grpc_pass grpc://{{ $.APP }}-{{ $upstream_port }}; + } + + {{ if $.CLIENT_MAX_BODY_SIZE }}client_max_body_size {{ $.CLIENT_MAX_BODY_SIZE }};{{ end }} + include {{ $.DOKKU_ROOT }}/{{ $.APP }}/nginx.conf.d/*.conf; +} +{{ end }}{{ end }} +{{ end }} +{{ end }} + +{{ if $.DOKKU_APP_WEB_LISTENERS }} +{{ range $upstream_port := $.PROXY_UPSTREAM_PORTS | split " " }} +upstream {{ $.APP }}-{{ $upstream_port }} { +{{ range $listeners := $.DOKKU_APP_WEB_LISTENERS | split " " }} +{{ $listener_list := $listeners | split ":" }} +{{ $listener_ip := index $listener_list 0 }} + server {{ $listener_ip }}:{{ $upstream_port }} max_conns=1;{{ end }} +} +{{ end }}{{ end }} diff --git a/onnxruntime-installation-matrix.png b/onnxruntime-installation-matrix.png new file mode 100644 index 00000000..674716a3 Binary files /dev/null and b/onnxruntime-installation-matrix.png differ diff --git a/pyproject.toml b/pyproject.toml index 6ddb34b8..449bf70f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,13 @@ [build-system] # These are the assumed default build requirements from pip: # https://pip.pypa.io/en/stable/reference/pip/#pep-517-and-518-support -requires = ["setuptools>=40.8.0", "wheel"] +requires = ["setuptools>=65.5.1", "wheel"] build-backend = "setuptools.build_meta" + +[versioneer] +VCS = "git" +style = "pep440" +versionfile_source = "rembg/_version.py" +versionfile_build = "rembg/_version.py" +tag_prefix = "v" +parentdir_prefix = "rembg-" diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..c24fe5bb --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::DeprecationWarning diff --git a/rembg.py b/rembg.py new file mode 100644 index 00000000..2f611b9f --- /dev/null +++ b/rembg.py @@ -0,0 +1,4 @@ +from rembg.cli import main + +if __name__ == "__main__": + main() diff --git a/rembg/__init__.py b/rembg/__init__.py new file mode 100644 index 00000000..26026af1 --- /dev/null +++ b/rembg/__init__.py @@ -0,0 +1,6 @@ +from . import _version + +__version__ = _version.get_versions()["version"] + +from .bg import remove +from .session_factory import new_session diff --git a/rembg/_version.py b/rembg/_version.py new file mode 100644 index 00000000..0bb29ed0 --- /dev/null +++ b/rembg/_version.py @@ -0,0 +1,677 @@ +# This file helps to compute a version number in source trees obtained from +# git-archive tarball (such as those provided by githubs download-from-tag +# feature). Distribution tarballs (built by setup.py sdist) and build +# directories (produced by setup.py build) will contain a much shorter file +# that just contains the computed version number. + +# This file is released into the public domain. Generated by +# versioneer-0.21 (https://github.com/python-versioneer/python-versioneer) + +"""Git implementation of _version.py.""" + +import errno +import os +import re +import subprocess +import sys +from typing import Callable, Dict + + +def get_keywords(): + """Get the keywords needed to look up the version information.""" + # these strings will be replaced by git during git-archive. + # setup.py/versioneer.py will grep for the variable names, so they must + # each be defined on a line of their own. _version.py will just call + # get_keywords(). + git_refnames = "$Format:%d$" + git_full = "$Format:%H$" + git_date = "$Format:%ci$" + keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} + return keywords + + +class VersioneerConfig: + """Container for Versioneer configuration parameters.""" + + +def get_config(): + """Create, populate and return the VersioneerConfig() object.""" + # these strings are filled in when 'setup.py versioneer' creates + # _version.py + cfg = VersioneerConfig() + cfg.VCS = "git" + cfg.style = "pep440" + cfg.tag_prefix = "v" + cfg.parentdir_prefix = "rembg-" + cfg.versionfile_source = "rembg/_version.py" + cfg.verbose = False + return cfg + + +class NotThisMethod(Exception): + """Exception raised if a method is not valid for the current scenario.""" + + +LONG_VERSION_PY: Dict[str, str] = {} +HANDLERS: Dict[str, Dict[str, Callable]] = {} + + +def register_vcs_handler(vcs, method): # decorator + """Create decorator to mark a method as the handler of a VCS.""" + + def decorate(f): + """Store f in HANDLERS[vcs][method].""" + if vcs not in HANDLERS: + HANDLERS[vcs] = {} + HANDLERS[vcs][method] = f + return f + + return decorate + + +def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): + """Call the given command(s).""" + assert isinstance(commands, list) + process = None + for command in commands: + try: + dispcmd = str([command] + args) + # remember shell=False, so use git.cmd on windows, not just git + process = subprocess.Popen( + [command] + args, + cwd=cwd, + env=env, + stdout=subprocess.PIPE, + stderr=(subprocess.PIPE if hide_stderr else None), + ) + break + except OSError: + e = sys.exc_info()[1] + if e.errno == errno.ENOENT: + continue + if verbose: + print("unable to run %s" % dispcmd) + print(e) + return None, None + else: + if verbose: + print("unable to find command, tried %s" % (commands,)) + return None, None + stdout = process.communicate()[0].strip().decode() + if process.returncode != 0: + if verbose: + print("unable to run %s (error)" % dispcmd) + print("stdout was %s" % stdout) + return None, process.returncode + return stdout, process.returncode + + +def versions_from_parentdir(parentdir_prefix, root, verbose): + """Try to determine the version from the parent directory name. + + Source tarballs conventionally unpack into a directory that includes both + the project name and a version string. We will also support searching up + two directory levels for an appropriately named parent directory + """ + rootdirs = [] + + for _ in range(3): + dirname = os.path.basename(root) + if dirname.startswith(parentdir_prefix): + return { + "version": dirname[len(parentdir_prefix) :], + "full-revisionid": None, + "dirty": False, + "error": None, + "date": None, + } + rootdirs.append(root) + root = os.path.dirname(root) # up a level + + if verbose: + print( + "Tried directories %s but none started with prefix %s" + % (str(rootdirs), parentdir_prefix) + ) + raise NotThisMethod("rootdir doesn't start with parentdir_prefix") + + +@register_vcs_handler("git", "get_keywords") +def git_get_keywords(versionfile_abs): + """Extract version information from the given file.""" + # the code embedded in _version.py can just fetch the value of these + # keywords. When used from setup.py, we don't want to import _version.py, + # so we do it with a regexp instead. This function is not used from + # _version.py. + keywords = {} + try: + with open(versionfile_abs, "r") as fobj: + for line in fobj: + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) + if line.strip().startswith("git_date ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["date"] = mo.group(1) + except OSError: + pass + return keywords + + +@register_vcs_handler("git", "keywords") +def git_versions_from_keywords(keywords, tag_prefix, verbose): + """Get version information from git keywords.""" + if "refnames" not in keywords: + raise NotThisMethod("Short version file found") + date = keywords.get("date") + if date is not None: + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + + # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant + # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 + # -like" string, which we must then edit to make compliant), because + # it's been around since git-1.5.3, and it's too difficult to + # discover which version we're using, or to work around using an + # older one. + date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) + refnames = keywords["refnames"].strip() + if refnames.startswith("$Format"): + if verbose: + print("keywords are unexpanded, not using") + raise NotThisMethod("unexpanded keywords, not a git-archive tarball") + refs = {r.strip() for r in refnames.strip("()").split(",")} + # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of + # just "foo-1.0". If we see a "tag: " prefix, prefer those. + TAG = "tag: " + tags = {r[len(TAG) :] for r in refs if r.startswith(TAG)} + if not tags: + # Either we're using git < 1.8.3, or there really are no tags. We use + # a heuristic: assume all version tags have a digit. The old git %d + # expansion behaves like git log --decorate=short and strips out the + # refs/heads/ and refs/tags/ prefixes that would let us distinguish + # between branches and tags. By ignoring refnames without digits, we + # filter out many common branch names like "release" and + # "stabilization", as well as "HEAD" and "master". + tags = {r for r in refs if re.search(r"\d", r)} + if verbose: + print("discarding '%s', no digits" % ",".join(refs - tags)) + if verbose: + print("likely tags: %s" % ",".join(sorted(tags))) + for ref in sorted(tags): + # sorting will prefer e.g. "2.0" over "2.0rc1" + if ref.startswith(tag_prefix): + r = ref[len(tag_prefix) :] + # Filter out refs that exactly match prefix or that don't start + # with a number once the prefix is stripped (mostly a concern + # when prefix is '') + if not re.match(r"\d", r): + continue + if verbose: + print("picking %s" % r) + return { + "version": r, + "full-revisionid": keywords["full"].strip(), + "dirty": False, + "error": None, + "date": date, + } + # no suitable tags, so version is "0+unknown", but full hex is still there + if verbose: + print("no suitable tags, using unknown + full revision id") + return { + "version": "0+unknown", + "full-revisionid": keywords["full"].strip(), + "dirty": False, + "error": "no suitable tags", + "date": None, + } + + +@register_vcs_handler("git", "pieces_from_vcs") +def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): + """Get version from 'git describe' in the root of the source tree. + + This only gets called if the git-archive 'subst' keywords were *not* + expanded, and _version.py hasn't already been rewritten with a short + version string, meaning we're inside a checked out source tree. + """ + GITS = ["git"] + TAG_PREFIX_REGEX = "*" + if sys.platform == "win32": + GITS = ["git.cmd", "git.exe"] + TAG_PREFIX_REGEX = r"\*" + + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) + if rc != 0: + if verbose: + print("Directory %s not under git control" % root) + raise NotThisMethod("'git rev-parse --git-dir' returned error") + + # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] + # if there isn't one, this yields HEX[-dirty] (no NUM) + describe_out, rc = runner( + GITS, + [ + "describe", + "--tags", + "--dirty", + "--always", + "--long", + "--match", + "%s%s" % (tag_prefix, TAG_PREFIX_REGEX), + ], + cwd=root, + ) + # --long was added in git-1.5.5 + if describe_out is None: + raise NotThisMethod("'git describe' failed") + describe_out = describe_out.strip() + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) + if full_out is None: + raise NotThisMethod("'git rev-parse' failed") + full_out = full_out.strip() + + pieces = {} + pieces["long"] = full_out + pieces["short"] = full_out[:7] # maybe improved later + pieces["error"] = None + + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], cwd=root) + # --abbrev-ref was added in git-1.6.3 + if rc != 0 or branch_name is None: + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") + branch_name = branch_name.strip() + + if branch_name == "HEAD": + # If we aren't exactly on a branch, pick a branch which represents + # the current commit. If all else fails, we are on a branchless + # commit. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) + # --contains was added in git-1.5.4 + if rc != 0 or branches is None: + raise NotThisMethod("'git branch --contains' returned error") + branches = branches.split("\n") + + # Remove the first line if we're running detached + if "(" in branches[0]: + branches.pop(0) + + # Strip off the leading "* " from the list of branches. + branches = [branch[2:] for branch in branches] + if "master" in branches: + branch_name = "master" + elif not branches: + branch_name = None + else: + # Pick the first branch that is returned. Good or bad. + branch_name = branches[0] + + pieces["branch"] = branch_name + + # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] + # TAG might have hyphens. + git_describe = describe_out + + # look for -dirty suffix + dirty = git_describe.endswith("-dirty") + pieces["dirty"] = dirty + if dirty: + git_describe = git_describe[: git_describe.rindex("-dirty")] + + # now we have TAG-NUM-gHEX or HEX + + if "-" in git_describe: + # TAG-NUM-gHEX + mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe) + if not mo: + # unparsable. Maybe git-describe is misbehaving? + pieces["error"] = "unable to parse git-describe output: '%s'" % describe_out + return pieces + + # tag + full_tag = mo.group(1) + if not full_tag.startswith(tag_prefix): + if verbose: + fmt = "tag '%s' doesn't start with prefix '%s'" + print(fmt % (full_tag, tag_prefix)) + pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % ( + full_tag, + tag_prefix, + ) + return pieces + pieces["closest-tag"] = full_tag[len(tag_prefix) :] + + # distance: number of commits since tag + pieces["distance"] = int(mo.group(2)) + + # commit: short hex revision ID + pieces["short"] = mo.group(3) + + else: + # HEX: no tags + pieces["closest-tag"] = None + count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) + pieces["distance"] = int(count_out) # total number of commits + + # commit date: see ISO-8601 comment in git_versions_from_keywords() + date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) + + return pieces + + +def plus_or_dot(pieces): + """Return a + if we don't already have one, else return a .""" + if "+" in pieces.get("closest-tag", ""): + return "." + return "+" + + +def render_pep440(pieces): + """Build up version string, with post-release "local version identifier". + + Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you + get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty + + Exceptions: + 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += plus_or_dot(pieces) + rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def render_pep440_branch(pieces): + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . + + The ".dev0" means not master branch. Note that .dev0 sorts backwards + (a feature branch will appear "older" than the master branch). + + Exceptions: + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0" + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def pep440_split_post(ver): + """Split pep440 version string at the post-release segment. + + Returns the release segments before the post-release and the + post-release version number (or -1 if no post-release segment is present). + """ + vc = str.split(ver, ".post") + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None + + +def render_pep440_pre(pieces): + """TAG[.postN.devDISTANCE] -- No -dirty. + + Exceptions: + 1: no tags. 0.post0.devDISTANCE + """ + if pieces["closest-tag"]: + if pieces["distance"]: + # update the post release segment + tag_version, post_version = pep440_split_post(pieces["closest-tag"]) + rendered = tag_version + if post_version is not None: + rendered += ".post%d.dev%d" % (post_version + 1, pieces["distance"]) + else: + rendered += ".post0.dev%d" % (pieces["distance"]) + else: + # no commits, use the tag as the version + rendered = pieces["closest-tag"] + else: + # exception #1 + rendered = "0.post0.dev%d" % pieces["distance"] + return rendered + + +def render_pep440_post(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX] . + + The ".dev0" means dirty. Note that .dev0 sorts backwards + (a dirty tree will appear "older" than the corresponding clean one), + but you shouldn't be releasing software with -dirty anyways. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%s" % pieces["short"] + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + rendered += "+g%s" % pieces["short"] + return rendered + + +def render_pep440_post_branch(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . + + The ".dev0" means not master branch. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def render_pep440_old(pieces): + """TAG[.postDISTANCE[.dev0]] . + + The ".dev0" means dirty. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + return rendered + + +def render_git_describe(pieces): + """TAG[-DISTANCE-gHEX][-dirty]. + + Like 'git describe --tags --dirty --always'. + + Exceptions: + 1: no tags. HEX[-dirty] (note: no 'g' prefix) + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"]: + rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) + else: + # exception #1 + rendered = pieces["short"] + if pieces["dirty"]: + rendered += "-dirty" + return rendered + + +def render_git_describe_long(pieces): + """TAG-DISTANCE-gHEX[-dirty]. + + Like 'git describe --tags --dirty --always -long'. + The distance/hash is unconditional. + + Exceptions: + 1: no tags. HEX[-dirty] (note: no 'g' prefix) + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) + else: + # exception #1 + rendered = pieces["short"] + if pieces["dirty"]: + rendered += "-dirty" + return rendered + + +def render(pieces, style): + """Render the given version pieces into the requested style.""" + if pieces["error"]: + return { + "version": "unknown", + "full-revisionid": pieces.get("long"), + "dirty": None, + "error": pieces["error"], + "date": None, + } + + if not style or style == "default": + style = "pep440" # the default + + if style == "pep440": + rendered = render_pep440(pieces) + elif style == "pep440-branch": + rendered = render_pep440_branch(pieces) + elif style == "pep440-pre": + rendered = render_pep440_pre(pieces) + elif style == "pep440-post": + rendered = render_pep440_post(pieces) + elif style == "pep440-post-branch": + rendered = render_pep440_post_branch(pieces) + elif style == "pep440-old": + rendered = render_pep440_old(pieces) + elif style == "git-describe": + rendered = render_git_describe(pieces) + elif style == "git-describe-long": + rendered = render_git_describe_long(pieces) + else: + raise ValueError("unknown style '%s'" % style) + + return { + "version": rendered, + "full-revisionid": pieces["long"], + "dirty": pieces["dirty"], + "error": None, + "date": pieces.get("date"), + } + + +def get_versions(): + """Get version information or return default if unable to do so.""" + # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have + # __file__, we can work backwards from there to the root. Some + # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which + # case we can only use expanded keywords. + + cfg = get_config() + verbose = cfg.verbose + + try: + return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, verbose) + except NotThisMethod: + pass + + try: + root = os.path.realpath(__file__) + # versionfile_source is the relative path from the top of the source + # tree (where the .git directory might live) to this file. Invert + # this to find the root from __file__. + for _ in cfg.versionfile_source.split("/"): + root = os.path.dirname(root) + except NameError: + return { + "version": "0+unknown", + "full-revisionid": None, + "dirty": None, + "error": "unable to find root of source tree", + "date": None, + } + + try: + pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) + return render(pieces, cfg.style) + except NotThisMethod: + pass + + try: + if cfg.parentdir_prefix: + return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) + except NotThisMethod: + pass + + return { + "version": "0+unknown", + "full-revisionid": None, + "dirty": None, + "error": "unable to compute version", + "date": None, + } diff --git a/rembg/bg.py b/rembg/bg.py new file mode 100644 index 00000000..cc4f3477 --- /dev/null +++ b/rembg/bg.py @@ -0,0 +1,213 @@ +import io +from enum import Enum +from typing import Any, List, Optional, Tuple, Union + +import numpy as np +from cv2 import ( + BORDER_DEFAULT, + MORPH_ELLIPSE, + MORPH_OPEN, + GaussianBlur, + getStructuringElement, + morphologyEx, +) +from PIL import Image, ImageOps +from PIL.Image import Image as PILImage +from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf +from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml +from pymatting.util.util import stack_images +from scipy.ndimage import binary_erosion + +from .session_factory import new_session +from .sessions import sessions_class +from .sessions.base import BaseSession + +kernel = getStructuringElement(MORPH_ELLIPSE, (3, 3)) + + +class ReturnType(Enum): + BYTES = 0 + PILLOW = 1 + NDARRAY = 2 + + +def alpha_matting_cutout( + img: PILImage, + mask: PILImage, + foreground_threshold: int, + background_threshold: int, + erode_structure_size: int, +) -> PILImage: + if img.mode == "RGBA" or img.mode == "CMYK": + img = img.convert("RGB") + + img = np.asarray(img) + mask = np.asarray(mask) + + is_foreground = mask > foreground_threshold + is_background = mask < background_threshold + + structure = None + if erode_structure_size > 0: + structure = np.ones( + (erode_structure_size, erode_structure_size), dtype=np.uint8 + ) + + is_foreground = binary_erosion(is_foreground, structure=structure) + is_background = binary_erosion(is_background, structure=structure, border_value=1) + + trimap = np.full(mask.shape, dtype=np.uint8, fill_value=128) + trimap[is_foreground] = 255 + trimap[is_background] = 0 + + img_normalized = img / 255.0 + trimap_normalized = trimap / 255.0 + + alpha = estimate_alpha_cf(img_normalized, trimap_normalized) + foreground = estimate_foreground_ml(img_normalized, alpha) + cutout = stack_images(foreground, alpha) + + cutout = np.clip(cutout * 255, 0, 255).astype(np.uint8) + cutout = Image.fromarray(cutout) + + return cutout + + +def naive_cutout(img: PILImage, mask: PILImage) -> PILImage: + empty = Image.new("RGBA", (img.size), 0) + cutout = Image.composite(img, empty, mask) + return cutout + + +def putalpha_cutout(img: PILImage, mask: PILImage) -> PILImage: + img.putalpha(mask) + return img + + +def get_concat_v_multi(imgs: List[PILImage]) -> PILImage: + pivot = imgs.pop(0) + for im in imgs: + pivot = get_concat_v(pivot, im) + return pivot + + +def get_concat_v(img1: PILImage, img2: PILImage) -> PILImage: + dst = Image.new("RGBA", (img1.width, img1.height + img2.height)) + dst.paste(img1, (0, 0)) + dst.paste(img2, (0, img1.height)) + return dst + + +def post_process(mask: np.ndarray) -> np.ndarray: + """ + Post Process the mask for a smooth boundary by applying Morphological Operations + Research based on paper: https://www.sciencedirect.com/science/article/pii/S2352914821000757 + args: + mask: Binary Numpy Mask + """ + mask = morphologyEx(mask, MORPH_OPEN, kernel) + mask = GaussianBlur(mask, (5, 5), sigmaX=2, sigmaY=2, borderType=BORDER_DEFAULT) + mask = np.where(mask < 127, 0, 255).astype(np.uint8) # convert again to binary + return mask + + +def apply_background_color(img: PILImage, color: Tuple[int, int, int, int]) -> PILImage: + r, g, b, a = color + colored_image = Image.new("RGBA", img.size, (r, g, b, a)) + colored_image.paste(img, mask=img) + + return colored_image + + +def fix_image_orientation(img: PILImage) -> PILImage: + return ImageOps.exif_transpose(img) + + +def download_models() -> None: + for session in sessions_class: + session.download_models() + + +def remove( + data: Union[bytes, PILImage, np.ndarray], + alpha_matting: bool = False, + alpha_matting_foreground_threshold: int = 240, + alpha_matting_background_threshold: int = 10, + alpha_matting_erode_size: int = 10, + session: Optional[BaseSession] = None, + only_mask: bool = False, + post_process_mask: bool = False, + bgcolor: Optional[Tuple[int, int, int, int]] = None, + *args: Optional[Any], + **kwargs: Optional[Any] +) -> Union[bytes, PILImage, np.ndarray]: + if isinstance(data, PILImage): + return_type = ReturnType.PILLOW + img = data + elif isinstance(data, bytes): + return_type = ReturnType.BYTES + img = Image.open(io.BytesIO(data)) + elif isinstance(data, np.ndarray): + return_type = ReturnType.NDARRAY + img = Image.fromarray(data) + else: + raise ValueError("Input type {} is not supported.".format(type(data))) + + putalpha = kwargs.pop("putalpha", False) + + # Fix image orientation + img = fix_image_orientation(img) + + if session is None: + session = new_session("u2net", *args, **kwargs) + + masks = session.predict(img, *args, **kwargs) + cutouts = [] + + for mask in masks: + if post_process_mask: + mask = Image.fromarray(post_process(np.array(mask))) + + if only_mask: + cutout = mask + + elif alpha_matting: + try: + cutout = alpha_matting_cutout( + img, + mask, + alpha_matting_foreground_threshold, + alpha_matting_background_threshold, + alpha_matting_erode_size, + ) + except ValueError: + if putalpha: + cutout = putalpha_cutout(img, mask) + else: + cutout = naive_cutout(img, mask) + else: + if putalpha: + cutout = putalpha_cutout(img, mask) + else: + cutout = naive_cutout(img, mask) + + cutouts.append(cutout) + + cutout = img + if len(cutouts) > 0: + cutout = get_concat_v_multi(cutouts) + + if bgcolor is not None and not only_mask: + cutout = apply_background_color(cutout, bgcolor) + + if ReturnType.PILLOW == return_type: + return cutout + + if ReturnType.NDARRAY == return_type: + return np.asarray(cutout) + + bio = io.BytesIO() + cutout.save(bio, "PNG") + bio.seek(0) + + return bio.read() diff --git a/rembg/cli.py b/rembg/cli.py new file mode 100644 index 00000000..6935dc9a --- /dev/null +++ b/rembg/cli.py @@ -0,0 +1,33 @@ +import pkg_resources + + +def main() -> None: + package_distribution = pkg_resources.get_distribution("rembg") + + for extra in package_distribution.extras: + if extra == "cli": + requirements = package_distribution.requires(extras=(extra,)) + for requirement in requirements: + try: + pkg_resources.require(requirement.project_name) + except pkg_resources.DistributionNotFound: + print(f"Missing dependency: '{requirement.project_name}'") + print( + "Please, install rembg with the cli feature: pip install rembg[cli]" + ) + exit(1) + + import click + + from . import _version + from .commands import command_functions + + @click.group() # type: ignore + @click.version_option(version=_version.get_versions()["version"]) + def _main() -> None: + pass + + for command in command_functions: + _main.add_command(command) # type: ignore + + _main() # type: ignore diff --git a/rembg/commands/__init__.py b/rembg/commands/__init__.py new file mode 100644 index 00000000..64f8993e --- /dev/null +++ b/rembg/commands/__init__.py @@ -0,0 +1,13 @@ +from importlib import import_module +from pathlib import Path +from pkgutil import iter_modules + +command_functions = [] + +package_dir = Path(__file__).resolve().parent +for _b, module_name, _p in iter_modules([str(package_dir)]): + module = import_module(f"{__name__}.{module_name}") + for attribute_name in dir(module): + attribute = getattr(module, attribute_name) + if attribute_name.endswith("_command"): + command_functions.append(attribute) diff --git a/rembg/commands/b_command.py b/rembg/commands/b_command.py new file mode 100644 index 00000000..69d2a62d --- /dev/null +++ b/rembg/commands/b_command.py @@ -0,0 +1,161 @@ +import asyncio +import io +import json +import os +import sys +from typing import IO + +import click +from PIL import Image + +from ..bg import remove +from ..session_factory import new_session +from ..sessions import sessions_names + + +@click.command( # type: ignore + name="b", + help="for a byte stream as input", +) +@click.option( + "-m", + "--model", + default="u2net", + type=click.Choice(sessions_names), + show_default=True, + show_choices=True, + help="model name", +) +@click.option( + "-a", + "--alpha-matting", + is_flag=True, + show_default=True, + help="use alpha matting", +) +@click.option( + "-af", + "--alpha-matting-foreground-threshold", + default=240, + type=int, + show_default=True, + help="trimap fg threshold", +) +@click.option( + "-ab", + "--alpha-matting-background-threshold", + default=10, + type=int, + show_default=True, + help="trimap bg threshold", +) +@click.option( + "-ae", + "--alpha-matting-erode-size", + default=10, + type=int, + show_default=True, + help="erode size", +) +@click.option( + "-om", + "--only-mask", + is_flag=True, + show_default=True, + help="output only the mask", +) +@click.option( + "-ppm", + "--post-process-mask", + is_flag=True, + show_default=True, + help="post process the mask", +) +@click.option( + "-bgc", + "--bgcolor", + default=None, + type=(int, int, int, int), + nargs=4, + help="Background color (R G B A) to replace the removed background with", +) +@click.option("-x", "--extras", type=str) +@click.option( + "-o", + "--output_specifier", + type=str, + help="printf-style specifier for output filenames (e.g. 'output-%d.png'))", +) +@click.argument( + "image_width", + type=int, +) +@click.argument( + "image_height", + type=int, +) +def rs_command( + model: str, + extras: str, + image_width: int, + image_height: int, + output_specifier: str, + **kwargs +) -> None: + try: + kwargs.update(json.loads(extras)) + except Exception: + pass + + session = new_session(model, **kwargs) + bytes_per_img = image_width * image_height * 3 + + if output_specifier: + output_dir = os.path.dirname( + os.path.abspath(os.path.expanduser(output_specifier)) + ) + + if not os.path.isdir(output_dir): + os.makedirs(output_dir, exist_ok=True) + + def img_to_byte_array(img: Image) -> bytes: + buff = io.BytesIO() + img.save(buff, format="PNG") + return buff.getvalue() + + async def connect_stdin_stdout(): + loop = asyncio.get_event_loop() + reader = asyncio.StreamReader() + protocol = asyncio.StreamReaderProtocol(reader) + + await loop.connect_read_pipe(lambda: protocol, sys.stdin) + w_transport, w_protocol = await loop.connect_write_pipe( + asyncio.streams.FlowControlMixin, sys.stdout + ) + + writer = asyncio.StreamWriter(w_transport, w_protocol, reader, loop) + return reader, writer + + async def main(): + reader, writer = await connect_stdin_stdout() + + idx = 0 + while True: + try: + img_bytes = await reader.readexactly(bytes_per_img) + if not img_bytes: + break + + img = Image.frombytes("RGB", (image_width, image_height), img_bytes) + output = remove(img, session=session, **kwargs) + + if output_specifier: + output.save((output_specifier % idx), format="PNG") + else: + writer.write(img_to_byte_array(output)) + + idx += 1 + except asyncio.IncompleteReadError: + break + + asyncio.run(main()) diff --git a/rembg/commands/i_command.py b/rembg/commands/i_command.py new file mode 100644 index 00000000..e96427f8 --- /dev/null +++ b/rembg/commands/i_command.py @@ -0,0 +1,93 @@ +import json +import sys +from typing import IO + +import click + +from ..bg import remove +from ..session_factory import new_session +from ..sessions import sessions_names + + +@click.command( # type: ignore + name="i", + help="for a file as input", +) +@click.option( + "-m", + "--model", + default="u2net", + type=click.Choice(sessions_names), + show_default=True, + show_choices=True, + help="model name", +) +@click.option( + "-a", + "--alpha-matting", + is_flag=True, + show_default=True, + help="use alpha matting", +) +@click.option( + "-af", + "--alpha-matting-foreground-threshold", + default=240, + type=int, + show_default=True, + help="trimap fg threshold", +) +@click.option( + "-ab", + "--alpha-matting-background-threshold", + default=10, + type=int, + show_default=True, + help="trimap bg threshold", +) +@click.option( + "-ae", + "--alpha-matting-erode-size", + default=10, + type=int, + show_default=True, + help="erode size", +) +@click.option( + "-om", + "--only-mask", + is_flag=True, + show_default=True, + help="output only the mask", +) +@click.option( + "-ppm", + "--post-process-mask", + is_flag=True, + show_default=True, + help="post process the mask", +) +@click.option( + "-bgc", + "--bgcolor", + default=None, + type=(int, int, int, int), + nargs=4, + help="Background color (R G B A) to replace the removed background with", +) +@click.option("-x", "--extras", type=str) +@click.argument( + "input", default=(None if sys.stdin.isatty() else "-"), type=click.File("rb") +) +@click.argument( + "output", + default=(None if sys.stdin.isatty() else "-"), + type=click.File("wb", lazy=True), +) +def i_command(model: str, extras: str, input: IO, output: IO, **kwargs) -> None: + try: + kwargs.update(json.loads(extras)) + except Exception: + pass + + output.write(remove(input.read(), session=new_session(model, **kwargs), **kwargs)) diff --git a/rembg/commands/p_command.py b/rembg/commands/p_command.py new file mode 100644 index 00000000..1e299985 --- /dev/null +++ b/rembg/commands/p_command.py @@ -0,0 +1,181 @@ +import json +import pathlib +import time +from typing import cast + +import click +import filetype +from tqdm import tqdm +from watchdog.events import FileSystemEvent, FileSystemEventHandler +from watchdog.observers import Observer + +from ..bg import remove +from ..session_factory import new_session +from ..sessions import sessions_names + + +@click.command( # type: ignore + name="p", + help="for a folder as input", +) +@click.option( + "-m", + "--model", + default="u2net", + type=click.Choice(sessions_names), + show_default=True, + show_choices=True, + help="model name", +) +@click.option( + "-a", + "--alpha-matting", + is_flag=True, + show_default=True, + help="use alpha matting", +) +@click.option( + "-af", + "--alpha-matting-foreground-threshold", + default=240, + type=int, + show_default=True, + help="trimap fg threshold", +) +@click.option( + "-ab", + "--alpha-matting-background-threshold", + default=10, + type=int, + show_default=True, + help="trimap bg threshold", +) +@click.option( + "-ae", + "--alpha-matting-erode-size", + default=10, + type=int, + show_default=True, + help="erode size", +) +@click.option( + "-om", + "--only-mask", + is_flag=True, + show_default=True, + help="output only the mask", +) +@click.option( + "-ppm", + "--post-process-mask", + is_flag=True, + show_default=True, + help="post process the mask", +) +@click.option( + "-w", + "--watch", + default=False, + is_flag=True, + show_default=True, + help="watches a folder for changes", +) +@click.option( + "-bgc", + "--bgcolor", + default=None, + type=(int, int, int, int), + nargs=4, + help="Background color (R G B A) to replace the removed background with", +) +@click.option("-x", "--extras", type=str) +@click.argument( + "input", + type=click.Path( + exists=True, + path_type=pathlib.Path, + file_okay=False, + dir_okay=True, + readable=True, + ), +) +@click.argument( + "output", + type=click.Path( + exists=False, + path_type=pathlib.Path, + file_okay=False, + dir_okay=True, + writable=True, + ), +) +def p_command( + model: str, + extras: str, + input: pathlib.Path, + output: pathlib.Path, + watch: bool, + **kwargs, +) -> None: + try: + kwargs.update(json.loads(extras)) + except Exception: + pass + + session = new_session(model, **kwargs) + + def process(each_input: pathlib.Path) -> None: + try: + mimetype = filetype.guess(each_input) + if mimetype is None: + return + if mimetype.mime.find("image") < 0: + return + + each_output = (output / each_input.name).with_suffix(".png") + each_output.parents[0].mkdir(parents=True, exist_ok=True) + + if not each_output.exists(): + each_output.write_bytes( + cast( + bytes, + remove(each_input.read_bytes(), session=session, **kwargs), + ) + ) + + if watch: + print( + f"processed: {each_input.absolute()} -> {each_output.absolute()}" + ) + except Exception as e: + print(e) + + inputs = list(input.glob("**/*")) + if not watch: + inputs = tqdm(inputs) + + for each_input in inputs: + if not each_input.is_dir(): + process(each_input) + + if watch: + observer = Observer() + + class EventHandler(FileSystemEventHandler): + def on_any_event(self, event: FileSystemEvent) -> None: + if not ( + event.is_directory or event.event_type in ["deleted", "closed"] + ): + process(pathlib.Path(event.src_path)) + + event_handler = EventHandler() + observer.schedule(event_handler, input, recursive=False) + observer.start() + + try: + while True: + time.sleep(1) + + finally: + observer.stop() + observer.join() diff --git a/rembg/commands/s_command.py b/rembg/commands/s_command.py new file mode 100644 index 00000000..b5147ec9 --- /dev/null +++ b/rembg/commands/s_command.py @@ -0,0 +1,282 @@ +import json +import os +import webbrowser +from typing import Optional, Tuple, cast + +import aiohttp +import click +import gradio as gr +import uvicorn +from asyncer import asyncify +from fastapi import Depends, FastAPI, File, Form, Query +from fastapi.middleware.cors import CORSMiddleware +from starlette.responses import Response + +from .._version import get_versions +from ..bg import remove +from ..session_factory import new_session +from ..sessions import sessions_names +from ..sessions.base import BaseSession + + +@click.command( # type: ignore + name="s", + help="for a http server", +) +@click.option( + "-p", + "--port", + default=5000, + type=int, + show_default=True, + help="port", +) +@click.option( + "-l", + "--log_level", + default="info", + type=str, + show_default=True, + help="log level", +) +@click.option( + "-t", + "--threads", + default=None, + type=int, + show_default=True, + help="number of worker threads", +) +def s_command(port: int, log_level: str, threads: int) -> None: + sessions: dict[str, BaseSession] = {} + tags_metadata = [ + { + "name": "Background Removal", + "description": "Endpoints that perform background removal with different image sources.", + "externalDocs": { + "description": "GitHub Source", + "url": "https://github.com/danielgatis/rembg", + }, + }, + ] + app = FastAPI( + title="Rembg", + description="Rembg is a tool to remove images background. That is it.", + version=get_versions()["version"], + contact={ + "name": "Daniel Gatis", + "url": "https://github.com/danielgatis", + "email": "danielgatis@gmail.com", + }, + license_info={ + "name": "MIT License", + "url": "https://github.com/danielgatis/rembg/blob/main/LICENSE.txt", + }, + openapi_tags=tags_metadata, + docs_url="/api", + ) + + app.add_middleware( + CORSMiddleware, + allow_credentials=True, + allow_origins=["*"], + allow_methods=["*"], + allow_headers=["*"], + ) + + class CommonQueryParams: + def __init__( + self, + model: str = Query( + description="Model to use when processing image", + regex=r"(" + "|".join(sessions_names) + ")", + default="u2net", + ), + a: bool = Query(default=False, description="Enable Alpha Matting"), + af: int = Query( + default=240, + ge=0, + le=255, + description="Alpha Matting (Foreground Threshold)", + ), + ab: int = Query( + default=10, + ge=0, + le=255, + description="Alpha Matting (Background Threshold)", + ), + ae: int = Query( + default=10, ge=0, description="Alpha Matting (Erode Structure Size)" + ), + om: bool = Query(default=False, description="Only Mask"), + ppm: bool = Query(default=False, description="Post Process Mask"), + bgc: Optional[str] = Query(default=None, description="Background Color"), + extras: Optional[str] = Query( + default=None, description="Extra parameters as JSON" + ), + ): + self.model = model + self.a = a + self.af = af + self.ab = ab + self.ae = ae + self.om = om + self.ppm = ppm + self.extras = extras + self.bgc = ( + cast(Tuple[int, int, int, int], tuple(map(int, bgc.split(",")))) + if bgc + else None + ) + + class CommonQueryPostParams: + def __init__( + self, + model: str = Form( + description="Model to use when processing image", + regex=r"(" + "|".join(sessions_names) + ")", + default="u2net", + ), + a: bool = Form(default=False, description="Enable Alpha Matting"), + af: int = Form( + default=240, + ge=0, + le=255, + description="Alpha Matting (Foreground Threshold)", + ), + ab: int = Form( + default=10, + ge=0, + le=255, + description="Alpha Matting (Background Threshold)", + ), + ae: int = Form( + default=10, ge=0, description="Alpha Matting (Erode Structure Size)" + ), + om: bool = Form(default=False, description="Only Mask"), + ppm: bool = Form(default=False, description="Post Process Mask"), + bgc: Optional[str] = Query(default=None, description="Background Color"), + extras: Optional[str] = Query( + default=None, description="Extra parameters as JSON" + ), + ): + self.model = model + self.a = a + self.af = af + self.ab = ab + self.ae = ae + self.om = om + self.ppm = ppm + self.extras = extras + self.bgc = ( + cast(Tuple[int, int, int, int], tuple(map(int, bgc.split(",")))) + if bgc + else None + ) + + def im_without_bg(content: bytes, commons: CommonQueryParams) -> Response: + kwargs = {} + + if commons.extras: + try: + kwargs.update(json.loads(commons.extras)) + except Exception: + pass + + return Response( + remove( + content, + session=sessions.setdefault( + commons.model, new_session(commons.model, **kwargs) + ), + alpha_matting=commons.a, + alpha_matting_foreground_threshold=commons.af, + alpha_matting_background_threshold=commons.ab, + alpha_matting_erode_size=commons.ae, + only_mask=commons.om, + post_process_mask=commons.ppm, + bgcolor=commons.bgc, + **kwargs, + ), + media_type="image/png", + ) + + @app.on_event("startup") + def startup(): + try: + webbrowser.open(f"http://localhost:{port}") + except Exception: + pass + + if threads is not None: + from anyio import CapacityLimiter + from anyio.lowlevel import RunVar + + RunVar("_default_thread_limiter").set(CapacityLimiter(threads)) + + @app.get( + path="/api/remove", + tags=["Background Removal"], + summary="Remove from URL", + description="Removes the background from an image obtained by retrieving an URL.", + ) + async def get_index( + url: str = Query( + default=..., description="URL of the image that has to be processed." + ), + commons: CommonQueryParams = Depends(), + ): + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + file = await response.read() + return await asyncify(im_without_bg)(file, commons) + + @app.post( + path="/api/remove", + tags=["Background Removal"], + summary="Remove from Stream", + description="Removes the background from an image sent within the request itself.", + ) + async def post_index( + file: bytes = File( + default=..., + description="Image file (byte stream) that has to be processed.", + ), + commons: CommonQueryPostParams = Depends(), + ): + return await asyncify(im_without_bg)(file, commons) # type: ignore + + def gr_app(app): + def inference(input_path, model, cmd_args): + output_path = "output.png" + + kwargs = {} + if cmd_args: + kwargs.update(json.loads(cmd_args)) + kwargs["session"] = new_session(model, **kwargs) + + with open(input_path, "rb") as i: + with open(output_path, "wb") as o: + input = i.read() + output = remove(input, **kwargs) + o.write(output) + return os.path.join(output_path) + + interface = gr.Interface( + inference, + [ + gr.components.Image(type="filepath", label="Input"), + gr.components.Dropdown(sessions_names, value="u2net", label="Models"), + gr.components.Textbox(label="Arguments"), + ], + gr.components.Image(type="filepath", label="Output"), + ) + + interface.queue(concurrency_count=3) + app = gr.mount_gradio_app(app, interface, path="/") + return app + + print(f"To access the API documentation, go to http://localhost:{port}/api") + print(f"To access the UI, go to http://localhost:{port}") + + uvicorn.run(gr_app(app), host="0.0.0.0", port=port, log_level=log_level) diff --git a/rembg/session_factory.py b/rembg/session_factory.py new file mode 100644 index 00000000..1f585745 --- /dev/null +++ b/rembg/session_factory.py @@ -0,0 +1,27 @@ +import os +from typing import Type + +import onnxruntime as ort + +from .sessions import sessions_class +from .sessions.base import BaseSession +from .sessions.u2net import U2netSession + + +def new_session( + model_name: str = "u2net", providers=None, *args, **kwargs +) -> BaseSession: + session_class: Type[BaseSession] = U2netSession + + for sc in sessions_class: + if sc.name() == model_name: + session_class = sc + break + + sess_opts = ort.SessionOptions() + + if "OMP_NUM_THREADS" in os.environ: + sess_opts.inter_op_num_threads = int(os.environ["OMP_NUM_THREADS"]) + sess_opts.intra_op_num_threads = int(os.environ["OMP_NUM_THREADS"]) + + return session_class(model_name, sess_opts, providers, *args, **kwargs) diff --git a/rembg/sessions/__init__.py b/rembg/sessions/__init__.py new file mode 100644 index 00000000..08ca20a5 --- /dev/null +++ b/rembg/sessions/__init__.py @@ -0,0 +1,22 @@ +from importlib import import_module +from inspect import isclass +from pathlib import Path +from pkgutil import iter_modules + +from .base import BaseSession + +sessions_class = [] +sessions_names = [] + +package_dir = Path(__file__).resolve().parent +for _b, module_name, _p in iter_modules([str(package_dir)]): + module = import_module(f"{__name__}.{module_name}") + for attribute_name in dir(module): + attribute = getattr(module, attribute_name) + if ( + isclass(attribute) + and issubclass(attribute, BaseSession) + and attribute != BaseSession + ): + sessions_class.append(attribute) + sessions_names.append(attribute.name()) diff --git a/rembg/sessions/base.py b/rembg/sessions/base.py new file mode 100644 index 00000000..16f988ed --- /dev/null +++ b/rembg/sessions/base.py @@ -0,0 +1,85 @@ +import os +from typing import Dict, List, Tuple + +import numpy as np +import onnxruntime as ort +from PIL import Image +from PIL.Image import Image as PILImage + + +class BaseSession: + def __init__( + self, + model_name: str, + sess_opts: ort.SessionOptions, + providers=None, + *args, + **kwargs + ): + self.model_name = model_name + + self.providers = [] + + _providers = ort.get_available_providers() + if providers: + for provider in providers: + if provider in _providers: + self.providers.append(provider) + else: + self.providers.extend(_providers) + + self.inner_session = ort.InferenceSession( + str(self.__class__.download_models(*args, **kwargs)), + providers=self.providers, + sess_options=sess_opts, + ) + + def normalize( + self, + img: PILImage, + mean: Tuple[float, float, float], + std: Tuple[float, float, float], + size: Tuple[int, int], + *args, + **kwargs + ) -> Dict[str, np.ndarray]: + im = img.convert("RGB").resize(size, Image.LANCZOS) + + im_ary = np.array(im) + im_ary = im_ary / np.max(im_ary) + + tmpImg = np.zeros((im_ary.shape[0], im_ary.shape[1], 3)) + tmpImg[:, :, 0] = (im_ary[:, :, 0] - mean[0]) / std[0] + tmpImg[:, :, 1] = (im_ary[:, :, 1] - mean[1]) / std[1] + tmpImg[:, :, 2] = (im_ary[:, :, 2] - mean[2]) / std[2] + + tmpImg = tmpImg.transpose((2, 0, 1)) + + return { + self.inner_session.get_inputs()[0] + .name: np.expand_dims(tmpImg, 0) + .astype(np.float32) + } + + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + raise NotImplementedError + + @classmethod + def checksum_disabled(cls, *args, **kwargs): + return os.getenv("MODEL_CHECKSUM_DISABLED", None) is not None + + @classmethod + def u2net_home(cls, *args, **kwargs): + return os.path.expanduser( + os.getenv( + "U2NET_HOME", os.path.join(os.getenv("XDG_DATA_HOME", "~"), ".u2net") + ) + ) + + @classmethod + def download_models(cls, *args, **kwargs): + raise NotImplementedError + + @classmethod + def name(cls, *args, **kwargs): + raise NotImplementedError diff --git a/rembg/sessions/dis_anime.py b/rembg/sessions/dis_anime.py new file mode 100644 index 00000000..822051ac --- /dev/null +++ b/rembg/sessions/dis_anime.py @@ -0,0 +1,49 @@ +import os +from typing import List + +import numpy as np +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +class DisSession(BaseSession): + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize(img, (0.485, 0.456, 0.406), (1.0, 1.0, 1.0), (1024, 1024)), + ) + + pred = ort_outs[0][:, 0, :, :] + + ma = np.max(pred) + mi = np.min(pred) + + pred = (pred - mi) / (ma - mi) + pred = np.squeeze(pred) + + mask = Image.fromarray((pred * 255).astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + return [mask] + + @classmethod + def download_models(cls, *args, **kwargs): + fname = f"{cls.name(*args, **kwargs)}.onnx" + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/isnet-anime.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:6f184e756bb3bd901c8849220a83e38e", + fname=fname, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return os.path.join(cls.u2net_home(*args, **kwargs), fname) + + @classmethod + def name(cls, *args, **kwargs): + return "isnet-anime" diff --git a/rembg/sessions/dis_general_use.py b/rembg/sessions/dis_general_use.py new file mode 100644 index 00000000..6a4cdae4 --- /dev/null +++ b/rembg/sessions/dis_general_use.py @@ -0,0 +1,49 @@ +import os +from typing import List + +import numpy as np +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +class DisSession(BaseSession): + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize(img, (0.485, 0.456, 0.406), (1.0, 1.0, 1.0), (1024, 1024)), + ) + + pred = ort_outs[0][:, 0, :, :] + + ma = np.max(pred) + mi = np.min(pred) + + pred = (pred - mi) / (ma - mi) + pred = np.squeeze(pred) + + mask = Image.fromarray((pred * 255).astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + return [mask] + + @classmethod + def download_models(cls, *args, **kwargs): + fname = f"{cls.name(*args, **kwargs)}.onnx" + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/isnet-general-use.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:fc16ebd8b0c10d971d3513d564d01e29", + fname=fname, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return os.path.join(cls.u2net_home(*args, **kwargs), fname) + + @classmethod + def name(cls, *args, **kwargs): + return "isnet-general-use" diff --git a/rembg/sessions/sam.py b/rembg/sessions/sam.py new file mode 100644 index 00000000..0bee3e09 --- /dev/null +++ b/rembg/sessions/sam.py @@ -0,0 +1,169 @@ +import os +from typing import List + +import numpy as np +import onnxruntime as ort +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +def get_preprocess_shape(oldh: int, oldw: int, long_side_length: int): + scale = long_side_length * 1.0 / max(oldh, oldw) + newh, neww = oldh * scale, oldw * scale + neww = int(neww + 0.5) + newh = int(newh + 0.5) + return (newh, neww) + + +def apply_coords(coords: np.ndarray, original_size, target_length) -> np.ndarray: + old_h, old_w = original_size + new_h, new_w = get_preprocess_shape( + original_size[0], original_size[1], target_length + ) + coords = coords.copy().astype(float) + coords[..., 0] = coords[..., 0] * (new_w / old_w) + coords[..., 1] = coords[..., 1] * (new_h / old_h) + return coords + + +def resize_longes_side(img: PILImage, size=1024): + w, h = img.size + if h > w: + new_h, new_w = size, int(w * size / h) + else: + new_h, new_w = int(h * size / w), size + + return img.resize((new_w, new_h)) + + +def pad_to_square(img: np.ndarray, size=1024): + h, w = img.shape[:2] + padh = size - h + padw = size - w + img = np.pad(img, ((0, padh), (0, padw), (0, 0)), mode="constant") + img = img.astype(np.float32) + return img + + +class SamSession(BaseSession): + def __init__(self, model_name: str, sess_opts: ort.SessionOptions, *args, **kwargs): + self.model_name = model_name + paths = self.__class__.download_models() + self.encoder = ort.InferenceSession( + str(paths[0]), + providers=ort.get_available_providers(), + sess_options=sess_opts, + ) + self.decoder = ort.InferenceSession( + str(paths[1]), + providers=ort.get_available_providers(), + sess_options=sess_opts, + ) + + def normalize( + self, + img: np.ndarray, + mean=(123.675, 116.28, 103.53), + std=(58.395, 57.12, 57.375), + size=(1024, 1024), + *args, + **kwargs, + ): + pixel_mean = np.array([*mean]).reshape(1, 1, -1) + pixel_std = np.array([*std]).reshape(1, 1, -1) + x = (img - pixel_mean) / pixel_std + return x + + def predict( + self, + img: PILImage, + *args, + **kwargs, + ) -> List[PILImage]: + # Preprocess image + image = resize_longes_side(img) + image = np.array(image) + image = self.normalize(image) + image = pad_to_square(image) + + input_labels = kwargs.get("input_labels") + input_points = kwargs.get("input_points") + + if input_labels is None: + raise ValueError("input_labels is required") + if input_points is None: + raise ValueError("input_points is required") + + # Transpose + image = image.transpose(2, 0, 1)[None, :, :, :] + # Run encoder (Image embedding) + encoded = self.encoder.run(None, {"x": image}) + image_embedding = encoded[0] + + # Add a batch index, concatenate a padding point, and transform. + onnx_coord = np.concatenate([input_points, np.array([[0.0, 0.0]])], axis=0)[ + None, :, : + ] + onnx_label = np.concatenate([input_labels, np.array([-1])], axis=0)[ + None, : + ].astype(np.float32) + onnx_coord = apply_coords(onnx_coord, img.size[::1], 1024).astype(np.float32) + + # Create an empty mask input and an indicator for no mask. + onnx_mask_input = np.zeros((1, 1, 256, 256), dtype=np.float32) + onnx_has_mask_input = np.zeros(1, dtype=np.float32) + + decoder_inputs = { + "image_embeddings": image_embedding, + "point_coords": onnx_coord, + "point_labels": onnx_label, + "mask_input": onnx_mask_input, + "has_mask_input": onnx_has_mask_input, + "orig_im_size": np.array(img.size[::-1], dtype=np.float32), + } + + masks, _, low_res_logits = self.decoder.run(None, decoder_inputs) + masks = masks > 0.0 + masks = [ + Image.fromarray((masks[i, 0] * 255).astype(np.uint8)) + for i in range(masks.shape[0]) + ] + + return masks + + @classmethod + def download_models(cls, *args, **kwargs): + fname_encoder = f"{cls.name(*args, **kwargs)}_encoder.onnx" + fname_decoder = f"{cls.name(*args, **kwargs)}_decoder.onnx" + + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/vit_b-encoder-quant.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:13d97c5c79ab13ef86d67cbde5f1b250", + fname=fname_encoder, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/vit_b-decoder-quant.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:fa3d1c36a3187d3de1c8deebf33dd127", + fname=fname_decoder, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return ( + os.path.join(cls.u2net_home(*args, **kwargs), fname_encoder), + os.path.join(cls.u2net_home(*args, **kwargs), fname_decoder), + ) + + @classmethod + def name(cls, *args, **kwargs): + return "sam" diff --git a/rembg/sessions/silueta.py b/rembg/sessions/silueta.py new file mode 100644 index 00000000..137d78f7 --- /dev/null +++ b/rembg/sessions/silueta.py @@ -0,0 +1,51 @@ +import os +from typing import List + +import numpy as np +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +class SiluetaSession(BaseSession): + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize( + img, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225), (320, 320) + ), + ) + + pred = ort_outs[0][:, 0, :, :] + + ma = np.max(pred) + mi = np.min(pred) + + pred = (pred - mi) / (ma - mi) + pred = np.squeeze(pred) + + mask = Image.fromarray((pred * 255).astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + return [mask] + + @classmethod + def download_models(cls, *args, **kwargs): + fname = f"{cls.name()}.onnx" + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/silueta.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:55e59e0d8062d2f5d013f4725ee84782", + fname=fname, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return os.path.join(cls.u2net_home(*args, **kwargs), fname) + + @classmethod + def name(cls, *args, **kwargs): + return "silueta" diff --git a/rembg/sessions/u2net.py b/rembg/sessions/u2net.py new file mode 100644 index 00000000..15664f4c --- /dev/null +++ b/rembg/sessions/u2net.py @@ -0,0 +1,51 @@ +import os +from typing import List + +import numpy as np +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +class U2netSession(BaseSession): + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize( + img, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225), (320, 320) + ), + ) + + pred = ort_outs[0][:, 0, :, :] + + ma = np.max(pred) + mi = np.min(pred) + + pred = (pred - mi) / (ma - mi) + pred = np.squeeze(pred) + + mask = Image.fromarray((pred * 255).astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + return [mask] + + @classmethod + def download_models(cls, *args, **kwargs): + fname = f"{cls.name(*args, **kwargs)}.onnx" + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:60024c5c889badc19c04ad937298a77b", + fname=fname, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return os.path.join(cls.u2net_home(*args, **kwargs), fname) + + @classmethod + def name(cls, *args, **kwargs): + return "u2net" diff --git a/rembg/sessions/u2net_cloth_seg.py b/rembg/sessions/u2net_cloth_seg.py new file mode 100644 index 00000000..6c8adb5f --- /dev/null +++ b/rembg/sessions/u2net_cloth_seg.py @@ -0,0 +1,128 @@ +import os +from typing import List + +import numpy as np +import pooch +from PIL import Image +from PIL.Image import Image as PILImage +from scipy.special import log_softmax + +from .base import BaseSession + +palette1 = [ + 0, + 0, + 0, + 255, + 255, + 255, + 0, + 0, + 0, + 0, + 0, + 0, +] + +palette2 = [ + 0, + 0, + 0, + 0, + 0, + 0, + 255, + 255, + 255, + 0, + 0, + 0, +] + +palette3 = [ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 255, + 255, + 255, +] + + +class Unet2ClothSession(BaseSession): + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize( + img, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225), (768, 768) + ), + ) + + pred = ort_outs + pred = log_softmax(pred[0], 1) + pred = np.argmax(pred, axis=1, keepdims=True) + pred = np.squeeze(pred, 0) + pred = np.squeeze(pred, 0) + + mask = Image.fromarray(pred.astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + masks = [] + + cloth_category = kwargs.get("cc") or kwargs.get("cloth_category") + + def upper_cloth(): + mask1 = mask.copy() + mask1.putpalette(palette1) + mask1 = mask1.convert("RGB").convert("L") + masks.append(mask1) + + def lower_cloth(): + mask2 = mask.copy() + mask2.putpalette(palette2) + mask2 = mask2.convert("RGB").convert("L") + masks.append(mask2) + + def full_cloth(): + mask3 = mask.copy() + mask3.putpalette(palette3) + mask3 = mask3.convert("RGB").convert("L") + masks.append(mask3) + + if cloth_category == "upper": + upper_cloth() + elif cloth_category == "lower": + lower_cloth() + elif cloth_category == "full": + full_cloth() + else: + upper_cloth() + lower_cloth() + full_cloth() + + return masks + + @classmethod + def download_models(cls, *args, **kwargs): + fname = f"{cls.name(*args, **kwargs)}.onnx" + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_cloth_seg.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:2434d1f3cb744e0e49386c906e5a08bb", + fname=fname, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return os.path.join(cls.u2net_home(*args, **kwargs), fname) + + @classmethod + def name(cls, *args, **kwargs): + return "u2net_cloth_seg" diff --git a/rembg/sessions/u2net_custom.py b/rembg/sessions/u2net_custom.py new file mode 100644 index 00000000..58cd3832 --- /dev/null +++ b/rembg/sessions/u2net_custom.py @@ -0,0 +1,59 @@ +import os +from typing import List + +import numpy as np +import onnxruntime as ort +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +class U2netCustomSession(BaseSession): + def __init__( + self, + model_name: str, + sess_opts: ort.SessionOptions, + providers=None, + *args, + **kwargs + ): + model_path = kwargs.get("model_path") + if model_path is None: + raise ValueError("model_path is required") + + super().__init__(model_name, sess_opts, providers, *args, **kwargs) + + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize( + img, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225), (320, 320) + ), + ) + + pred = ort_outs[0][:, 0, :, :] + + ma = np.max(pred) + mi = np.min(pred) + + pred = (pred - mi) / (ma - mi) + pred = np.squeeze(pred) + + mask = Image.fromarray((pred * 255).astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + return [mask] + + @classmethod + def download_models(cls, *args, **kwargs): + model_path = kwargs.get("model_path") + if model_path is None: + return + + return os.path.abspath(os.path.expanduser(model_path)) + + @classmethod + def name(cls, *args, **kwargs): + return "u2net_custom" diff --git a/rembg/sessions/u2net_human_seg.py b/rembg/sessions/u2net_human_seg.py new file mode 100644 index 00000000..2c6c8047 --- /dev/null +++ b/rembg/sessions/u2net_human_seg.py @@ -0,0 +1,51 @@ +import os +from typing import List + +import numpy as np +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +class U2netHumanSegSession(BaseSession): + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize( + img, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225), (320, 320) + ), + ) + + pred = ort_outs[0][:, 0, :, :] + + ma = np.max(pred) + mi = np.min(pred) + + pred = (pred - mi) / (ma - mi) + pred = np.squeeze(pred) + + mask = Image.fromarray((pred * 255).astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + return [mask] + + @classmethod + def download_models(cls, *args, **kwargs): + fname = f"{cls.name(*args, **kwargs)}.onnx" + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net_human_seg.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:c09ddc2e0104f800e3e1bb4652583d1f", + fname=fname, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return os.path.join(cls.u2net_home(*args, **kwargs), fname) + + @classmethod + def name(cls, *args, **kwargs): + return "u2net_human_seg" diff --git a/rembg/sessions/u2netp.py b/rembg/sessions/u2netp.py new file mode 100644 index 00000000..e34420b0 --- /dev/null +++ b/rembg/sessions/u2netp.py @@ -0,0 +1,51 @@ +import os +from typing import List + +import numpy as np +import pooch +from PIL import Image +from PIL.Image import Image as PILImage + +from .base import BaseSession + + +class U2netpSession(BaseSession): + def predict(self, img: PILImage, *args, **kwargs) -> List[PILImage]: + ort_outs = self.inner_session.run( + None, + self.normalize( + img, (0.485, 0.456, 0.406), (0.229, 0.224, 0.225), (320, 320) + ), + ) + + pred = ort_outs[0][:, 0, :, :] + + ma = np.max(pred) + mi = np.min(pred) + + pred = (pred - mi) / (ma - mi) + pred = np.squeeze(pred) + + mask = Image.fromarray((pred * 255).astype("uint8"), mode="L") + mask = mask.resize(img.size, Image.LANCZOS) + + return [mask] + + @classmethod + def download_models(cls, *args, **kwargs): + fname = f"{cls.name(*args, **kwargs)}.onnx" + pooch.retrieve( + "https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2netp.onnx", + None + if cls.checksum_disabled(*args, **kwargs) + else "md5:8e83ca70e441ab06c318d82300c84806", + fname=fname, + path=cls.u2net_home(*args, **kwargs), + progressbar=True, + ) + + return os.path.join(cls.u2net_home(*args, **kwargs), fname) + + @classmethod + def name(cls, *args, **kwargs): + return "u2netp" diff --git a/requirements.txt b/requirements.txt index e2686cb2..3d27e647 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,11 +1,93 @@ -flask==1.1.2 -numpy==1.19.1 -pillow==7.2.0 -scikit-image==0.17.2 -torch==1.6.0 -torchvision==0.7.0 -waitress==1.4.4 -tqdm==4.48.2 -requests==2.24.0 -scipy==1.5.2 -pymatting==1.0.6 +aiofiles==23.2.1 +aiohttp==3.8.6 +aiosignal==1.3.1 +altair==5.1.2 +annotated-types==0.6.0 +anyio==3.7.1 +async-timeout==4.0.3 +asyncer==0.0.2 +attrs==23.1.0 +certifi==2023.7.22 +charset-normalizer==3.3.2 +click==8.1.7 +colorama==0.4.6 +coloredlogs==15.0.1 +contourpy==1.2.0 +cycler==0.12.1 +exceptiongroup==1.1.3 +fastapi==0.104.1 +ffmpy==0.3.1 +filelock==3.13.1 +filetype==1.2.0 +flatbuffers==23.5.26 +fonttools==4.44.0 +frozenlist==1.4.0 +fsspec==2023.10.0 +gradio==3.41.0 +gradio_client==0.5.0 +h11==0.14.0 +httpcore==1.0.2 +httpx==0.25.1 +huggingface-hub==0.19.1 +humanfriendly==10.0 +idna==3.4 +imageio==2.32.0 +importlib-resources==6.1.1 +Jinja2==3.1.2 +jsonschema==4.19.2 +jsonschema-specifications==2023.7.1 +kiwisolver==1.4.5 +lazy_loader==0.3 +llvmlite==0.41.1 +markdown-it-py==3.0.0 +MarkupSafe==2.1.3 +matplotlib==3.8.1 +mdurl==0.1.2 +mpmath==1.3.0 +multidict==6.0.4 +networkx==3.2.1 +numba==0.58.1 +numpy==1.26.2 +onnxruntime==1.16.2 +opencv-python-headless==4.8.1.78 +orjson==3.9.10 +packaging==23.2 +pandas==2.1.3 +Pillow==10.0.1 +platformdirs==4.0.0 +pooch==1.8.0 +protobuf==4.25.0 +pydantic==2.5.0 +pydantic_core==2.14.1 +pydub==0.25.1 +Pygments==2.16.1 +PyMatting==1.1.11 +pyparsing==3.1.1 +python-dateutil==2.8.2 +python-multipart==0.0.6 +pytz==2023.3.post1 +PyYAML==6.0.1 +referencing==0.30.2 +requests==2.31.0 +rich==13.6.0 +rpds-py==0.12.0 +scikit-image==0.22.0 +scipy==1.11.3 +semantic-version==2.10.0 +shellingham==1.5.4 +six==1.16.0 +sniffio==1.3.0 +starlette==0.27.0 +sympy==1.12 +tifffile==2023.9.26 +tomlkit==0.12.0 +toolz==0.12.0 +tqdm==4.66.1 +typer==0.9.0 +typing_extensions==4.8.0 +tzdata==2023.3 +urllib3==2.1.0 +uvicorn==0.24.0.post1 +watchdog==3.0.0 +websockets==11.0.3 +yarl==1.9.2 \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index dadae668..0bf58854 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,3 +2,15 @@ # This includes the license file(s) in the wheel. # https://wheel.readthedocs.io/en/stable/user_guide.html#including-license-files-in-the-generated-wheel-file license_files = LICENSE.txt + +# See the docstring in versioneer.py for instructions. Note that you must +# re-run 'versioneer.py setup' after changing this section, and commit the +# resulting files. + +[versioneer] +VCS = git +style = pep440 +versionfile_source = rembg/_version.py +versionfile_build = rembg/_version.py +tag_prefix = v +parentdir_prefix = rembg- diff --git a/setup.py b/setup.py index a0a50345..dccc6b61 100644 --- a/setup.py +++ b/setup.py @@ -1,17 +1,64 @@ +import os import pathlib +import sys +sys.path.append(os.path.dirname(__file__)) from setuptools import find_packages, setup +import versioneer + here = pathlib.Path(__file__).parent.resolve() long_description = (here / "README.md").read_text(encoding="utf-8") -with open("requirements.txt") as f: - requireds = f.read().splitlines() +install_requires = [ + "numpy", + "onnxruntime", + "opencv-python-headless", + "pillow", + "pooch", + "pymatting", + "scikit-image", + "scipy", + "tqdm", +] + +extras_require = { + "dev": [ + "bandit", + "black", + "flake8", + "imagehash", + "isort", + "mypy", + "pytest", + "setuptools", + "twine", + "wheel", + ], + "gpu": ["onnxruntime-gpu"], + "cli": [ + "aiohttp", + "asyncer", + "click", + "fastapi", + "filetype", + "gradio", + "python-multipart", + "uvicorn", + "watchdog", + ], +} + +entry_points = { + "console_scripts": [ + "rembg=rembg.cli:main", + ], +} + setup( name="rembg", - version="1.0.13", description="Remove image background", long_description=long_description, long_description_content_type="text/markdown", @@ -20,17 +67,25 @@ author_email="danielgatis@gmail.com", classifiers=[ "License :: OSI Approved :: MIT License", + "Topic :: Scientific/Engineering", + "Topic :: Scientific/Engineering :: Mathematics", + "Topic :: Scientific/Engineering :: Artificial Intelligence", + "Topic :: Software Development", + "Topic :: Software Development :: Libraries", + "Topic :: Software Development :: Libraries :: Python Modules", + "Programming Language :: Python", "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", ], keywords="remove, background, u2net", - package_dir={"": "src"}, - packages=find_packages(where="src"), - python_requires=">=3.5, <4", - install_requires=requireds, - entry_points={ - "console_scripts": [ - "rembg=rembg.cmd.cli:main", - "rembg-server=rembg.cmd.server:main", - ], - }, + python_requires=">=3.8, <3.12", + packages=find_packages(), + install_requires=install_requires, + entry_points=entry_points, + extras_require=extras_require, + version=versioneer.get_version(), + cmdclass=versioneer.get_cmdclass(), ) diff --git a/src/rembg/__init__.py b/src/rembg/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/rembg/bg.py b/src/rembg/bg.py deleted file mode 100644 index f364eeb8..00000000 --- a/src/rembg/bg.py +++ /dev/null @@ -1,99 +0,0 @@ -import io - -import numpy as np -from PIL import Image -from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf -from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml -from pymatting.util.util import stack_images -from scipy.ndimage.morphology import binary_erosion - -from .u2net import detect - -model_u2net = detect.load_model(model_name="u2net") -model_u2netp = detect.load_model(model_name="u2netp") - - -def alpha_matting_cutout( - img, mask, foreground_threshold, background_threshold, erode_structure_size, -): - base_size = (1000, 1000) - size = img.size - - img.thumbnail(base_size, Image.LANCZOS) - mask = mask.resize(img.size, Image.LANCZOS) - - img = np.asarray(img) - mask = np.asarray(mask) - - # guess likely foreground/background - is_foreground = mask > foreground_threshold - is_background = mask < background_threshold - - # erode foreground/background - structure = None - if erode_structure_size > 0: - structure = np.ones((erode_structure_size, erode_structure_size), dtype=np.int) - - is_foreground = binary_erosion(is_foreground, structure=structure) - is_background = binary_erosion(is_background, structure=structure, border_value=1) - - # build trimap - # 0 = background - # 128 = unknown - # 255 = foreground - trimap = np.full(mask.shape, dtype=np.uint8, fill_value=128) - trimap[is_foreground] = 255 - trimap[is_background] = 0 - - # build the cutout image - img_normalized = img / 255.0 - trimap_normalized = trimap / 255.0 - - alpha = estimate_alpha_cf(img_normalized, trimap_normalized) - foreground = estimate_foreground_ml(img_normalized, alpha) - cutout = stack_images(foreground, alpha) - - cutout = np.clip(cutout * 255, 0, 255).astype(np.uint8) - cutout = Image.fromarray(cutout) - cutout = cutout.resize(size, Image.LANCZOS) - - return cutout - - -def naive_cutout(img, mask): - empty = Image.new("RGBA", (img.size), 0) - cutout = Image.composite(img, empty, mask.resize(img.size, Image.LANCZOS)) - return cutout - - -def remove( - data, - model_name="u2net", - alpha_matting=False, - alpha_matting_foreground_threshold=240, - alpha_matting_background_threshold=10, - alpha_matting_erode_structure_size=10, -): - model = model_u2net - - if model == "u2netp": - model = model_u2netp - - img = Image.open(io.BytesIO(data)).convert("RGB") - mask = detect.predict(model, np.array(img)).convert("L") - - if alpha_matting: - cutout = alpha_matting_cutout( - img, - mask, - alpha_matting_foreground_threshold, - alpha_matting_background_threshold, - alpha_matting_erode_structure_size, - ) - else: - cutout = naive_cutout(img, mask) - - bio = io.BytesIO() - cutout.save(bio, "PNG") - - return bio.getbuffer() diff --git a/src/rembg/cmd/__init__.py b/src/rembg/cmd/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/rembg/cmd/cli.py b/src/rembg/cmd/cli.py deleted file mode 100644 index ce0229a2..00000000 --- a/src/rembg/cmd/cli.py +++ /dev/null @@ -1,125 +0,0 @@ -import argparse -import glob -import imghdr -import os -from distutils.util import strtobool - -from ..bg import remove - - -def main(): - ap = argparse.ArgumentParser() - - ap.add_argument( - "-m", - "--model", - default="u2net", - type=str, - choices=("u2net", "u2netp"), - help="The model name.", - ) - - ap.add_argument( - "-a", - "--alpha-matting", - nargs="?", - const=True, - default=False, - type=lambda x: bool(strtobool(x)), - help="When true use alpha matting cutout.", - ) - - ap.add_argument( - "-af", - "--alpha-matting-foreground-threshold", - default=240, - type=int, - help="The trimap foreground threshold.", - ) - - ap.add_argument( - "-ab", - "--alpha-matting-background-threshold", - default=10, - type=int, - help="The trimap background threshold.", - ) - - ap.add_argument( - "-ae", - "--alpha-matting-erode-size", - default=10, - type=int, - help="Size of element used for the erosion.", - ) - - ap.add_argument( - "-p", "--path", nargs="+", help="Path of a file or a folder of files.", - ) - - ap.add_argument( - "-o", - "--output", - nargs="?", - default="-", - type=argparse.FileType("wb"), - help="Path to the output png image.", - ) - - ap.add_argument( - "input", - nargs="?", - default="-", - type=argparse.FileType("rb"), - help="Path to the input image.", - ) - - args = ap.parse_args() - - r = lambda i: i.buffer.read() if hasattr(i, "buffer") else i.read() - w = lambda o, data: o.buffer.write(data) if hasattr(o, "buffer") else o.write(data) - - if args.path: - full_paths = [os.path.abspath(path) for path in args.path] - files = set() - - for path in full_paths: - if os.path.isfile(path): - files.add(path) - else: - full_paths += glob.glob(path + "/*") - - for fi in files: - if imghdr.what(fi) is None: - continue - - with open(fi, "rb") as input: - with open(os.path.splitext(fi)[0] + ".out.png", "wb") as output: - w( - output, - remove( - r(input), - model_name=args.model, - alpha_matting=args.alpha_matting, - alpha_matting_foreground_threshold=args.alpha_matting_foreground_threshold, - alpha_matting_background_threshold=args.alpha_matting_background_threshold, - alpha_matting_erode_structure_size=args.alpha_matting_erode_size, - ), - ) - - else: - w( - args.output, - remove( - r(args.input), - model_name=args.model, - alpha_matting=args.alpha_matting, - alpha_matting_foreground_threshold=args.alpha_matting_foreground_threshold, - alpha_matting_background_threshold=args.alpha_matting_background_threshold, - alpha_matting_erode_structure_size=args.alpha_matting_erode_size, - ), - ) - - -if __name__ == "__main__": - main() diff --git a/src/rembg/cmd/server.py b/src/rembg/cmd/server.py deleted file mode 100644 index f946fd7f..00000000 --- a/src/rembg/cmd/server.py +++ /dev/null @@ -1,62 +0,0 @@ -import argparse -from io import BytesIO -from urllib.parse import unquote_plus -from urllib.request import urlopen - -from flask import Flask, request, send_file -from waitress import serve - -from ..bg import remove - -app = Flask(__name__) - - -@app.route("/", methods=["GET", "POST"]) -def index(): - file_content = "" - - if request.method == "POST": - if "file" not in request.files: - return {"error": "missing post form param 'file'"}, 400 - - file_content = request.files["file"].read() - - if request.method == "GET": - url = request.args.get("url", type=str) - if url is None: - return {"error": "missing query param 'url'"}, 400 - - file_content = urlopen(unquote_plus(url)).read() - - if file_content == "": - return {"error": "File content is empty"}, 400 - - model = request.args.get("model", type=str, default="u2net") - if model not in ("u2net", "u2netp"): - return {"error": "invalid query param 'model'"}, 400 - - try: - return send_file(BytesIO(remove(file_content, model)), mimetype="image/png",) - except Exception as e: - app.logger.exception(e, exc_info=True) - return {"error": "oops, something went wrong!"}, 500 - - -def main(): - ap = argparse.ArgumentParser() - - ap.add_argument( - "-a", "--addr", default="0.0.0.0", type=str, help="The IP address to bind to.", - ) - - ap.add_argument( - "-p", "--port", default=5000, type=int, help="The port to bind to.", - ) - - args = ap.parse_args() - app.add_url_rule("/", "index", index) - serve(app, host=args.addr, port=args.port) - - -if __name__ == "__main__": - main() diff --git a/src/rembg/u2net/__init__.py b/src/rembg/u2net/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/rembg/u2net/data_loader.py b/src/rembg/u2net/data_loader.py deleted file mode 100644 index 35a27970..00000000 --- a/src/rembg/u2net/data_loader.py +++ /dev/null @@ -1,327 +0,0 @@ -# data loader -from __future__ import division, print_function - -import random - -import matplotlib.pyplot as plt -import numpy as np -import torch -from PIL import Image -from skimage import color, io, transform -from torch.utils.data import DataLoader, Dataset -from torchvision import transforms, utils - - -# ==========================dataset load========================== -class RescaleT(object): - def __init__(self, output_size): - assert isinstance(output_size, (int, tuple)) - self.output_size = output_size - - def __call__(self, sample): - imidx, image, label = sample["imidx"], sample["image"], sample["label"] - - h, w = image.shape[:2] - - if isinstance(self.output_size, int): - if h > w: - new_h, new_w = self.output_size * h / w, self.output_size - else: - new_h, new_w = self.output_size, self.output_size * w / h - else: - new_h, new_w = self.output_size - - new_h, new_w = int(new_h), int(new_w) - - # #resize the image to new_h x new_w and convert image from range [0,255] to [0,1] - # img = transform.resize(image,(new_h,new_w),mode='constant') - # lbl = transform.resize(label,(new_h,new_w),mode='constant', order=0, preserve_range=True) - - img = transform.resize( - image, (self.output_size, self.output_size), mode="constant" - ) - lbl = transform.resize( - label, - (self.output_size, self.output_size), - mode="constant", - order=0, - preserve_range=True, - ) - - return {"imidx": imidx, "image": img, "label": lbl} - - -class Rescale(object): - def __init__(self, output_size): - assert isinstance(output_size, (int, tuple)) - self.output_size = output_size - - def __call__(self, sample): - imidx, image, label = sample["imidx"], sample["image"], sample["label"] - - if random.random() >= 0.5: - image = image[::-1] - label = label[::-1] - - h, w = image.shape[:2] - - if isinstance(self.output_size, int): - if h > w: - new_h, new_w = self.output_size * h / w, self.output_size - else: - new_h, new_w = self.output_size, self.output_size * w / h - else: - new_h, new_w = self.output_size - - new_h, new_w = int(new_h), int(new_w) - - # #resize the image to new_h x new_w and convert image from range [0,255] to [0,1] - img = transform.resize(image, (new_h, new_w), mode="constant") - lbl = transform.resize( - label, (new_h, new_w), mode="constant", order=0, preserve_range=True - ) - - return {"imidx": imidx, "image": img, "label": lbl} - - -class RandomCrop(object): - def __init__(self, output_size): - assert isinstance(output_size, (int, tuple)) - if isinstance(output_size, int): - self.output_size = (output_size, output_size) - else: - assert len(output_size) == 2 - self.output_size = output_size - - def __call__(self, sample): - imidx, image, label = sample["imidx"], sample["image"], sample["label"] - - if random.random() >= 0.5: - image = image[::-1] - label = label[::-1] - - h, w = image.shape[:2] - new_h, new_w = self.output_size - - top = np.random.randint(0, h - new_h) - left = np.random.randint(0, w - new_w) - - image = image[top : top + new_h, left : left + new_w] - label = label[top : top + new_h, left : left + new_w] - - return {"imidx": imidx, "image": image, "label": label} - - -class ToTensor(object): - """Convert ndarrays in sample to Tensors.""" - - def __call__(self, sample): - - imidx, image, label = sample["imidx"], sample["image"], sample["label"] - - tmpImg = np.zeros((image.shape[0], image.shape[1], 3)) - tmpLbl = np.zeros(label.shape) - - image = image / np.max(image) - if np.max(label) < 1e-6: - label = label - else: - label = label / np.max(label) - - if image.shape[2] == 1: - tmpImg[:, :, 0] = (image[:, :, 0] - 0.485) / 0.229 - tmpImg[:, :, 1] = (image[:, :, 0] - 0.485) / 0.229 - tmpImg[:, :, 2] = (image[:, :, 0] - 0.485) / 0.229 - else: - tmpImg[:, :, 0] = (image[:, :, 0] - 0.485) / 0.229 - tmpImg[:, :, 1] = (image[:, :, 1] - 0.456) / 0.224 - tmpImg[:, :, 2] = (image[:, :, 2] - 0.406) / 0.225 - - tmpLbl[:, :, 0] = label[:, :, 0] - - # change the r,g,b to b,r,g from [0,255] to [0,1] - # transforms.Normalize(mean = (0.485, 0.456, 0.406), std = (0.229, 0.224, 0.225)) - tmpImg = tmpImg.transpose((2, 0, 1)) - tmpLbl = label.transpose((2, 0, 1)) - - return { - "imidx": torch.from_numpy(imidx), - "image": torch.from_numpy(tmpImg), - "label": torch.from_numpy(tmpLbl), - } - - -class ToTensorLab(object): - """Convert ndarrays in sample to Tensors.""" - - def __init__(self, flag=0): - self.flag = flag - - def __call__(self, sample): - - imidx, image, label = sample["imidx"], sample["image"], sample["label"] - - tmpLbl = np.zeros(label.shape) - - if np.max(label) < 1e-6: - label = label - else: - label = label / np.max(label) - - # change the color space - if self.flag == 2: # with rgb and Lab colors - tmpImg = np.zeros((image.shape[0], image.shape[1], 6)) - tmpImgt = np.zeros((image.shape[0], image.shape[1], 3)) - if image.shape[2] == 1: - tmpImgt[:, :, 0] = image[:, :, 0] - tmpImgt[:, :, 1] = image[:, :, 0] - tmpImgt[:, :, 2] = image[:, :, 0] - else: - tmpImgt = image - tmpImgtl = color.rgb2lab(tmpImgt) - - # nomalize image to range [0,1] - tmpImg[:, :, 0] = (tmpImgt[:, :, 0] - np.min(tmpImgt[:, :, 0])) / ( - np.max(tmpImgt[:, :, 0]) - np.min(tmpImgt[:, :, 0]) - ) - tmpImg[:, :, 1] = (tmpImgt[:, :, 1] - np.min(tmpImgt[:, :, 1])) / ( - np.max(tmpImgt[:, :, 1]) - np.min(tmpImgt[:, :, 1]) - ) - tmpImg[:, :, 2] = (tmpImgt[:, :, 2] - np.min(tmpImgt[:, :, 2])) / ( - np.max(tmpImgt[:, :, 2]) - np.min(tmpImgt[:, :, 2]) - ) - tmpImg[:, :, 3] = (tmpImgtl[:, :, 0] - np.min(tmpImgtl[:, :, 0])) / ( - np.max(tmpImgtl[:, :, 0]) - np.min(tmpImgtl[:, :, 0]) - ) - tmpImg[:, :, 4] = (tmpImgtl[:, :, 1] - np.min(tmpImgtl[:, :, 1])) / ( - np.max(tmpImgtl[:, :, 1]) - np.min(tmpImgtl[:, :, 1]) - ) - tmpImg[:, :, 5] = (tmpImgtl[:, :, 2] - np.min(tmpImgtl[:, :, 2])) / ( - np.max(tmpImgtl[:, :, 2]) - np.min(tmpImgtl[:, :, 2]) - ) - - # tmpImg = tmpImg/(np.max(tmpImg)-np.min(tmpImg)) - - tmpImg[:, :, 0] = (tmpImg[:, :, 0] - np.mean(tmpImg[:, :, 0])) / np.std( - tmpImg[:, :, 0] - ) - tmpImg[:, :, 1] = (tmpImg[:, :, 1] - np.mean(tmpImg[:, :, 1])) / np.std( - tmpImg[:, :, 1] - ) - tmpImg[:, :, 2] = (tmpImg[:, :, 2] - np.mean(tmpImg[:, :, 2])) / np.std( - tmpImg[:, :, 2] - ) - tmpImg[:, :, 3] = (tmpImg[:, :, 3] - np.mean(tmpImg[:, :, 3])) / np.std( - tmpImg[:, :, 3] - ) - tmpImg[:, :, 4] = (tmpImg[:, :, 4] - np.mean(tmpImg[:, :, 4])) / np.std( - tmpImg[:, :, 4] - ) - tmpImg[:, :, 5] = (tmpImg[:, :, 5] - np.mean(tmpImg[:, :, 5])) / np.std( - tmpImg[:, :, 5] - ) - - elif self.flag == 1: # with Lab color - tmpImg = np.zeros((image.shape[0], image.shape[1], 3)) - - if image.shape[2] == 1: - tmpImg[:, :, 0] = image[:, :, 0] - tmpImg[:, :, 1] = image[:, :, 0] - tmpImg[:, :, 2] = image[:, :, 0] - else: - tmpImg = image - - tmpImg = color.rgb2lab(tmpImg) - - # tmpImg = tmpImg/(np.max(tmpImg)-np.min(tmpImg)) - - tmpImg[:, :, 0] = (tmpImg[:, :, 0] - np.min(tmpImg[:, :, 0])) / ( - np.max(tmpImg[:, :, 0]) - np.min(tmpImg[:, :, 0]) - ) - tmpImg[:, :, 1] = (tmpImg[:, :, 1] - np.min(tmpImg[:, :, 1])) / ( - np.max(tmpImg[:, :, 1]) - np.min(tmpImg[:, :, 1]) - ) - tmpImg[:, :, 2] = (tmpImg[:, :, 2] - np.min(tmpImg[:, :, 2])) / ( - np.max(tmpImg[:, :, 2]) - np.min(tmpImg[:, :, 2]) - ) - - tmpImg[:, :, 0] = (tmpImg[:, :, 0] - np.mean(tmpImg[:, :, 0])) / np.std( - tmpImg[:, :, 0] - ) - tmpImg[:, :, 1] = (tmpImg[:, :, 1] - np.mean(tmpImg[:, :, 1])) / np.std( - tmpImg[:, :, 1] - ) - tmpImg[:, :, 2] = (tmpImg[:, :, 2] - np.mean(tmpImg[:, :, 2])) / np.std( - tmpImg[:, :, 2] - ) - - else: # with rgb color - tmpImg = np.zeros((image.shape[0], image.shape[1], 3)) - image = image / np.max(image) - if image.shape[2] == 1: - tmpImg[:, :, 0] = (image[:, :, 0] - 0.485) / 0.229 - tmpImg[:, :, 1] = (image[:, :, 0] - 0.485) / 0.229 - tmpImg[:, :, 2] = (image[:, :, 0] - 0.485) / 0.229 - else: - tmpImg[:, :, 0] = (image[:, :, 0] - 0.485) / 0.229 - tmpImg[:, :, 1] = (image[:, :, 1] - 0.456) / 0.224 - tmpImg[:, :, 2] = (image[:, :, 2] - 0.406) / 0.225 - - tmpLbl[:, :, 0] = label[:, :, 0] - - # change the r,g,b to b,r,g from [0,255] to [0,1] - # transforms.Normalize(mean = (0.485, 0.456, 0.406), std = (0.229, 0.224, 0.225)) - tmpImg = tmpImg.transpose((2, 0, 1)) - tmpLbl = label.transpose((2, 0, 1)) - - return { - "imidx": torch.from_numpy(imidx), - "image": torch.from_numpy(tmpImg), - "label": torch.from_numpy(tmpLbl), - } - - -class SalObjDataset(Dataset): - def __init__(self, img_name_list, lbl_name_list, transform=None): - # self.root_dir = root_dir - # self.image_name_list = glob.glob(image_dir+'*.png') - # self.label_name_list = glob.glob(label_dir+'*.png') - self.image_name_list = img_name_list - self.label_name_list = lbl_name_list - self.transform = transform - - def __len__(self): - return len(self.image_name_list) - - def __getitem__(self, idx): - - # image = Image.open(self.image_name_list[idx])#io.imread(self.image_name_list[idx]) - # label = Image.open(self.label_name_list[idx])#io.imread(self.label_name_list[idx]) - - image = io.imread(self.image_name_list[idx]) - imname = self.image_name_list[idx] - imidx = np.array([idx]) - - if 0 == len(self.label_name_list): - label_3 = np.zeros(image.shape) - else: - label_3 = io.imread(self.label_name_list[idx]) - - label = np.zeros(label_3.shape[0:2]) - if 3 == len(label_3.shape): - label = label_3[:, :, 0] - elif 2 == len(label_3.shape): - label = label_3 - - if 3 == len(image.shape) and 2 == len(label.shape): - label = label[:, :, np.newaxis] - elif 2 == len(image.shape) and 2 == len(label.shape): - image = image[:, :, np.newaxis] - label = label[:, :, np.newaxis] - - sample = {"imidx": imidx, "image": image, "label": label} - - if self.transform: - sample = self.transform(sample) - - return sample diff --git a/src/rembg/u2net/detect.py b/src/rembg/u2net/detect.py deleted file mode 100644 index 5afa4757..00000000 --- a/src/rembg/u2net/detect.py +++ /dev/null @@ -1,127 +0,0 @@ -import errno -import os -import sys -import urllib.request - -import numpy as np -import requests -import torch -import torch.nn as nn -import torch.nn.functional as F -import torchvision -from PIL import Image -from skimage import transform -from torchvision import transforms -from tqdm import tqdm - -from . import data_loader, u2net - - -def download(url, fname, path): - if os.path.exists(path): - return - - resp = requests.get(url, stream=True) - total = int(resp.headers.get("content-length", 0)) - with open(path, "wb") as file, tqdm( - desc=fname, total=total, unit="iB", unit_scale=True, unit_divisor=1024, - ) as bar: - for data in resp.iter_content(chunk_size=1024): - size = file.write(data) - bar.update(size) - - -def load_model(model_name: str = "u2net"): - os.makedirs(os.path.expanduser(os.path.join("~", ".u2net")), exist_ok=True) - - if model_name == "u2netp": - net = u2net.U2NETP(3, 1) - path = os.path.expanduser(os.path.join("~", ".u2net", model_name)) - download( - "https://www.dropbox.com/s/usb1fyiuh8as5gi/u2netp.pth?dl=1", - "u2netp.pth", - path, - ) - elif model_name == "u2net": - net = u2net.U2NET(3, 1) - path = os.path.expanduser(os.path.join("~", ".u2net", model_name)) - download( - "https://www.dropbox.com/s/kdu5mhose1clds0/u2net.pth?dl=1", - "u2net.pth", - path, - ) - else: - print("Choose between u2net or u2netp", file=sys.stderr) - - try: - if torch.cuda.is_available(): - net.load_state_dict(torch.load(path)) - net.to(torch.device("cuda")) - else: - net.load_state_dict(torch.load(path, map_location="cpu",)) - except FileNotFoundError: - raise FileNotFoundError( - errno.ENOENT, os.strerror(errno.ENOENT), model_name + ".pth" - ) - - net.eval() - - return net - - -def norm_pred(d): - ma = torch.max(d) - mi = torch.min(d) - dn = (d - mi) / (ma - mi) - - return dn - - -def preprocess(image): - label_3 = np.zeros(image.shape) - label = np.zeros(label_3.shape[0:2]) - - if 3 == len(label_3.shape): - label = label_3[:, :, 0] - elif 2 == len(label_3.shape): - label = label_3 - - if 3 == len(image.shape) and 2 == len(label.shape): - label = label[:, :, np.newaxis] - elif 2 == len(image.shape) and 2 == len(label.shape): - image = image[:, :, np.newaxis] - label = label[:, :, np.newaxis] - - transform = transforms.Compose( - [data_loader.RescaleT(320), data_loader.ToTensorLab(flag=0)] - ) - sample = transform({"imidx": np.array([0]), "image": image, "label": label}) - - return sample - - -def predict(net, item): - - sample = preprocess(item) - - with torch.no_grad(): - - if torch.cuda.is_available(): - inputs_test = torch.cuda.FloatTensor( - sample["image"].unsqueeze(0).cuda().float() - ) - else: - inputs_test = torch.FloatTensor(sample["image"].unsqueeze(0).float()) - - d1, d2, d3, d4, d5, d6, d7 = net(inputs_test) - - pred = d1[:, 0, :, :] - predict = norm_pred(pred) - - predict = predict.squeeze() - predict_np = predict.cpu().detach().numpy() - img = Image.fromarray(predict_np * 255).convert("RGB") - - del d1, d2, d3, d4, d5, d6, d7, pred, predict, predict_np, inputs_test, sample - - return img diff --git a/src/rembg/u2net/u2net.py b/src/rembg/u2net/u2net.py deleted file mode 100644 index 7ac24aa1..00000000 --- a/src/rembg/u2net/u2net.py +++ /dev/null @@ -1,541 +0,0 @@ -import torch -import torch.nn as nn -import torch.nn.functional as F -from torchvision import models - - -class REBNCONV(nn.Module): - def __init__(self, in_ch=3, out_ch=3, dirate=1): - super(REBNCONV, self).__init__() - - self.conv_s1 = nn.Conv2d( - in_ch, out_ch, 3, padding=1 * dirate, dilation=1 * dirate - ) - self.bn_s1 = nn.BatchNorm2d(out_ch) - self.relu_s1 = nn.ReLU(inplace=True) - - def forward(self, x): - - hx = x - xout = self.relu_s1(self.bn_s1(self.conv_s1(hx))) - - return xout - - -## upsample tensor 'src' to have the same spatial size with tensor 'tar' -def _upsample_like(src, tar): - - src = F.interpolate(src, size=tar.shape[2:], mode="bilinear", align_corners=False) - - return src - - -### RSU-7 ### -class RSU7(nn.Module): # UNet07DRES(nn.Module): - def __init__(self, in_ch=3, mid_ch=12, out_ch=3): - super(RSU7, self).__init__() - - self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) - - self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) - self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool5 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=1) - - self.rebnconv7 = REBNCONV(mid_ch, mid_ch, dirate=2) - - self.rebnconv6d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1) - - def forward(self, x): - - hx = x - hxin = self.rebnconvin(hx) - - hx1 = self.rebnconv1(hxin) - hx = self.pool1(hx1) - - hx2 = self.rebnconv2(hx) - hx = self.pool2(hx2) - - hx3 = self.rebnconv3(hx) - hx = self.pool3(hx3) - - hx4 = self.rebnconv4(hx) - hx = self.pool4(hx4) - - hx5 = self.rebnconv5(hx) - hx = self.pool5(hx5) - - hx6 = self.rebnconv6(hx) - - hx7 = self.rebnconv7(hx6) - - hx6d = self.rebnconv6d(torch.cat((hx7, hx6), 1)) - hx6dup = _upsample_like(hx6d, hx5) - - hx5d = self.rebnconv5d(torch.cat((hx6dup, hx5), 1)) - hx5dup = _upsample_like(hx5d, hx4) - - hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1)) - hx4dup = _upsample_like(hx4d, hx3) - - hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1)) - hx3dup = _upsample_like(hx3d, hx2) - - hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) - hx2dup = _upsample_like(hx2d, hx1) - - hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1)) - - return hx1d + hxin - - -### RSU-6 ### -class RSU6(nn.Module): # UNet06DRES(nn.Module): - def __init__(self, in_ch=3, mid_ch=12, out_ch=3): - super(RSU6, self).__init__() - - self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) - - self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) - self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=1) - - self.rebnconv6 = REBNCONV(mid_ch, mid_ch, dirate=2) - - self.rebnconv5d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1) - - def forward(self, x): - - hx = x - - hxin = self.rebnconvin(hx) - - hx1 = self.rebnconv1(hxin) - hx = self.pool1(hx1) - - hx2 = self.rebnconv2(hx) - hx = self.pool2(hx2) - - hx3 = self.rebnconv3(hx) - hx = self.pool3(hx3) - - hx4 = self.rebnconv4(hx) - hx = self.pool4(hx4) - - hx5 = self.rebnconv5(hx) - - hx6 = self.rebnconv6(hx5) - - hx5d = self.rebnconv5d(torch.cat((hx6, hx5), 1)) - hx5dup = _upsample_like(hx5d, hx4) - - hx4d = self.rebnconv4d(torch.cat((hx5dup, hx4), 1)) - hx4dup = _upsample_like(hx4d, hx3) - - hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1)) - hx3dup = _upsample_like(hx3d, hx2) - - hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) - hx2dup = _upsample_like(hx2d, hx1) - - hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1)) - - return hx1d + hxin - - -### RSU-5 ### -class RSU5(nn.Module): # UNet05DRES(nn.Module): - def __init__(self, in_ch=3, mid_ch=12, out_ch=3): - super(RSU5, self).__init__() - - self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) - - self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) - self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=1) - - self.rebnconv5 = REBNCONV(mid_ch, mid_ch, dirate=2) - - self.rebnconv4d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1) - - def forward(self, x): - - hx = x - - hxin = self.rebnconvin(hx) - - hx1 = self.rebnconv1(hxin) - hx = self.pool1(hx1) - - hx2 = self.rebnconv2(hx) - hx = self.pool2(hx2) - - hx3 = self.rebnconv3(hx) - hx = self.pool3(hx3) - - hx4 = self.rebnconv4(hx) - - hx5 = self.rebnconv5(hx4) - - hx4d = self.rebnconv4d(torch.cat((hx5, hx4), 1)) - hx4dup = _upsample_like(hx4d, hx3) - - hx3d = self.rebnconv3d(torch.cat((hx4dup, hx3), 1)) - hx3dup = _upsample_like(hx3d, hx2) - - hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) - hx2dup = _upsample_like(hx2d, hx1) - - hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1)) - - return hx1d + hxin - - -### RSU-4 ### -class RSU4(nn.Module): # UNet04DRES(nn.Module): - def __init__(self, in_ch=3, mid_ch=12, out_ch=3): - super(RSU4, self).__init__() - - self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) - - self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) - self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=1) - self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=1) - - self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=2) - - self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=1) - self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1) - - def forward(self, x): - - hx = x - - hxin = self.rebnconvin(hx) - - hx1 = self.rebnconv1(hxin) - hx = self.pool1(hx1) - - hx2 = self.rebnconv2(hx) - hx = self.pool2(hx2) - - hx3 = self.rebnconv3(hx) - - hx4 = self.rebnconv4(hx3) - - hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1)) - hx3dup = _upsample_like(hx3d, hx2) - - hx2d = self.rebnconv2d(torch.cat((hx3dup, hx2), 1)) - hx2dup = _upsample_like(hx2d, hx1) - - hx1d = self.rebnconv1d(torch.cat((hx2dup, hx1), 1)) - - return hx1d + hxin - - -### RSU-4F ### -class RSU4F(nn.Module): # UNet04FRES(nn.Module): - def __init__(self, in_ch=3, mid_ch=12, out_ch=3): - super(RSU4F, self).__init__() - - self.rebnconvin = REBNCONV(in_ch, out_ch, dirate=1) - - self.rebnconv1 = REBNCONV(out_ch, mid_ch, dirate=1) - self.rebnconv2 = REBNCONV(mid_ch, mid_ch, dirate=2) - self.rebnconv3 = REBNCONV(mid_ch, mid_ch, dirate=4) - - self.rebnconv4 = REBNCONV(mid_ch, mid_ch, dirate=8) - - self.rebnconv3d = REBNCONV(mid_ch * 2, mid_ch, dirate=4) - self.rebnconv2d = REBNCONV(mid_ch * 2, mid_ch, dirate=2) - self.rebnconv1d = REBNCONV(mid_ch * 2, out_ch, dirate=1) - - def forward(self, x): - - hx = x - - hxin = self.rebnconvin(hx) - - hx1 = self.rebnconv1(hxin) - hx2 = self.rebnconv2(hx1) - hx3 = self.rebnconv3(hx2) - - hx4 = self.rebnconv4(hx3) - - hx3d = self.rebnconv3d(torch.cat((hx4, hx3), 1)) - hx2d = self.rebnconv2d(torch.cat((hx3d, hx2), 1)) - hx1d = self.rebnconv1d(torch.cat((hx2d, hx1), 1)) - - return hx1d + hxin - - -##### U^2-Net #### -class U2NET(nn.Module): - def __init__(self, in_ch=3, out_ch=1): - super(U2NET, self).__init__() - - self.stage1 = RSU7(in_ch, 32, 64) - self.pool12 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage2 = RSU6(64, 32, 128) - self.pool23 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage3 = RSU5(128, 64, 256) - self.pool34 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage4 = RSU4(256, 128, 512) - self.pool45 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage5 = RSU4F(512, 256, 512) - self.pool56 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage6 = RSU4F(512, 256, 512) - - # decoder - self.stage5d = RSU4F(1024, 256, 512) - self.stage4d = RSU4(1024, 128, 256) - self.stage3d = RSU5(512, 64, 128) - self.stage2d = RSU6(256, 32, 64) - self.stage1d = RSU7(128, 16, 64) - - self.side1 = nn.Conv2d(64, out_ch, 3, padding=1) - self.side2 = nn.Conv2d(64, out_ch, 3, padding=1) - self.side3 = nn.Conv2d(128, out_ch, 3, padding=1) - self.side4 = nn.Conv2d(256, out_ch, 3, padding=1) - self.side5 = nn.Conv2d(512, out_ch, 3, padding=1) - self.side6 = nn.Conv2d(512, out_ch, 3, padding=1) - - self.outconv = nn.Conv2d(6, out_ch, 1) - - def forward(self, x): - - hx = x - - # stage 1 - hx1 = self.stage1(hx) - hx = self.pool12(hx1) - - # stage 2 - hx2 = self.stage2(hx) - hx = self.pool23(hx2) - - # stage 3 - hx3 = self.stage3(hx) - hx = self.pool34(hx3) - - # stage 4 - hx4 = self.stage4(hx) - hx = self.pool45(hx4) - - # stage 5 - hx5 = self.stage5(hx) - hx = self.pool56(hx5) - - # stage 6 - hx6 = self.stage6(hx) - hx6up = _upsample_like(hx6, hx5) - - # -------------------- decoder -------------------- - hx5d = self.stage5d(torch.cat((hx6up, hx5), 1)) - hx5dup = _upsample_like(hx5d, hx4) - - hx4d = self.stage4d(torch.cat((hx5dup, hx4), 1)) - hx4dup = _upsample_like(hx4d, hx3) - - hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1)) - hx3dup = _upsample_like(hx3d, hx2) - - hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1)) - hx2dup = _upsample_like(hx2d, hx1) - - hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1)) - - # side output - d1 = self.side1(hx1d) - - d2 = self.side2(hx2d) - d2 = _upsample_like(d2, d1) - - d3 = self.side3(hx3d) - d3 = _upsample_like(d3, d1) - - d4 = self.side4(hx4d) - d4 = _upsample_like(d4, d1) - - d5 = self.side5(hx5d) - d5 = _upsample_like(d5, d1) - - d6 = self.side6(hx6) - d6 = _upsample_like(d6, d1) - - d0 = self.outconv(torch.cat((d1, d2, d3, d4, d5, d6), 1)) - - return ( - torch.sigmoid(d0), - torch.sigmoid(d1), - torch.sigmoid(d2), - torch.sigmoid(d3), - torch.sigmoid(d4), - torch.sigmoid(d5), - torch.sigmoid(d6), - ) - - -### U^2-Net small ### -class U2NETP(nn.Module): - def __init__(self, in_ch=3, out_ch=1): - super(U2NETP, self).__init__() - - self.stage1 = RSU7(in_ch, 16, 64) - self.pool12 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage2 = RSU6(64, 16, 64) - self.pool23 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage3 = RSU5(64, 16, 64) - self.pool34 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage4 = RSU4(64, 16, 64) - self.pool45 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage5 = RSU4F(64, 16, 64) - self.pool56 = nn.MaxPool2d(2, stride=2, ceil_mode=True) - - self.stage6 = RSU4F(64, 16, 64) - - # decoder - self.stage5d = RSU4F(128, 16, 64) - self.stage4d = RSU4(128, 16, 64) - self.stage3d = RSU5(128, 16, 64) - self.stage2d = RSU6(128, 16, 64) - self.stage1d = RSU7(128, 16, 64) - - self.side1 = nn.Conv2d(64, out_ch, 3, padding=1) - self.side2 = nn.Conv2d(64, out_ch, 3, padding=1) - self.side3 = nn.Conv2d(64, out_ch, 3, padding=1) - self.side4 = nn.Conv2d(64, out_ch, 3, padding=1) - self.side5 = nn.Conv2d(64, out_ch, 3, padding=1) - self.side6 = nn.Conv2d(64, out_ch, 3, padding=1) - - self.outconv = nn.Conv2d(6, out_ch, 1) - - def forward(self, x): - - hx = x - - # stage 1 - hx1 = self.stage1(hx) - hx = self.pool12(hx1) - - # stage 2 - hx2 = self.stage2(hx) - hx = self.pool23(hx2) - - # stage 3 - hx3 = self.stage3(hx) - hx = self.pool34(hx3) - - # stage 4 - hx4 = self.stage4(hx) - hx = self.pool45(hx4) - - # stage 5 - hx5 = self.stage5(hx) - hx = self.pool56(hx5) - - # stage 6 - hx6 = self.stage6(hx) - hx6up = _upsample_like(hx6, hx5) - - # decoder - hx5d = self.stage5d(torch.cat((hx6up, hx5), 1)) - hx5dup = _upsample_like(hx5d, hx4) - - hx4d = self.stage4d(torch.cat((hx5dup, hx4), 1)) - hx4dup = _upsample_like(hx4d, hx3) - - hx3d = self.stage3d(torch.cat((hx4dup, hx3), 1)) - hx3dup = _upsample_like(hx3d, hx2) - - hx2d = self.stage2d(torch.cat((hx3dup, hx2), 1)) - hx2dup = _upsample_like(hx2d, hx1) - - hx1d = self.stage1d(torch.cat((hx2dup, hx1), 1)) - - # side output - d1 = self.side1(hx1d) - - d2 = self.side2(hx2d) - d2 = _upsample_like(d2, d1) - - d3 = self.side3(hx3d) - d3 = _upsample_like(d3, d1) - - d4 = self.side4(hx4d) - d4 = _upsample_like(d4, d1) - - d5 = self.side5(hx5d) - d5 = _upsample_like(d5, d1) - - d6 = self.side6(hx6) - d6 = _upsample_like(d6, d1) - - d0 = self.outconv(torch.cat((d1, d2, d3, d4, d5, d6), 1)) - - return ( - torch.sigmoid(d0), - torch.sigmoid(d1), - torch.sigmoid(d2), - torch.sigmoid(d3), - torch.sigmoid(d4), - torch.sigmoid(d5), - torch.sigmoid(d6), - ) diff --git a/tests/fixtures/anime-girl-1.jpg b/tests/fixtures/anime-girl-1.jpg new file mode 100644 index 00000000..5f404ece Binary files /dev/null and b/tests/fixtures/anime-girl-1.jpg differ diff --git a/tests/fixtures/car-1.jpg b/tests/fixtures/car-1.jpg new file mode 100644 index 00000000..c6dc1e6d Binary files /dev/null and b/tests/fixtures/car-1.jpg differ diff --git a/tests/fixtures/cloth-1.jpg b/tests/fixtures/cloth-1.jpg new file mode 100644 index 00000000..a33ba7ee Binary files /dev/null and b/tests/fixtures/cloth-1.jpg differ diff --git a/tests/results/anime-girl-1.isnet-anime.png b/tests/results/anime-girl-1.isnet-anime.png new file mode 100644 index 00000000..b4159337 Binary files /dev/null and b/tests/results/anime-girl-1.isnet-anime.png differ diff --git a/tests/results/anime-girl-1.isnet-general-use.png b/tests/results/anime-girl-1.isnet-general-use.png new file mode 100644 index 00000000..f4597194 Binary files /dev/null and b/tests/results/anime-girl-1.isnet-general-use.png differ diff --git a/tests/results/anime-girl-1.sam.png b/tests/results/anime-girl-1.sam.png new file mode 100644 index 00000000..daf576f0 Binary files /dev/null and b/tests/results/anime-girl-1.sam.png differ diff --git a/tests/results/anime-girl-1.silueta.png b/tests/results/anime-girl-1.silueta.png new file mode 100644 index 00000000..8f076d19 Binary files /dev/null and b/tests/results/anime-girl-1.silueta.png differ diff --git a/tests/results/anime-girl-1.u2net.png b/tests/results/anime-girl-1.u2net.png new file mode 100644 index 00000000..97feccbb Binary files /dev/null and b/tests/results/anime-girl-1.u2net.png differ diff --git a/tests/results/anime-girl-1.u2net_cloth_seg.png b/tests/results/anime-girl-1.u2net_cloth_seg.png new file mode 100644 index 00000000..e56b7af4 Binary files /dev/null and b/tests/results/anime-girl-1.u2net_cloth_seg.png differ diff --git a/tests/results/anime-girl-1.u2net_human_seg.png b/tests/results/anime-girl-1.u2net_human_seg.png new file mode 100644 index 00000000..805b94d0 Binary files /dev/null and b/tests/results/anime-girl-1.u2net_human_seg.png differ diff --git a/tests/results/anime-girl-1.u2netp.png b/tests/results/anime-girl-1.u2netp.png new file mode 100644 index 00000000..24b2f8d5 Binary files /dev/null and b/tests/results/anime-girl-1.u2netp.png differ diff --git a/tests/results/car-1.isnet-anime.png b/tests/results/car-1.isnet-anime.png new file mode 100644 index 00000000..37f4becb Binary files /dev/null and b/tests/results/car-1.isnet-anime.png differ diff --git a/tests/results/car-1.isnet-general-use.png b/tests/results/car-1.isnet-general-use.png new file mode 100644 index 00000000..ab3836c7 Binary files /dev/null and b/tests/results/car-1.isnet-general-use.png differ diff --git a/tests/results/car-1.sam.png b/tests/results/car-1.sam.png new file mode 100644 index 00000000..f36b6695 Binary files /dev/null and b/tests/results/car-1.sam.png differ diff --git a/tests/results/car-1.silueta.png b/tests/results/car-1.silueta.png new file mode 100644 index 00000000..9fd2086f Binary files /dev/null and b/tests/results/car-1.silueta.png differ diff --git a/tests/results/car-1.u2net.png b/tests/results/car-1.u2net.png new file mode 100644 index 00000000..88338ea8 Binary files /dev/null and b/tests/results/car-1.u2net.png differ diff --git a/tests/results/car-1.u2net_cloth_seg.png b/tests/results/car-1.u2net_cloth_seg.png new file mode 100644 index 00000000..64ffd88b Binary files /dev/null and b/tests/results/car-1.u2net_cloth_seg.png differ diff --git a/tests/results/car-1.u2net_human_seg.png b/tests/results/car-1.u2net_human_seg.png new file mode 100644 index 00000000..11052054 Binary files /dev/null and b/tests/results/car-1.u2net_human_seg.png differ diff --git a/tests/results/car-1.u2netp.png b/tests/results/car-1.u2netp.png new file mode 100644 index 00000000..7ad593f6 Binary files /dev/null and b/tests/results/car-1.u2netp.png differ diff --git a/tests/results/cloth-1.isnet-anime.png b/tests/results/cloth-1.isnet-anime.png new file mode 100644 index 00000000..f9db40e1 Binary files /dev/null and b/tests/results/cloth-1.isnet-anime.png differ diff --git a/tests/results/cloth-1.isnet-general-use.png b/tests/results/cloth-1.isnet-general-use.png new file mode 100644 index 00000000..c5a0e3de Binary files /dev/null and b/tests/results/cloth-1.isnet-general-use.png differ diff --git a/tests/results/cloth-1.sam.png b/tests/results/cloth-1.sam.png new file mode 100644 index 00000000..664a7dcf Binary files /dev/null and b/tests/results/cloth-1.sam.png differ diff --git a/tests/results/cloth-1.silueta.png b/tests/results/cloth-1.silueta.png new file mode 100644 index 00000000..5d98d322 Binary files /dev/null and b/tests/results/cloth-1.silueta.png differ diff --git a/tests/results/cloth-1.u2net.png b/tests/results/cloth-1.u2net.png new file mode 100644 index 00000000..df5fb741 Binary files /dev/null and b/tests/results/cloth-1.u2net.png differ diff --git a/tests/results/cloth-1.u2net_cloth_seg.png b/tests/results/cloth-1.u2net_cloth_seg.png new file mode 100644 index 00000000..05557dd6 Binary files /dev/null and b/tests/results/cloth-1.u2net_cloth_seg.png differ diff --git a/tests/results/cloth-1.u2net_human_seg.png b/tests/results/cloth-1.u2net_human_seg.png new file mode 100644 index 00000000..efb45331 Binary files /dev/null and b/tests/results/cloth-1.u2net_human_seg.png differ diff --git a/tests/results/cloth-1.u2netp.png b/tests/results/cloth-1.u2netp.png new file mode 100644 index 00000000..5ec1fb2f Binary files /dev/null and b/tests/results/cloth-1.u2netp.png differ diff --git a/tests/test_remove.py b/tests/test_remove.py new file mode 100644 index 00000000..7f9901c4 --- /dev/null +++ b/tests/test_remove.py @@ -0,0 +1,64 @@ +from io import BytesIO +from pathlib import Path + +from imagehash import phash as hash_img +from PIL import Image + +from rembg import new_session, remove + +here = Path(__file__).parent.resolve() + +def test_remove(): + kwargs = { + "sam": { + "anime-girl-1" : { + "input_points": [[400, 165]], + "input_labels": [1], + }, + + "car-1" : { + "input_points": [[250, 200]], + "input_labels": [1], + }, + + "cloth-1" : { + "input_points": [[370, 495]], + "input_labels": [1], + }, + } + } + + for model in [ + "u2net", + "u2netp", + "u2net_human_seg", + "u2net_cloth_seg", + "silueta", + "isnet-general-use", + "isnet-anime", + "sam" + ]: + for picture in ["anime-girl-1", "car-1", "cloth-1"]: + image_path = Path(here / "fixtures" / f"{picture}.jpg") + image = image_path.read_bytes() + + actual = remove(image, session=new_session(model), **kwargs.get(model, {}).get(picture, {})) + actual_hash = hash_img(Image.open(BytesIO(actual))) + + expected_path = Path(here / "results" / f"{picture}.{model}.png") + # Uncomment to update the expected results + # f = open(expected_path, "wb") + # f.write(actual) + # f.close() + + expected = expected_path.read_bytes() + expected_hash = hash_img(Image.open(BytesIO(expected))) + + print(f"image_path: {image_path}") + print(f"expected_path: {expected_path}") + print(f"actual_hash: {actual_hash}") + print(f"expected_hash: {expected_hash}") + print(f"actual_hash == expected_hash: {actual_hash == expected_hash}") + print("---\n") + + assert actual_hash == expected_hash diff --git a/versioneer.py b/versioneer.py new file mode 100644 index 00000000..d70f31b1 --- /dev/null +++ b/versioneer.py @@ -0,0 +1,2163 @@ +# Version: 0.21 + +"""The Versioneer - like a rocketeer, but for versions. + +The Versioneer +============== + +* like a rocketeer, but for versions! +* https://github.com/python-versioneer/python-versioneer +* Brian Warner +* License: Public Domain +* Compatible with: Python 3.6, 3.7, 3.8, 3.9 and pypy3 +* [![Latest Version][pypi-image]][pypi-url] +* [![Build Status][travis-image]][travis-url] + +This is a tool for managing a recorded version number in distutils-based +python projects. The goal is to remove the tedious and error-prone "update +the embedded version string" step from your release process. Making a new +release should be as easy as recording a new tag in your version-control +system, and maybe making new tarballs. + + +## Quick Install + +* `pip install versioneer` to somewhere in your $PATH +* add a `[versioneer]` section to your setup.cfg (see [Install](INSTALL.md)) +* run `versioneer install` in your source tree, commit the results +* Verify version information with `python setup.py version` + +## Version Identifiers + +Source trees come from a variety of places: + +* a version-control system checkout (mostly used by developers) +* a nightly tarball, produced by build automation +* a snapshot tarball, produced by a web-based VCS browser, like github's + "tarball from tag" feature +* a release tarball, produced by "setup.py sdist", distributed through PyPI + +Within each source tree, the version identifier (either a string or a number, +this tool is format-agnostic) can come from a variety of places: + +* ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows + about recent "tags" and an absolute revision-id +* the name of the directory into which the tarball was unpacked +* an expanded VCS keyword ($Id$, etc) +* a `_version.py` created by some earlier build step + +For released software, the version identifier is closely related to a VCS +tag. Some projects use tag names that include more than just the version +string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool +needs to strip the tag prefix to extract the version identifier. For +unreleased software (between tags), the version identifier should provide +enough information to help developers recreate the same tree, while also +giving them an idea of roughly how old the tree is (after version 1.2, before +version 1.3). Many VCS systems can report a description that captures this, +for example `git describe --tags --dirty --always` reports things like +"0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the +0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has +uncommitted changes). + +The version identifier is used for multiple purposes: + +* to allow the module to self-identify its version: `myproject.__version__` +* to choose a name and prefix for a 'setup.py sdist' tarball + +## Theory of Operation + +Versioneer works by adding a special `_version.py` file into your source +tree, where your `__init__.py` can import it. This `_version.py` knows how to +dynamically ask the VCS tool for version information at import time. + +`_version.py` also contains `$Revision$` markers, and the installation +process marks `_version.py` to have this marker rewritten with a tag name +during the `git archive` command. As a result, generated tarballs will +contain enough information to get the proper version. + +To allow `setup.py` to compute a version too, a `versioneer.py` is added to +the top level of your source tree, next to `setup.py` and the `setup.cfg` +that configures it. This overrides several distutils/setuptools commands to +compute the version when invoked, and changes `setup.py build` and `setup.py +sdist` to replace `_version.py` with a small static file that contains just +the generated version data. + +## Installation + +See [INSTALL.md](./INSTALL.md) for detailed installation instructions. + +## Version-String Flavors + +Code which uses Versioneer can learn about its version string at runtime by +importing `_version` from your main `__init__.py` file and running the +`get_versions()` function. From the "outside" (e.g. in `setup.py`), you can +import the top-level `versioneer.py` and run `get_versions()`. + +Both functions return a dictionary with different flavors of version +information: + +* `['version']`: A condensed version string, rendered using the selected + style. This is the most commonly used value for the project's version + string. The default "pep440" style yields strings like `0.11`, + `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section + below for alternative styles. + +* `['full-revisionid']`: detailed revision identifier. For Git, this is the + full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". + +* `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the + commit date in ISO 8601 format. This will be None if the date is not + available. + +* `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that + this is only accurate if run in a VCS checkout, otherwise it is likely to + be False or None + +* `['error']`: if the version string could not be computed, this will be set + to a string describing the problem, otherwise it will be None. It may be + useful to throw an exception in setup.py if this is set, to avoid e.g. + creating tarballs with a version string of "unknown". + +Some variants are more useful than others. Including `full-revisionid` in a +bug report should allow developers to reconstruct the exact code being tested +(or indicate the presence of local changes that should be shared with the +developers). `version` is suitable for display in an "about" box or a CLI +`--version` output: it can be easily compared against release notes and lists +of bugs fixed in various releases. + +The installer adds the following text to your `__init__.py` to place a basic +version in `YOURPROJECT.__version__`: + + from ._version import get_versions + __version__ = get_versions()['version'] + del get_versions + +## Styles + +The setup.cfg `style=` configuration controls how the VCS information is +rendered into a version string. + +The default style, "pep440", produces a PEP440-compliant string, equal to the +un-prefixed tag name for actual releases, and containing an additional "local +version" section with more detail for in-between builds. For Git, this is +TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags +--dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the +tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and +that this commit is two revisions ("+2") beyond the "0.11" tag. For released +software (exactly equal to a known tag), the identifier will only contain the +stripped tag, e.g. "0.11". + +Other styles are available. See [details.md](details.md) in the Versioneer +source tree for descriptions. + +## Debugging + +Versioneer tries to avoid fatal errors: if something goes wrong, it will tend +to return a version of "0+unknown". To investigate the problem, run `setup.py +version`, which will run the version-lookup code in a verbose mode, and will +display the full contents of `get_versions()` (including the `error` string, +which may help identify what went wrong). + +## Known Limitations + +Some situations are known to cause problems for Versioneer. This details the +most significant ones. More can be found on Github +[issues page](https://github.com/python-versioneer/python-versioneer/issues). + +### Subprojects + +Versioneer has limited support for source trees in which `setup.py` is not in +the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are +two common reasons why `setup.py` might not be in the root: + +* Source trees which contain multiple subprojects, such as + [Buildbot](https://github.com/buildbot/buildbot), which contains both + "master" and "slave" subprojects, each with their own `setup.py`, + `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI + distributions (and upload multiple independently-installable tarballs). +* Source trees whose main purpose is to contain a C library, but which also + provide bindings to Python (and perhaps other languages) in subdirectories. + +Versioneer will look for `.git` in parent directories, and most operations +should get the right version string. However `pip` and `setuptools` have bugs +and implementation details which frequently cause `pip install .` from a +subproject directory to fail to find a correct version string (so it usually +defaults to `0+unknown`). + +`pip install --editable .` should work correctly. `setup.py install` might +work too. + +Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in +some later version. + +[Bug #38](https://github.com/python-versioneer/python-versioneer/issues/38) is tracking +this issue. The discussion in +[PR #61](https://github.com/python-versioneer/python-versioneer/pull/61) describes the +issue from the Versioneer side in more detail. +[pip PR#3176](https://github.com/pypa/pip/pull/3176) and +[pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve +pip to let Versioneer work correctly. + +Versioneer-0.16 and earlier only looked for a `.git` directory next to the +`setup.cfg`, so subprojects were completely unsupported with those releases. + +### Editable installs with setuptools <= 18.5 + +`setup.py develop` and `pip install --editable .` allow you to install a +project into a virtualenv once, then continue editing the source code (and +test) without re-installing after every change. + +"Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a +convenient way to specify executable scripts that should be installed along +with the python package. + +These both work as expected when using modern setuptools. When using +setuptools-18.5 or earlier, however, certain operations will cause +`pkg_resources.DistributionNotFound` errors when running the entrypoint +script, which must be resolved by re-installing the package. This happens +when the install happens with one version, then the egg_info data is +regenerated while a different version is checked out. Many setup.py commands +cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into +a different virtualenv), so this can be surprising. + +[Bug #83](https://github.com/python-versioneer/python-versioneer/issues/83) describes +this one, but upgrading to a newer version of setuptools should probably +resolve it. + + +## Updating Versioneer + +To upgrade your project to a new release of Versioneer, do the following: + +* install the new Versioneer (`pip install -U versioneer` or equivalent) +* edit `setup.cfg`, if necessary, to include any new configuration settings + indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details. +* re-run `versioneer install` in your source tree, to replace + `SRC/_version.py` +* commit any changed files + +## Future Directions + +This tool is designed to make it easily extended to other version-control +systems: all VCS-specific components are in separate directories like +src/git/ . The top-level `versioneer.py` script is assembled from these +components by running make-versioneer.py . In the future, make-versioneer.py +will take a VCS name as an argument, and will construct a version of +`versioneer.py` that is specific to the given VCS. It might also take the +configuration arguments that are currently provided manually during +installation by editing setup.py . Alternatively, it might go the other +direction and include code from all supported VCS systems, reducing the +number of intermediate scripts. + +## Similar projects + +* [setuptools_scm](https://github.com/pypa/setuptools_scm/) - a non-vendored build-time + dependency +* [minver](https://github.com/jbweston/miniver) - a lightweight reimplementation of + versioneer +* [versioningit](https://github.com/jwodder/versioningit) - a PEP 518-based setuptools + plugin + +## License + +To make Versioneer easier to embed, all its code is dedicated to the public +domain. The `_version.py` that it creates is also in the public domain. +Specifically, both are released under the Creative Commons "Public Domain +Dedication" license (CC0-1.0), as described in +https://creativecommons.org/publicdomain/zero/1.0/ . + +[pypi-image]: https://img.shields.io/pypi/v/versioneer.svg +[pypi-url]: https://pypi.python.org/pypi/versioneer/ +[travis-image]: +https://img.shields.io/travis/com/python-versioneer/python-versioneer.svg +[travis-url]: https://travis-ci.com/github/python-versioneer/python-versioneer + +""" +# pylint:disable=invalid-name,import-outside-toplevel,missing-function-docstring +# pylint:disable=missing-class-docstring,too-many-branches,too-many-statements +# pylint:disable=raise-missing-from,too-many-lines,too-many-locals,import-error +# pylint:disable=too-few-public-methods,redefined-outer-name,consider-using-with +# pylint:disable=attribute-defined-outside-init,too-many-arguments + +import configparser +import errno +import json +import os +import re +import subprocess +import sys +from typing import Callable, Dict + + +class VersioneerConfig: + """Container for Versioneer configuration parameters.""" + + +def get_root(): + """Get the project root directory. + + We require that all commands are run from the project root, i.e. the + directory that contains setup.py, setup.cfg, and versioneer.py . + """ + root = os.path.realpath(os.path.abspath(os.getcwd())) + setup_py = os.path.join(root, "setup.py") + versioneer_py = os.path.join(root, "versioneer.py") + if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): + # allow 'python path/to/setup.py COMMAND' + root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) + setup_py = os.path.join(root, "setup.py") + versioneer_py = os.path.join(root, "versioneer.py") + if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): + err = ( + "Versioneer was unable to run the project root directory. " + "Versioneer requires setup.py to be executed from " + "its immediate directory (like 'python setup.py COMMAND'), " + "or in a way that lets it use sys.argv[0] to find the root " + "(like 'python path/to/setup.py COMMAND')." + ) + raise VersioneerBadRootError(err) + try: + # Certain runtime workflows (setup.py install/develop in a setuptools + # tree) execute all dependencies in a single python process, so + # "versioneer" may be imported multiple times, and python's shared + # module-import table will cache the first one. So we can't use + # os.path.dirname(__file__), as that will find whichever + # versioneer.py was first imported, even in later projects. + my_path = os.path.realpath(os.path.abspath(__file__)) + me_dir = os.path.normcase(os.path.splitext(my_path)[0]) + vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) + if me_dir != vsr_dir: + print( + "Warning: build in %s is using versioneer.py from %s" + % (os.path.dirname(my_path), versioneer_py) + ) + except NameError: + pass + return root + + +def get_config_from_root(root): + """Read the project setup.cfg file to determine Versioneer config.""" + # This might raise OSError (if setup.cfg is missing), or + # configparser.NoSectionError (if it lacks a [versioneer] section), or + # configparser.NoOptionError (if it lacks "VCS="). See the docstring at + # the top of versioneer.py for instructions on writing your setup.cfg . + setup_cfg = os.path.join(root, "setup.cfg") + parser = configparser.ConfigParser() + with open(setup_cfg, "r") as cfg_file: + parser.read_file(cfg_file) + VCS = parser.get("versioneer", "VCS") # mandatory + + # Dict-like interface for non-mandatory entries + section = parser["versioneer"] + + cfg = VersioneerConfig() + cfg.VCS = VCS + cfg.style = section.get("style", "") + cfg.versionfile_source = section.get("versionfile_source") + cfg.versionfile_build = section.get("versionfile_build") + cfg.tag_prefix = section.get("tag_prefix") + if cfg.tag_prefix in ("''", '""'): + cfg.tag_prefix = "" + cfg.parentdir_prefix = section.get("parentdir_prefix") + cfg.verbose = section.get("verbose") + return cfg + + +class NotThisMethod(Exception): + """Exception raised if a method is not valid for the current scenario.""" + + +# these dictionaries contain VCS-specific tools +LONG_VERSION_PY: Dict[str, str] = {} +HANDLERS: Dict[str, Dict[str, Callable]] = {} + + +def register_vcs_handler(vcs, method): # decorator + """Create decorator to mark a method as the handler of a VCS.""" + + def decorate(f): + """Store f in HANDLERS[vcs][method].""" + HANDLERS.setdefault(vcs, {})[method] = f + return f + + return decorate + + +def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): + """Call the given command(s).""" + assert isinstance(commands, list) + process = None + for command in commands: + try: + dispcmd = str([command] + args) + # remember shell=False, so use git.cmd on windows, not just git + process = subprocess.Popen( + [command] + args, + cwd=cwd, + env=env, + stdout=subprocess.PIPE, + stderr=(subprocess.PIPE if hide_stderr else None), + ) + break + except OSError: + e = sys.exc_info()[1] + if e.errno == errno.ENOENT: + continue + if verbose: + print("unable to run %s" % dispcmd) + print(e) + return None, None + else: + if verbose: + print("unable to find command, tried %s" % (commands,)) + return None, None + stdout = process.communicate()[0].strip().decode() + if process.returncode != 0: + if verbose: + print("unable to run %s (error)" % dispcmd) + print("stdout was %s" % stdout) + return None, process.returncode + return stdout, process.returncode + + +LONG_VERSION_PY[ + "git" +] = r''' +# This file helps to compute a version number in source trees obtained from +# git-archive tarball (such as those provided by githubs download-from-tag +# feature). Distribution tarballs (built by setup.py sdist) and build +# directories (produced by setup.py build) will contain a much shorter file +# that just contains the computed version number. + +# This file is released into the public domain. Generated by +# versioneer-0.21 (https://github.com/python-versioneer/python-versioneer) + +"""Git implementation of _version.py.""" + +import errno +import os +import re +import subprocess +import sys +from typing import Callable, Dict + + +def get_keywords(): + """Get the keywords needed to look up the version information.""" + # these strings will be replaced by git during git-archive. + # setup.py/versioneer.py will grep for the variable names, so they must + # each be defined on a line of their own. _version.py will just call + # get_keywords(). + git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" + git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" + git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" + keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} + return keywords + + +class VersioneerConfig: + """Container for Versioneer configuration parameters.""" + + +def get_config(): + """Create, populate and return the VersioneerConfig() object.""" + # these strings are filled in when 'setup.py versioneer' creates + # _version.py + cfg = VersioneerConfig() + cfg.VCS = "git" + cfg.style = "%(STYLE)s" + cfg.tag_prefix = "%(TAG_PREFIX)s" + cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" + cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" + cfg.verbose = False + return cfg + + +class NotThisMethod(Exception): + """Exception raised if a method is not valid for the current scenario.""" + + +LONG_VERSION_PY: Dict[str, str] = {} +HANDLERS: Dict[str, Dict[str, Callable]] = {} + + +def register_vcs_handler(vcs, method): # decorator + """Create decorator to mark a method as the handler of a VCS.""" + def decorate(f): + """Store f in HANDLERS[vcs][method].""" + if vcs not in HANDLERS: + HANDLERS[vcs] = {} + HANDLERS[vcs][method] = f + return f + return decorate + + +def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, + env=None): + """Call the given command(s).""" + assert isinstance(commands, list) + process = None + for command in commands: + try: + dispcmd = str([command] + args) + # remember shell=False, so use git.cmd on windows, not just git + process = subprocess.Popen([command] + args, cwd=cwd, env=env, + stdout=subprocess.PIPE, + stderr=(subprocess.PIPE if hide_stderr + else None)) + break + except OSError: + e = sys.exc_info()[1] + if e.errno == errno.ENOENT: + continue + if verbose: + print("unable to run %%s" %% dispcmd) + print(e) + return None, None + else: + if verbose: + print("unable to find command, tried %%s" %% (commands,)) + return None, None + stdout = process.communicate()[0].strip().decode() + if process.returncode != 0: + if verbose: + print("unable to run %%s (error)" %% dispcmd) + print("stdout was %%s" %% stdout) + return None, process.returncode + return stdout, process.returncode + + +def versions_from_parentdir(parentdir_prefix, root, verbose): + """Try to determine the version from the parent directory name. + + Source tarballs conventionally unpack into a directory that includes both + the project name and a version string. We will also support searching up + two directory levels for an appropriately named parent directory + """ + rootdirs = [] + + for _ in range(3): + dirname = os.path.basename(root) + if dirname.startswith(parentdir_prefix): + return {"version": dirname[len(parentdir_prefix):], + "full-revisionid": None, + "dirty": False, "error": None, "date": None} + rootdirs.append(root) + root = os.path.dirname(root) # up a level + + if verbose: + print("Tried directories %%s but none started with prefix %%s" %% + (str(rootdirs), parentdir_prefix)) + raise NotThisMethod("rootdir doesn't start with parentdir_prefix") + + +@register_vcs_handler("git", "get_keywords") +def git_get_keywords(versionfile_abs): + """Extract version information from the given file.""" + # the code embedded in _version.py can just fetch the value of these + # keywords. When used from setup.py, we don't want to import _version.py, + # so we do it with a regexp instead. This function is not used from + # _version.py. + keywords = {} + try: + with open(versionfile_abs, "r") as fobj: + for line in fobj: + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) + if line.strip().startswith("git_date ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["date"] = mo.group(1) + except OSError: + pass + return keywords + + +@register_vcs_handler("git", "keywords") +def git_versions_from_keywords(keywords, tag_prefix, verbose): + """Get version information from git keywords.""" + if "refnames" not in keywords: + raise NotThisMethod("Short version file found") + date = keywords.get("date") + if date is not None: + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + + # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant + # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 + # -like" string, which we must then edit to make compliant), because + # it's been around since git-1.5.3, and it's too difficult to + # discover which version we're using, or to work around using an + # older one. + date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) + refnames = keywords["refnames"].strip() + if refnames.startswith("$Format"): + if verbose: + print("keywords are unexpanded, not using") + raise NotThisMethod("unexpanded keywords, not a git-archive tarball") + refs = {r.strip() for r in refnames.strip("()").split(",")} + # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of + # just "foo-1.0". If we see a "tag: " prefix, prefer those. + TAG = "tag: " + tags = {r[len(TAG):] for r in refs if r.startswith(TAG)} + if not tags: + # Either we're using git < 1.8.3, or there really are no tags. We use + # a heuristic: assume all version tags have a digit. The old git %%d + # expansion behaves like git log --decorate=short and strips out the + # refs/heads/ and refs/tags/ prefixes that would let us distinguish + # between branches and tags. By ignoring refnames without digits, we + # filter out many common branch names like "release" and + # "stabilization", as well as "HEAD" and "master". + tags = {r for r in refs if re.search(r'\d', r)} + if verbose: + print("discarding '%%s', no digits" %% ",".join(refs - tags)) + if verbose: + print("likely tags: %%s" %% ",".join(sorted(tags))) + for ref in sorted(tags): + # sorting will prefer e.g. "2.0" over "2.0rc1" + if ref.startswith(tag_prefix): + r = ref[len(tag_prefix):] + # Filter out refs that exactly match prefix or that don't start + # with a number once the prefix is stripped (mostly a concern + # when prefix is '') + if not re.match(r'\d', r): + continue + if verbose: + print("picking %%s" %% r) + return {"version": r, + "full-revisionid": keywords["full"].strip(), + "dirty": False, "error": None, + "date": date} + # no suitable tags, so version is "0+unknown", but full hex is still there + if verbose: + print("no suitable tags, using unknown + full revision id") + return {"version": "0+unknown", + "full-revisionid": keywords["full"].strip(), + "dirty": False, "error": "no suitable tags", "date": None} + + +@register_vcs_handler("git", "pieces_from_vcs") +def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): + """Get version from 'git describe' in the root of the source tree. + + This only gets called if the git-archive 'subst' keywords were *not* + expanded, and _version.py hasn't already been rewritten with a short + version string, meaning we're inside a checked out source tree. + """ + GITS = ["git"] + TAG_PREFIX_REGEX = "*" + if sys.platform == "win32": + GITS = ["git.cmd", "git.exe"] + TAG_PREFIX_REGEX = r"\*" + + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, + hide_stderr=True) + if rc != 0: + if verbose: + print("Directory %%s not under git control" %% root) + raise NotThisMethod("'git rev-parse --git-dir' returned error") + + # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] + # if there isn't one, this yields HEX[-dirty] (no NUM) + describe_out, rc = runner(GITS, ["describe", "--tags", "--dirty", + "--always", "--long", + "--match", + "%%s%%s" %% (tag_prefix, TAG_PREFIX_REGEX)], + cwd=root) + # --long was added in git-1.5.5 + if describe_out is None: + raise NotThisMethod("'git describe' failed") + describe_out = describe_out.strip() + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) + if full_out is None: + raise NotThisMethod("'git rev-parse' failed") + full_out = full_out.strip() + + pieces = {} + pieces["long"] = full_out + pieces["short"] = full_out[:7] # maybe improved later + pieces["error"] = None + + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], + cwd=root) + # --abbrev-ref was added in git-1.6.3 + if rc != 0 or branch_name is None: + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") + branch_name = branch_name.strip() + + if branch_name == "HEAD": + # If we aren't exactly on a branch, pick a branch which represents + # the current commit. If all else fails, we are on a branchless + # commit. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) + # --contains was added in git-1.5.4 + if rc != 0 or branches is None: + raise NotThisMethod("'git branch --contains' returned error") + branches = branches.split("\n") + + # Remove the first line if we're running detached + if "(" in branches[0]: + branches.pop(0) + + # Strip off the leading "* " from the list of branches. + branches = [branch[2:] for branch in branches] + if "master" in branches: + branch_name = "master" + elif not branches: + branch_name = None + else: + # Pick the first branch that is returned. Good or bad. + branch_name = branches[0] + + pieces["branch"] = branch_name + + # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] + # TAG might have hyphens. + git_describe = describe_out + + # look for -dirty suffix + dirty = git_describe.endswith("-dirty") + pieces["dirty"] = dirty + if dirty: + git_describe = git_describe[:git_describe.rindex("-dirty")] + + # now we have TAG-NUM-gHEX or HEX + + if "-" in git_describe: + # TAG-NUM-gHEX + mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) + if not mo: + # unparsable. Maybe git-describe is misbehaving? + pieces["error"] = ("unable to parse git-describe output: '%%s'" + %% describe_out) + return pieces + + # tag + full_tag = mo.group(1) + if not full_tag.startswith(tag_prefix): + if verbose: + fmt = "tag '%%s' doesn't start with prefix '%%s'" + print(fmt %% (full_tag, tag_prefix)) + pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" + %% (full_tag, tag_prefix)) + return pieces + pieces["closest-tag"] = full_tag[len(tag_prefix):] + + # distance: number of commits since tag + pieces["distance"] = int(mo.group(2)) + + # commit: short hex revision ID + pieces["short"] = mo.group(3) + + else: + # HEX: no tags + pieces["closest-tag"] = None + count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) + pieces["distance"] = int(count_out) # total number of commits + + # commit date: see ISO-8601 comment in git_versions_from_keywords() + date = runner(GITS, ["show", "-s", "--format=%%ci", "HEAD"], cwd=root)[0].strip() + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) + + return pieces + + +def plus_or_dot(pieces): + """Return a + if we don't already have one, else return a .""" + if "+" in pieces.get("closest-tag", ""): + return "." + return "+" + + +def render_pep440(pieces): + """Build up version string, with post-release "local version identifier". + + Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you + get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty + + Exceptions: + 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += plus_or_dot(pieces) + rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], + pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def render_pep440_branch(pieces): + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . + + The ".dev0" means not master branch. Note that .dev0 sorts backwards + (a feature branch will appear "older" than the master branch). + + Exceptions: + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0" + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+untagged.%%d.g%%s" %% (pieces["distance"], + pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def pep440_split_post(ver): + """Split pep440 version string at the post-release segment. + + Returns the release segments before the post-release and the + post-release version number (or -1 if no post-release segment is present). + """ + vc = str.split(ver, ".post") + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None + + +def render_pep440_pre(pieces): + """TAG[.postN.devDISTANCE] -- No -dirty. + + Exceptions: + 1: no tags. 0.post0.devDISTANCE + """ + if pieces["closest-tag"]: + if pieces["distance"]: + # update the post release segment + tag_version, post_version = pep440_split_post(pieces["closest-tag"]) + rendered = tag_version + if post_version is not None: + rendered += ".post%%d.dev%%d" %% (post_version+1, pieces["distance"]) + else: + rendered += ".post0.dev%%d" %% (pieces["distance"]) + else: + # no commits, use the tag as the version + rendered = pieces["closest-tag"] + else: + # exception #1 + rendered = "0.post0.dev%%d" %% pieces["distance"] + return rendered + + +def render_pep440_post(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX] . + + The ".dev0" means dirty. Note that .dev0 sorts backwards + (a dirty tree will appear "older" than the corresponding clean one), + but you shouldn't be releasing software with -dirty anyways. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%%d" %% pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%%s" %% pieces["short"] + else: + # exception #1 + rendered = "0.post%%d" %% pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + rendered += "+g%%s" %% pieces["short"] + return rendered + + +def render_pep440_post_branch(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . + + The ".dev0" means not master branch. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%%d" %% pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%%s" %% pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0.post%%d" %% pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+g%%s" %% pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def render_pep440_old(pieces): + """TAG[.postDISTANCE[.dev0]] . + + The ".dev0" means dirty. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%%d" %% pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + else: + # exception #1 + rendered = "0.post%%d" %% pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + return rendered + + +def render_git_describe(pieces): + """TAG[-DISTANCE-gHEX][-dirty]. + + Like 'git describe --tags --dirty --always'. + + Exceptions: + 1: no tags. HEX[-dirty] (note: no 'g' prefix) + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"]: + rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) + else: + # exception #1 + rendered = pieces["short"] + if pieces["dirty"]: + rendered += "-dirty" + return rendered + + +def render_git_describe_long(pieces): + """TAG-DISTANCE-gHEX[-dirty]. + + Like 'git describe --tags --dirty --always -long'. + The distance/hash is unconditional. + + Exceptions: + 1: no tags. HEX[-dirty] (note: no 'g' prefix) + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) + else: + # exception #1 + rendered = pieces["short"] + if pieces["dirty"]: + rendered += "-dirty" + return rendered + + +def render(pieces, style): + """Render the given version pieces into the requested style.""" + if pieces["error"]: + return {"version": "unknown", + "full-revisionid": pieces.get("long"), + "dirty": None, + "error": pieces["error"], + "date": None} + + if not style or style == "default": + style = "pep440" # the default + + if style == "pep440": + rendered = render_pep440(pieces) + elif style == "pep440-branch": + rendered = render_pep440_branch(pieces) + elif style == "pep440-pre": + rendered = render_pep440_pre(pieces) + elif style == "pep440-post": + rendered = render_pep440_post(pieces) + elif style == "pep440-post-branch": + rendered = render_pep440_post_branch(pieces) + elif style == "pep440-old": + rendered = render_pep440_old(pieces) + elif style == "git-describe": + rendered = render_git_describe(pieces) + elif style == "git-describe-long": + rendered = render_git_describe_long(pieces) + else: + raise ValueError("unknown style '%%s'" %% style) + + return {"version": rendered, "full-revisionid": pieces["long"], + "dirty": pieces["dirty"], "error": None, + "date": pieces.get("date")} + + +def get_versions(): + """Get version information or return default if unable to do so.""" + # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have + # __file__, we can work backwards from there to the root. Some + # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which + # case we can only use expanded keywords. + + cfg = get_config() + verbose = cfg.verbose + + try: + return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, + verbose) + except NotThisMethod: + pass + + try: + root = os.path.realpath(__file__) + # versionfile_source is the relative path from the top of the source + # tree (where the .git directory might live) to this file. Invert + # this to find the root from __file__. + for _ in cfg.versionfile_source.split('/'): + root = os.path.dirname(root) + except NameError: + return {"version": "0+unknown", "full-revisionid": None, + "dirty": None, + "error": "unable to find root of source tree", + "date": None} + + try: + pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) + return render(pieces, cfg.style) + except NotThisMethod: + pass + + try: + if cfg.parentdir_prefix: + return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) + except NotThisMethod: + pass + + return {"version": "0+unknown", "full-revisionid": None, + "dirty": None, + "error": "unable to compute version", "date": None} +''' + + +@register_vcs_handler("git", "get_keywords") +def git_get_keywords(versionfile_abs): + """Extract version information from the given file.""" + # the code embedded in _version.py can just fetch the value of these + # keywords. When used from setup.py, we don't want to import _version.py, + # so we do it with a regexp instead. This function is not used from + # _version.py. + keywords = {} + try: + with open(versionfile_abs, "r") as fobj: + for line in fobj: + if line.strip().startswith("git_refnames ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["refnames"] = mo.group(1) + if line.strip().startswith("git_full ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["full"] = mo.group(1) + if line.strip().startswith("git_date ="): + mo = re.search(r'=\s*"(.*)"', line) + if mo: + keywords["date"] = mo.group(1) + except OSError: + pass + return keywords + + +@register_vcs_handler("git", "keywords") +def git_versions_from_keywords(keywords, tag_prefix, verbose): + """Get version information from git keywords.""" + if "refnames" not in keywords: + raise NotThisMethod("Short version file found") + date = keywords.get("date") + if date is not None: + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + + # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant + # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 + # -like" string, which we must then edit to make compliant), because + # it's been around since git-1.5.3, and it's too difficult to + # discover which version we're using, or to work around using an + # older one. + date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) + refnames = keywords["refnames"].strip() + if refnames.startswith("$Format"): + if verbose: + print("keywords are unexpanded, not using") + raise NotThisMethod("unexpanded keywords, not a git-archive tarball") + refs = {r.strip() for r in refnames.strip("()").split(",")} + # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of + # just "foo-1.0". If we see a "tag: " prefix, prefer those. + TAG = "tag: " + tags = {r[len(TAG) :] for r in refs if r.startswith(TAG)} + if not tags: + # Either we're using git < 1.8.3, or there really are no tags. We use + # a heuristic: assume all version tags have a digit. The old git %d + # expansion behaves like git log --decorate=short and strips out the + # refs/heads/ and refs/tags/ prefixes that would let us distinguish + # between branches and tags. By ignoring refnames without digits, we + # filter out many common branch names like "release" and + # "stabilization", as well as "HEAD" and "master". + tags = {r for r in refs if re.search(r"\d", r)} + if verbose: + print("discarding '%s', no digits" % ",".join(refs - tags)) + if verbose: + print("likely tags: %s" % ",".join(sorted(tags))) + for ref in sorted(tags): + # sorting will prefer e.g. "2.0" over "2.0rc1" + if ref.startswith(tag_prefix): + r = ref[len(tag_prefix) :] + # Filter out refs that exactly match prefix or that don't start + # with a number once the prefix is stripped (mostly a concern + # when prefix is '') + if not re.match(r"\d", r): + continue + if verbose: + print("picking %s" % r) + return { + "version": r, + "full-revisionid": keywords["full"].strip(), + "dirty": False, + "error": None, + "date": date, + } + # no suitable tags, so version is "0+unknown", but full hex is still there + if verbose: + print("no suitable tags, using unknown + full revision id") + return { + "version": "0+unknown", + "full-revisionid": keywords["full"].strip(), + "dirty": False, + "error": "no suitable tags", + "date": None, + } + + +@register_vcs_handler("git", "pieces_from_vcs") +def git_pieces_from_vcs(tag_prefix, root, verbose, runner=run_command): + """Get version from 'git describe' in the root of the source tree. + + This only gets called if the git-archive 'subst' keywords were *not* + expanded, and _version.py hasn't already been rewritten with a short + version string, meaning we're inside a checked out source tree. + """ + GITS = ["git"] + TAG_PREFIX_REGEX = "*" + if sys.platform == "win32": + GITS = ["git.cmd", "git.exe"] + TAG_PREFIX_REGEX = r"\*" + + _, rc = runner(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) + if rc != 0: + if verbose: + print("Directory %s not under git control" % root) + raise NotThisMethod("'git rev-parse --git-dir' returned error") + + # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] + # if there isn't one, this yields HEX[-dirty] (no NUM) + describe_out, rc = runner( + GITS, + [ + "describe", + "--tags", + "--dirty", + "--always", + "--long", + "--match", + "%s%s" % (tag_prefix, TAG_PREFIX_REGEX), + ], + cwd=root, + ) + # --long was added in git-1.5.5 + if describe_out is None: + raise NotThisMethod("'git describe' failed") + describe_out = describe_out.strip() + full_out, rc = runner(GITS, ["rev-parse", "HEAD"], cwd=root) + if full_out is None: + raise NotThisMethod("'git rev-parse' failed") + full_out = full_out.strip() + + pieces = {} + pieces["long"] = full_out + pieces["short"] = full_out[:7] # maybe improved later + pieces["error"] = None + + branch_name, rc = runner(GITS, ["rev-parse", "--abbrev-ref", "HEAD"], cwd=root) + # --abbrev-ref was added in git-1.6.3 + if rc != 0 or branch_name is None: + raise NotThisMethod("'git rev-parse --abbrev-ref' returned error") + branch_name = branch_name.strip() + + if branch_name == "HEAD": + # If we aren't exactly on a branch, pick a branch which represents + # the current commit. If all else fails, we are on a branchless + # commit. + branches, rc = runner(GITS, ["branch", "--contains"], cwd=root) + # --contains was added in git-1.5.4 + if rc != 0 or branches is None: + raise NotThisMethod("'git branch --contains' returned error") + branches = branches.split("\n") + + # Remove the first line if we're running detached + if "(" in branches[0]: + branches.pop(0) + + # Strip off the leading "* " from the list of branches. + branches = [branch[2:] for branch in branches] + if "master" in branches: + branch_name = "master" + elif not branches: + branch_name = None + else: + # Pick the first branch that is returned. Good or bad. + branch_name = branches[0] + + pieces["branch"] = branch_name + + # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] + # TAG might have hyphens. + git_describe = describe_out + + # look for -dirty suffix + dirty = git_describe.endswith("-dirty") + pieces["dirty"] = dirty + if dirty: + git_describe = git_describe[: git_describe.rindex("-dirty")] + + # now we have TAG-NUM-gHEX or HEX + + if "-" in git_describe: + # TAG-NUM-gHEX + mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe) + if not mo: + # unparsable. Maybe git-describe is misbehaving? + pieces["error"] = "unable to parse git-describe output: '%s'" % describe_out + return pieces + + # tag + full_tag = mo.group(1) + if not full_tag.startswith(tag_prefix): + if verbose: + fmt = "tag '%s' doesn't start with prefix '%s'" + print(fmt % (full_tag, tag_prefix)) + pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % ( + full_tag, + tag_prefix, + ) + return pieces + pieces["closest-tag"] = full_tag[len(tag_prefix) :] + + # distance: number of commits since tag + pieces["distance"] = int(mo.group(2)) + + # commit: short hex revision ID + pieces["short"] = mo.group(3) + + else: + # HEX: no tags + pieces["closest-tag"] = None + count_out, rc = runner(GITS, ["rev-list", "HEAD", "--count"], cwd=root) + pieces["distance"] = int(count_out) # total number of commits + + # commit date: see ISO-8601 comment in git_versions_from_keywords() + date = runner(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[0].strip() + # Use only the last line. Previous lines may contain GPG signature + # information. + date = date.splitlines()[-1] + pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) + + return pieces + + +def do_vcs_install(manifest_in, versionfile_source, ipy): + """Git-specific installation logic for Versioneer. + + For Git, this means creating/changing .gitattributes to mark _version.py + for export-subst keyword substitution. + """ + GITS = ["git"] + if sys.platform == "win32": + GITS = ["git.cmd", "git.exe"] + files = [manifest_in, versionfile_source] + if ipy: + files.append(ipy) + try: + my_path = __file__ + if my_path.endswith(".pyc") or my_path.endswith(".pyo"): + my_path = os.path.splitext(my_path)[0] + ".py" + versioneer_file = os.path.relpath(my_path) + except NameError: + versioneer_file = "versioneer.py" + files.append(versioneer_file) + present = False + try: + with open(".gitattributes", "r") as fobj: + for line in fobj: + if line.strip().startswith(versionfile_source): + if "export-subst" in line.strip().split()[1:]: + present = True + break + except OSError: + pass + if not present: + with open(".gitattributes", "a+") as fobj: + fobj.write(f"{versionfile_source} export-subst\n") + files.append(".gitattributes") + run_command(GITS, ["add", "--"] + files) + + +def versions_from_parentdir(parentdir_prefix, root, verbose): + """Try to determine the version from the parent directory name. + + Source tarballs conventionally unpack into a directory that includes both + the project name and a version string. We will also support searching up + two directory levels for an appropriately named parent directory + """ + rootdirs = [] + + for _ in range(3): + dirname = os.path.basename(root) + if dirname.startswith(parentdir_prefix): + return { + "version": dirname[len(parentdir_prefix) :], + "full-revisionid": None, + "dirty": False, + "error": None, + "date": None, + } + rootdirs.append(root) + root = os.path.dirname(root) # up a level + + if verbose: + print( + "Tried directories %s but none started with prefix %s" + % (str(rootdirs), parentdir_prefix) + ) + raise NotThisMethod("rootdir doesn't start with parentdir_prefix") + + +SHORT_VERSION_PY = """ +# This file was generated by 'versioneer.py' (0.21) from +# revision-control system data, or from the parent directory name of an +# unpacked source archive. Distribution tarballs contain a pre-generated copy +# of this file. + +import json + +version_json = ''' +%s +''' # END VERSION_JSON + + +def get_versions(): + return json.loads(version_json) +""" + + +def versions_from_file(filename): + """Try to determine the version from _version.py if present.""" + try: + with open(filename) as f: + contents = f.read() + except OSError: + raise NotThisMethod("unable to read _version.py") + mo = re.search( + r"version_json = '''\n(.*)''' # END VERSION_JSON", contents, re.M | re.S + ) + if not mo: + mo = re.search( + r"version_json = '''\r\n(.*)''' # END VERSION_JSON", contents, re.M | re.S + ) + if not mo: + raise NotThisMethod("no version_json in _version.py") + return json.loads(mo.group(1)) + + +def write_to_version_file(filename, versions): + """Write the given version number to the given _version.py file.""" + os.unlink(filename) + contents = json.dumps(versions, sort_keys=True, indent=1, separators=(",", ": ")) + with open(filename, "w") as f: + f.write(SHORT_VERSION_PY % contents) + + print("set %s to '%s'" % (filename, versions["version"])) + + +def plus_or_dot(pieces): + """Return a + if we don't already have one, else return a .""" + if "+" in pieces.get("closest-tag", ""): + return "." + return "+" + + +def render_pep440(pieces): + """Build up version string, with post-release "local version identifier". + + Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you + get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty + + Exceptions: + 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += plus_or_dot(pieces) + rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def render_pep440_branch(pieces): + """TAG[[.dev0]+DISTANCE.gHEX[.dirty]] . + + The ".dev0" means not master branch. Note that .dev0 sorts backwards + (a feature branch will appear "older" than the master branch). + + Exceptions: + 1: no tags. 0[.dev0]+untagged.DISTANCE.gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0" + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def pep440_split_post(ver): + """Split pep440 version string at the post-release segment. + + Returns the release segments before the post-release and the + post-release version number (or -1 if no post-release segment is present). + """ + vc = str.split(ver, ".post") + return vc[0], int(vc[1] or 0) if len(vc) == 2 else None + + +def render_pep440_pre(pieces): + """TAG[.postN.devDISTANCE] -- No -dirty. + + Exceptions: + 1: no tags. 0.post0.devDISTANCE + """ + if pieces["closest-tag"]: + if pieces["distance"]: + # update the post release segment + tag_version, post_version = pep440_split_post(pieces["closest-tag"]) + rendered = tag_version + if post_version is not None: + rendered += ".post%d.dev%d" % (post_version + 1, pieces["distance"]) + else: + rendered += ".post0.dev%d" % (pieces["distance"]) + else: + # no commits, use the tag as the version + rendered = pieces["closest-tag"] + else: + # exception #1 + rendered = "0.post0.dev%d" % pieces["distance"] + return rendered + + +def render_pep440_post(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX] . + + The ".dev0" means dirty. Note that .dev0 sorts backwards + (a dirty tree will appear "older" than the corresponding clean one), + but you shouldn't be releasing software with -dirty anyways. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%s" % pieces["short"] + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + rendered += "+g%s" % pieces["short"] + return rendered + + +def render_pep440_post_branch(pieces): + """TAG[.postDISTANCE[.dev0]+gHEX[.dirty]] . + + The ".dev0" means not master branch. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0]+gHEX[.dirty] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += plus_or_dot(pieces) + rendered += "g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["branch"] != "master": + rendered += ".dev0" + rendered += "+g%s" % pieces["short"] + if pieces["dirty"]: + rendered += ".dirty" + return rendered + + +def render_pep440_old(pieces): + """TAG[.postDISTANCE[.dev0]] . + + The ".dev0" means dirty. + + Exceptions: + 1: no tags. 0.postDISTANCE[.dev0] + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"] or pieces["dirty"]: + rendered += ".post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + else: + # exception #1 + rendered = "0.post%d" % pieces["distance"] + if pieces["dirty"]: + rendered += ".dev0" + return rendered + + +def render_git_describe(pieces): + """TAG[-DISTANCE-gHEX][-dirty]. + + Like 'git describe --tags --dirty --always'. + + Exceptions: + 1: no tags. HEX[-dirty] (note: no 'g' prefix) + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + if pieces["distance"]: + rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) + else: + # exception #1 + rendered = pieces["short"] + if pieces["dirty"]: + rendered += "-dirty" + return rendered + + +def render_git_describe_long(pieces): + """TAG-DISTANCE-gHEX[-dirty]. + + Like 'git describe --tags --dirty --always -long'. + The distance/hash is unconditional. + + Exceptions: + 1: no tags. HEX[-dirty] (note: no 'g' prefix) + """ + if pieces["closest-tag"]: + rendered = pieces["closest-tag"] + rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) + else: + # exception #1 + rendered = pieces["short"] + if pieces["dirty"]: + rendered += "-dirty" + return rendered + + +def render(pieces, style): + """Render the given version pieces into the requested style.""" + if pieces["error"]: + return { + "version": "unknown", + "full-revisionid": pieces.get("long"), + "dirty": None, + "error": pieces["error"], + "date": None, + } + + if not style or style == "default": + style = "pep440" # the default + + if style == "pep440": + rendered = render_pep440(pieces) + elif style == "pep440-branch": + rendered = render_pep440_branch(pieces) + elif style == "pep440-pre": + rendered = render_pep440_pre(pieces) + elif style == "pep440-post": + rendered = render_pep440_post(pieces) + elif style == "pep440-post-branch": + rendered = render_pep440_post_branch(pieces) + elif style == "pep440-old": + rendered = render_pep440_old(pieces) + elif style == "git-describe": + rendered = render_git_describe(pieces) + elif style == "git-describe-long": + rendered = render_git_describe_long(pieces) + else: + raise ValueError("unknown style '%s'" % style) + + return { + "version": rendered, + "full-revisionid": pieces["long"], + "dirty": pieces["dirty"], + "error": None, + "date": pieces.get("date"), + } + + +class VersioneerBadRootError(Exception): + """The project root directory is unknown or missing key files.""" + + +def get_versions(verbose=False): + """Get the project version from whatever source is available. + + Returns dict with two keys: 'version' and 'full'. + """ + if "versioneer" in sys.modules: + # see the discussion in cmdclass.py:get_cmdclass() + del sys.modules["versioneer"] + + root = get_root() + cfg = get_config_from_root(root) + + assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" + handlers = HANDLERS.get(cfg.VCS) + assert handlers, "unrecognized VCS '%s'" % cfg.VCS + verbose = verbose or cfg.verbose + assert ( + cfg.versionfile_source is not None + ), "please set versioneer.versionfile_source" + assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" + + versionfile_abs = os.path.join(root, cfg.versionfile_source) + + # extract version from first of: _version.py, VCS command (e.g. 'git + # describe'), parentdir. This is meant to work for developers using a + # source checkout, for users of a tarball created by 'setup.py sdist', + # and for users of a tarball/zipball created by 'git archive' or github's + # download-from-tag feature or the equivalent in other VCSes. + + get_keywords_f = handlers.get("get_keywords") + from_keywords_f = handlers.get("keywords") + if get_keywords_f and from_keywords_f: + try: + keywords = get_keywords_f(versionfile_abs) + ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) + if verbose: + print("got version from expanded keyword %s" % ver) + return ver + except NotThisMethod: + pass + + try: + ver = versions_from_file(versionfile_abs) + if verbose: + print("got version from file %s %s" % (versionfile_abs, ver)) + return ver + except NotThisMethod: + pass + + from_vcs_f = handlers.get("pieces_from_vcs") + if from_vcs_f: + try: + pieces = from_vcs_f(cfg.tag_prefix, root, verbose) + ver = render(pieces, cfg.style) + if verbose: + print("got version from VCS %s" % ver) + return ver + except NotThisMethod: + pass + + try: + if cfg.parentdir_prefix: + ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) + if verbose: + print("got version from parentdir %s" % ver) + return ver + except NotThisMethod: + pass + + if verbose: + print("unable to compute version") + + return { + "version": "0+unknown", + "full-revisionid": None, + "dirty": None, + "error": "unable to compute version", + "date": None, + } + + +def get_version(): + """Get the short version string for this project.""" + return get_versions()["version"] + + +def get_cmdclass(cmdclass=None): + """Get the custom setuptools/distutils subclasses used by Versioneer. + + If the package uses a different cmdclass (e.g. one from numpy), it + should be provide as an argument. + """ + if "versioneer" in sys.modules: + del sys.modules["versioneer"] + # this fixes the "python setup.py develop" case (also 'install' and + # 'easy_install .'), in which subdependencies of the main project are + # built (using setup.py bdist_egg) in the same python process. Assume + # a main project A and a dependency B, which use different versions + # of Versioneer. A's setup.py imports A's Versioneer, leaving it in + # sys.modules by the time B's setup.py is executed, causing B to run + # with the wrong versioneer. Setuptools wraps the sub-dep builds in a + # sandbox that restores sys.modules to it's pre-build state, so the + # parent is protected against the child's "import versioneer". By + # removing ourselves from sys.modules here, before the child build + # happens, we protect the child from the parent's versioneer too. + # Also see https://github.com/python-versioneer/python-versioneer/issues/52 + + cmds = {} if cmdclass is None else cmdclass.copy() + + # we add "version" to both distutils and setuptools + from distutils.core import Command + + class cmd_version(Command): + description = "report generated version string" + user_options = [] + boolean_options = [] + + def initialize_options(self): + pass + + def finalize_options(self): + pass + + def run(self): + vers = get_versions(verbose=True) + print("Version: %s" % vers["version"]) + print(" full-revisionid: %s" % vers.get("full-revisionid")) + print(" dirty: %s" % vers.get("dirty")) + print(" date: %s" % vers.get("date")) + if vers["error"]: + print(" error: %s" % vers["error"]) + + cmds["version"] = cmd_version + + # we override "build_py" in both distutils and setuptools + # + # most invocation pathways end up running build_py: + # distutils/build -> build_py + # distutils/install -> distutils/build ->.. + # setuptools/bdist_wheel -> distutils/install ->.. + # setuptools/bdist_egg -> distutils/install_lib -> build_py + # setuptools/install -> bdist_egg ->.. + # setuptools/develop -> ? + # pip install: + # copies source tree to a tempdir before running egg_info/etc + # if .git isn't copied too, 'git describe' will fail + # then does setup.py bdist_wheel, or sometimes setup.py install + # setup.py egg_info -> ? + + # we override different "build_py" commands for both environments + if "build_py" in cmds: + _build_py = cmds["build_py"] + elif "setuptools" in sys.modules: + from setuptools.command.build_py import build_py as _build_py + else: + from distutils.command.build_py import build_py as _build_py + + class cmd_build_py(_build_py): + def run(self): + root = get_root() + cfg = get_config_from_root(root) + versions = get_versions() + _build_py.run(self) + # now locate _version.py in the new build/ directory and replace + # it with an updated value + if cfg.versionfile_build: + target_versionfile = os.path.join(self.build_lib, cfg.versionfile_build) + print("UPDATING %s" % target_versionfile) + write_to_version_file(target_versionfile, versions) + + cmds["build_py"] = cmd_build_py + + if "build_ext" in cmds: + _build_ext = cmds["build_ext"] + elif "setuptools" in sys.modules: + from setuptools.command.build_ext import build_ext as _build_ext + else: + from distutils.command.build_ext import build_ext as _build_ext + + class cmd_build_ext(_build_ext): + def run(self): + root = get_root() + cfg = get_config_from_root(root) + versions = get_versions() + _build_ext.run(self) + if self.inplace: + # build_ext --inplace will only build extensions in + # build/lib<..> dir with no _version.py to write to. + # As in place builds will already have a _version.py + # in the module dir, we do not need to write one. + return + # now locate _version.py in the new build/ directory and replace + # it with an updated value + target_versionfile = os.path.join(self.build_lib, cfg.versionfile_build) + print("UPDATING %s" % target_versionfile) + write_to_version_file(target_versionfile, versions) + + cmds["build_ext"] = cmd_build_ext + + if "cx_Freeze" in sys.modules: # cx_freeze enabled? + from cx_Freeze.dist import build_exe as _build_exe + + # nczeczulin reports that py2exe won't like the pep440-style string + # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. + # setup(console=[{ + # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION + # "product_version": versioneer.get_version(), + # ... + + class cmd_build_exe(_build_exe): + def run(self): + root = get_root() + cfg = get_config_from_root(root) + versions = get_versions() + target_versionfile = cfg.versionfile_source + print("UPDATING %s" % target_versionfile) + write_to_version_file(target_versionfile, versions) + + _build_exe.run(self) + os.unlink(target_versionfile) + with open(cfg.versionfile_source, "w") as f: + LONG = LONG_VERSION_PY[cfg.VCS] + f.write( + LONG + % { + "DOLLAR": "$", + "STYLE": cfg.style, + "TAG_PREFIX": cfg.tag_prefix, + "PARENTDIR_PREFIX": cfg.parentdir_prefix, + "VERSIONFILE_SOURCE": cfg.versionfile_source, + } + ) + + cmds["build_exe"] = cmd_build_exe + del cmds["build_py"] + + if "py2exe" in sys.modules: # py2exe enabled? + from py2exe.distutils_buildexe import py2exe as _py2exe + + class cmd_py2exe(_py2exe): + def run(self): + root = get_root() + cfg = get_config_from_root(root) + versions = get_versions() + target_versionfile = cfg.versionfile_source + print("UPDATING %s" % target_versionfile) + write_to_version_file(target_versionfile, versions) + + _py2exe.run(self) + os.unlink(target_versionfile) + with open(cfg.versionfile_source, "w") as f: + LONG = LONG_VERSION_PY[cfg.VCS] + f.write( + LONG + % { + "DOLLAR": "$", + "STYLE": cfg.style, + "TAG_PREFIX": cfg.tag_prefix, + "PARENTDIR_PREFIX": cfg.parentdir_prefix, + "VERSIONFILE_SOURCE": cfg.versionfile_source, + } + ) + + cmds["py2exe"] = cmd_py2exe + + # we override different "sdist" commands for both environments + if "sdist" in cmds: + _sdist = cmds["sdist"] + elif "setuptools" in sys.modules: + from setuptools.command.sdist import sdist as _sdist + else: + from distutils.command.sdist import sdist as _sdist + + class cmd_sdist(_sdist): + def run(self): + versions = get_versions() + self._versioneer_generated_versions = versions + # unless we update this, the command will keep using the old + # version + self.distribution.metadata.version = versions["version"] + return _sdist.run(self) + + def make_release_tree(self, base_dir, files): + root = get_root() + cfg = get_config_from_root(root) + _sdist.make_release_tree(self, base_dir, files) + # now locate _version.py in the new base_dir directory + # (remembering that it may be a hardlink) and replace it with an + # updated value + target_versionfile = os.path.join(base_dir, cfg.versionfile_source) + print("UPDATING %s" % target_versionfile) + write_to_version_file( + target_versionfile, self._versioneer_generated_versions + ) + + cmds["sdist"] = cmd_sdist + + return cmds + + +CONFIG_ERROR = """ +setup.cfg is missing the necessary Versioneer configuration. You need +a section like: + + [versioneer] + VCS = git + style = pep440 + versionfile_source = src/myproject/_version.py + versionfile_build = myproject/_version.py + tag_prefix = + parentdir_prefix = myproject- + +You will also need to edit your setup.py to use the results: + + import versioneer + setup(version=versioneer.get_version(), + cmdclass=versioneer.get_cmdclass(), ...) + +Please read the docstring in ./versioneer.py for configuration instructions, +edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. +""" + +SAMPLE_CONFIG = """ +# See the docstring in versioneer.py for instructions. Note that you must +# re-run 'versioneer.py setup' after changing this section, and commit the +# resulting files. + +[versioneer] +#VCS = git +#style = pep440 +#versionfile_source = +#versionfile_build = +#tag_prefix = +#parentdir_prefix = + +""" + +OLD_SNIPPET = """ +from ._version import get_versions +__version__ = get_versions()['version'] +del get_versions +""" + +INIT_PY_SNIPPET = """ +from . import {0} +__version__ = {0}.get_versions()['version'] +""" + + +def do_setup(): + """Do main VCS-independent setup function for installing Versioneer.""" + root = get_root() + try: + cfg = get_config_from_root(root) + except (OSError, configparser.NoSectionError, configparser.NoOptionError) as e: + if isinstance(e, (OSError, configparser.NoSectionError)): + print("Adding sample versioneer config to setup.cfg", file=sys.stderr) + with open(os.path.join(root, "setup.cfg"), "a") as f: + f.write(SAMPLE_CONFIG) + print(CONFIG_ERROR, file=sys.stderr) + return 1 + + print(" creating %s" % cfg.versionfile_source) + with open(cfg.versionfile_source, "w") as f: + LONG = LONG_VERSION_PY[cfg.VCS] + f.write( + LONG + % { + "DOLLAR": "$", + "STYLE": cfg.style, + "TAG_PREFIX": cfg.tag_prefix, + "PARENTDIR_PREFIX": cfg.parentdir_prefix, + "VERSIONFILE_SOURCE": cfg.versionfile_source, + } + ) + + ipy = os.path.join(os.path.dirname(cfg.versionfile_source), "__init__.py") + if os.path.exists(ipy): + try: + with open(ipy, "r") as f: + old = f.read() + except OSError: + old = "" + module = os.path.splitext(os.path.basename(cfg.versionfile_source))[0] + snippet = INIT_PY_SNIPPET.format(module) + if OLD_SNIPPET in old: + print(" replacing boilerplate in %s" % ipy) + with open(ipy, "w") as f: + f.write(old.replace(OLD_SNIPPET, snippet)) + elif snippet not in old: + print(" appending to %s" % ipy) + with open(ipy, "a") as f: + f.write(snippet) + else: + print(" %s unmodified" % ipy) + else: + print(" %s doesn't exist, ok" % ipy) + ipy = None + + # Make sure both the top-level "versioneer.py" and versionfile_source + # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so + # they'll be copied into source distributions. Pip won't be able to + # install the package without this. + manifest_in = os.path.join(root, "MANIFEST.in") + simple_includes = set() + try: + with open(manifest_in, "r") as f: + for line in f: + if line.startswith("include "): + for include in line.split()[1:]: + simple_includes.add(include) + except OSError: + pass + # That doesn't cover everything MANIFEST.in can do + # (http://docs.python.org/2/distutils/sourcedist.html#commands), so + # it might give some false negatives. Appending redundant 'include' + # lines is safe, though. + if "versioneer.py" not in simple_includes: + print(" appending 'versioneer.py' to MANIFEST.in") + with open(manifest_in, "a") as f: + f.write("include versioneer.py\n") + else: + print(" 'versioneer.py' already in MANIFEST.in") + if cfg.versionfile_source not in simple_includes: + print( + " appending versionfile_source ('%s') to MANIFEST.in" + % cfg.versionfile_source + ) + with open(manifest_in, "a") as f: + f.write("include %s\n" % cfg.versionfile_source) + else: + print(" versionfile_source already in MANIFEST.in") + + # Make VCS-specific changes. For git, this means creating/changing + # .gitattributes to mark _version.py for export-subst keyword + # substitution. + do_vcs_install(manifest_in, cfg.versionfile_source, ipy) + return 0 + + +def scan_setup_py(): + """Validate the contents of setup.py against Versioneer's expectations.""" + found = set() + setters = False + errors = 0 + with open("setup.py", "r") as f: + for line in f.readlines(): + if "import versioneer" in line: + found.add("import") + if "versioneer.get_cmdclass()" in line: + found.add("cmdclass") + if "versioneer.get_version()" in line: + found.add("get_version") + if "versioneer.VCS" in line: + setters = True + if "versioneer.versionfile_source" in line: + setters = True + if len(found) != 3: + print("") + print("Your setup.py appears to be missing some important items") + print("(but I might be wrong). Please make sure it has something") + print("roughly like the following:") + print("") + print(" import versioneer") + print(" setup( version=versioneer.get_version(),") + print(" cmdclass=versioneer.get_cmdclass(), ...)") + print("") + errors += 1 + if setters: + print("You should remove lines like 'versioneer.VCS = ' and") + print("'versioneer.versionfile_source = ' . This configuration") + print("now lives in setup.cfg, and should be removed from setup.py") + print("") + errors += 1 + return errors + + +if __name__ == "__main__": + cmd = sys.argv[1] + if cmd == "setup": + errors = do_setup() + errors += scan_setup_py() + if errors: + sys.exit(1)