span-ncnn-py
Custom models:
- ClearRealityV1 (4X) by Kim2091
- SPANkendata (4X) by terrainer
- 2x-span-anime-pretrain (2X) by Kim2091
- 2xHFA2kSpan (2x) by Phhofm Python Binding for span-ncnn-py with PyBind11
Span go brrr for upscaling images.
Current building status matrix
| System | Status | CPU (32bit) | CPU (64bit) | GPU (32bit) | GPU (64bit) |
|---|---|---|---|---|---|
| Linux (Clang) | — | ✅ | — | ✅ | |
| Linux (GCC) | — | ✅ | — | ✅ | |
| Windows | — | ✅ | — | ✅ | |
| MacOS | — | ✅ | — | ✅ | |
| MacOS (ARM) | — | ✅ | — | ✅ |
Usage
Python >= 3.6 (>= 3.9 in MacOS arm)
To use this package, simply install it via pip:
pip install span-ncnn-py
For Linux user:
apt install -y libomp5 libvulkan-dev
Then, import the span class from the package:
from span_ncnn_py import span
To initialize the model:
span = Span(gpuid: int = 0, tta_mode: bool = False, tilesize: int = 0, model: int = 0)
# model can be 0, 1, 2, 3, 4, 5, 6; 0 for default
# 0: {"param": "spanx2_ch48.param", "bin": "spanx2_ch48.bin", "scale": 2},
# 1: {"param": "spanx2_ch52.param", "bin": "spanx2_ch52.bin", "scale": 2},
# 2: {"param": "spanx4_ch48.param", "bin": "spanx4_ch48.bin", "scale": 4},
# 3: {"param": "spanx4_ch52.param", "bin": "spanx4_ch52.bin", "scale": 4},
# 4: {"param": "2xHFA2kSPAN_27k.param", "bin": "2xHFA2kSPAN_27k.bin", "scale": 2},
# 5: {"param": "4xSPANkendata.param", "bin": "4xSPANkendata.bin", "scale": 4},
# 6: {"param": "ClearReality4x.param", "bin": "ClearReality4x.bin", "scale": 4}
Here, gpuid specifies the GPU device to use, tta_mode enables test-time augmentation, tilesize specifies the tile size for processing (0 or >= 32), and model specifies the num of the pre-trained model to use.
Once the model is initialized, you can use the upscale method to super-resolve your images:
Pillow
from PIL import Image
span = span(gpuid=0)
with Image.open("input.jpg") as image:
image = span.process_pil(image)
image.save("output.jpg", quality=95)
opencv-python
import cv2
span = span(gpuid=0)
image = cv2.imdecode(np.fromfile("input.jpg", dtype=np.uint8), cv2.IMREAD_COLOR)
image = span.process_cv2(image)
cv2.imencode(".jpg", image)[1].tofile("output_cv2.jpg")
ffmpeg
import subprocess as sp
# your ffmpeg parameters
command_out = [FFMPEG_BIN, ........]
command_in = [FFMPEG_BIN, ........]
pipe_out = sp.Popen(command_out, stdout=sp.PIPE, bufsize=10 ** 8)
pipe_in = sp.Popen(command_in, stdin=sp.PIPE)
realesrgan = Realesrgan(gpuid=0)
while True:
raw_image = pipe_out.stdout.read(src_width * src_height * 3)
if not raw_image:
break
raw_image = realesrgan.process_bytes(raw_image, src_width, src_height, 3)
pipe_in.stdin.write(raw_image)
Build
The project just only been tested in Ubuntu 18+ and Debian 9+ environments on Linux, so if the project does not work on your system, please try building it.
References
The following references were used in the development of this project:
xinntao/Real-ESRGAN-ncnn-vulkan - This project was the main inspiration for our work. It provided the core implementation of the Real-ESRGAN algorithm using the ncnn and Vulkan libraries.
Real-ESRGAN - Real-ESRGAN is an AI super resolution model, aims at developing Practical Algorithms for General Image/Video Restoration.
media2x/realsr-ncnn-vulkan-python - This project was used as a reference for implementing the wrapper. Special thanks to the original author for sharing the code.
ncnn - ncnn is a high-performance neural network inference framework developed by Tencent AI Lab.
License
This project is licensed under the BSD 3-Clause - see the LICENSE file for details.
Metadata
Release files for span-ncnn-py 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| span_ncnn_py-1.2.0.tar.gz | 10.3 MB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| span_ncnn_py-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.3 MB
Release files / span_ncnn_py-1.2.0.tar.gz
| Download URL | span_ncnn_py-1.2.0.tar.gz |
|---|---|
| Size | 10.3 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e07ae51ea917e3c1f9a88a9cd6e395321d76ee1fbac1f578468f4deba7c28796
|
|
BLAKE2b-256 checksum How to use checksums |
2048ac0f97cca79bd45620d7405c97cc2695b54ec75b36a9e3506ece135002f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.2
|
Release files / span_ncnn_py-1.2.0-py3-none-any.whl
| Download URL | span_ncnn_py-1.2.0-py3-none-any.whl |
|---|---|
| Size | 9.0 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9c9160eb6d85f8a50c53ec6cbc141940f6ffc19e7076d9f39fea36ebec43de07
|
|
BLAKE2b-256 checksum How to use checksums |
9b9c8f9af37df429492dee8ff970bd263e1609cdfa487f96143aa98c66418e2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.12.2
|
Security for span-ncnn-py 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.