rembg

Видаляє фон за об’єктом. Використовує навчені моделі (посилання є на GitHub)

https://pypi.org/project/rembg/2.0.28/

pip install rembg

Встановлювати в вірт.середоище. Ставить близько 700 Мб модулів.

attrs
certifi
charset-normalizer
coloredlogs
flatbuffers
humanfriendly
idna
imageio
jsonschema
jsonschema-specifications
lazy_loader
llvmlite
mpmath
networkx
numba
numpy
onnxruntime
opencv-python-headless
packaging
pillow
platformdirs
pooch
protobuf
pymatting
referencing
requests
rpds-py
scikit-image
scipy
sympy
tifffile
tqdm
urllib3
#!/home/user/dev/rembg/env/bin/python

from rembg import remove

input_path = '/home/user/dev/rembg/test.jpg'
output_path = '/home/user/dev/rembg/test_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)

При першому запуску становлює файл загальної навченої моделі в /home/user/.u2net/

В цю директорію можно скопіювати й інші моделі.

Щоб запускати з командної строки необхідно ще наставити модулів:

pip install click
pip install filetype
pip install watchdog
pip install aiohttp
    > multidict, frozenlist, async-timeout, yarl, aiosignal, aiohttp
pip install gradio
    > aiofiles, altair, annotated-types, anyio, contourpy, cycler, exceptiongroup,
    fastapi, ffmpy, filelock, fonttools, fsspec, gradio-client, h11, httpcore, httpx,
    huggingface-hub, importlib-resources, jinja2, kiwisolver, markdown-it-py,
    markupsafe, matplotlib, mdurl, orjson, pandas, pydantic, pydantic-core, pydub,
    pygments, pyparsing, python-dateutil, python-multipart, pytz, pyyaml, rich, ruff,
    semantic-version, shellingham, six, sniffio, starlette, tomlkit, toolz, typer,
    typing-extensions, tzdata, uvicorn, websockets
pip install asyncer

Тепер можна запускати так:

rembg i -m MODEL ~/img/test.jpg ~/img/test_output.pngg

де MODEL - необхідний файл моделі.

Трохи краще результат, з ключем -a - виходить менша сіра кайма по краям:

rembg i -a -m MODEL ~/img/test.jpg ~/img/test_output.png

Last updated