add tests

This commit is contained in:
Daniel Gatis 2022-08-24 20:10:40 -03:00
parent f2efa5d4c5
commit e7a8a209db
19 changed files with 180 additions and 123 deletions

View File

@ -3,8 +3,7 @@ name: Bug report
about: Create a report to help us improve
title: "[BUG] ..."
labels: bug
assignees: ''
assignees: ""
---
**Describe the bug**
@ -12,6 +11,7 @@ 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 '....'

View File

@ -3,8 +3,7 @@ name: Feature request
about: Suggest an idea for this project
title: "[FEATURE] ..."
labels: enhancement
assignees: ''
assignees: ""
---
**Is your feature request related to a problem? Please describe.**

View File

@ -1,4 +1,4 @@
name: lint_python
name: Lint
on: [pull_request, push]

View File

@ -10,20 +10,16 @@ jobs:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v2
-
name: Login to Docker Hub
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
-
name: Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
-
name: Build and push
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .

25
.github/workflows/tests.yml vendored Normal file
View File

@ -0,0 +1,25 @@
name: Run tests
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
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: |
python -m pip install --upgrade pip
pip install pytest
pip install -r requirements.txt
- name: Test with pytest
run: |
pytest

1
.gitignore vendored
View File

@ -13,6 +13,7 @@ __pycache__/
.envrc
.python-version
.idea
.pytest_cache
# due to using tox and pytest
.tox

View File

@ -3,6 +3,8 @@ include LICENSE.txt
include README.md
include setup.py
include pyproject.toml
include requirements.txt
include requirements-gpu.txt
include versioneer.py
include rembg/_version.py

View File

@ -37,17 +37,16 @@ Rembg is a tool to remove images background. That is it.
**If this project has helped you, please consider making a [donation](https://www.buymeacoffee.com/danielgatis).**
### Installation
#### **!! This library is for Python 3.9 only !!**
CPU support:
```bash
pip install rembg
```
GPU support:
```bash
pip install rembg[gpu]
```
@ -55,16 +54,19 @@ pip install rembg[gpu]
### Usage as a cli
Remove the background from a remote image
```bash
curl -s http://input.png | rembg i > output.png
```
Remove the background from a local file
```bash
rembg i path/to/input.png path/to/output.png
```
Remove the background from all images in a folder
```bash
rembg p path/to/input path/to/output
```
@ -72,6 +74,7 @@ rembg p path/to/input path/to/output
### Usage as a server
Start the server
```bash
rembg s
```
@ -83,18 +86,25 @@ http://localhost:5000/docs
```
Image with background:
```
https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/1280px-Gull_portrait_ca_usa.jpg
```
Image without background:
```
http://localhost:5000/?url=https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/1280px-Gull_portrait_ca_usa.jpg
```
Also you can send the file as a FormData (multipart/form-data):
```html
<form action="http://localhost:5000" method="post" enctype="multipart/form-data">
<form
action="http://localhost:5000"
method="post"
enctype="multipart/form-data"
>
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
@ -103,6 +113,7 @@ Also you can send the file as a FormData (multipart/form-data):
### Usage as a library
Input and output as bytes
```python
from rembg import remove
@ -117,6 +128,7 @@ with open(input_path, 'rb') as i:
```
Input and output as a PIL image
```python
from rembg import remove
from PIL import Image
@ -130,6 +142,7 @@ output.save(output_path)
```
Input and output as a numpy array
```python
from rembg import remove
import cv2
@ -151,11 +164,13 @@ docker run -p 5000:5000 danielgatis/rembg s
```
Image with background:
```
https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/1280px-Gull_portrait_ca_usa.jpg
```
Image without background:
```
http://localhost:5000/?url=https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/1280px-Gull_portrait_ca_usa.jpg
```
@ -179,6 +194,7 @@ https://github.com/danielgatis/rembg/issues/193#issuecomment-1055534289
### Advance usage
Sometimes it is possible to achieve better results by turning on alpha matting. Example:
```bash
curl -s http://input.png | rembg i -a -ae 15 > output.png
```
@ -211,6 +227,7 @@ Please contact me at danielgatis@gmail.com if you need help to put it on the clo
- https://github.com/pymatting/pymatting
### Buy me a coffee
Liked some of my work? Buy me a coffee (or more likely a beer)
<a href="https://www.buymeacoffee.com/danielgatis" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;"></a>

3
pytest.ini Normal file
View File

@ -0,0 +1,3 @@
[pytest]
filterwarnings =
ignore::DeprecationWarning

View File

@ -1,9 +1,3 @@
import sys
import warnings
if not (sys.version_info.major == 3 and sys.version_info.minor == 9):
warnings.warn("This library is only for Python 3.9", RuntimeWarning)
from . import _version
__version__ = _version.get_versions()["version"]

View File

@ -16,7 +16,7 @@ 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.morphology import binary_erosion
from scipy.ndimage import binary_erosion
from .session_base import BaseSession
from .session_factory import new_session

View File

@ -18,7 +18,7 @@ class BaseSession:
std: Tuple[float, float, float],
size: Tuple[int, int],
) -> Dict[str, np.ndarray]:
im = img.convert("RGB").resize(size, Image.LANCZOS)
im = img.convert("RGB").resize(size, Image.Resampling.LANCZOS)
im_ary = np.array(im)
im_ary = im_ary / np.max(im_ary)

View File

@ -25,6 +25,6 @@ class SimpleSession(BaseSession):
pred = np.squeeze(pred)
mask = Image.fromarray((pred * 255).astype("uint8"), mode="L")
mask = mask.resize(img.size, Image.LANCZOS)
mask = mask.resize(img.size, Image.Resampling.LANCZOS)
return [mask]

View File

@ -1 +1 @@
onnxruntime-gpu==1.10.0
onnxruntime-gpu==1.12.1

View File

@ -1,17 +1,18 @@
aiohttp==3.8.1
asyncer==0.0.1
click==8.0.3
fastapi==0.72.0
filetype==1.0.9
click==8.1.3
fastapi==0.80.0
filetype==1.1.0
gdown==4.5.1
numpy==1.22.3
onnxruntime==1.12.0
imagehash==4.2.1
numpy==1.21.6
onnxruntime==1.12.1
opencv-python-headless==4.6.0.66
pillow==9.0.1
pymatting==1.1.7
pillow==9.2.0
pymatting==1.1.8
python-multipart==0.0.5
scikit-image==0.19.1
scipy==1.8.0
tqdm==4.62.3
uvicorn==0.17.0
watchdog==2.1.7
scikit-image==0.19.3
scipy==1.7.3
tqdm==4.64.0
uvicorn==0.18.3
watchdog==2.1.9

View File

@ -11,10 +11,10 @@ here = pathlib.Path(__file__).parent.resolve()
long_description = (here / "README.md").read_text(encoding="utf-8")
with open("requirements.txt") as f:
with open(here / "requirements.txt") as f:
requireds = f.read().splitlines()
with open("requirements-gpu.txt") as f:
with open(here / "requirements-gpu.txt") as f:
gpu_requireds = f.read().splitlines()
setup(
@ -27,11 +27,10 @@ setup(
author_email="danielgatis@gmail.com",
classifiers=[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3.9",
],
keywords="remove, background, u2net",
packages=["rembg"],
python_requires="~=3.9.0",
python_requires=">=3.7",
install_requires=requireds,
entry_points={
"console_scripts": [

20
tests/test_remove.py Normal file
View File

@ -0,0 +1,20 @@
from io import BytesIO
from pathlib import Path
from imagehash import average_hash
from PIL import Image
from rembg import remove
here = Path(__file__).parent.resolve()
def test_remove():
image = Path(here / ".." / "examples" / "animal-1.jpg").read_bytes()
expected = Path(here / ".." / "examples" / "animal-1.out.png").read_bytes()
actual = remove(image)
actual_hash = average_hash(Image.open(BytesIO(actual)))
expected_hash = average_hash(Image.open(BytesIO(expected)))
assert actual_hash == expected_hash