kornia-rs: low level computer vision library in Rust
The kornia crate is a low level library for Computer Vision written in Rust π¦
Use the library to perform image I/O, visualisation and other low level operations in your machine learning and data-science projects in a thread-safe and efficient way.
Getting Started
cargo run --bin hello_world -- --image-path path/to/image.jpg
use Image;
use functional as F;
Features
- π¦The library is primarly written in Rust.
- π Multi-threaded and efficient image I/O, image processing and advanced computer vision operators.
- π’ Efficient Tensor and Image API for deep learning and scientific computing.
- π Python bindings are created with PyO3/Maturin.
- π¦ We package with support for Linux [amd64/arm64], Macos and WIndows.
- Supported Python versions are 3.7/3.8/3.9/3.10/3.11/3.12/3.13, including the free-threaded build.
Supported image formats
- Read images from AVIF, BMP, DDS, Farbeld, GIF, HDR, ICO, JPEG (libjpeg-turbo), OpenEXR, PNG, PNM, TGA, TIFF, WebP.
Image processing
- Convert images to grayscale, resize, crop, rotate, flip, pad, normalize, denormalize, and other image processing operations.
Video processing
- Capture video frames from a camera and video writers.
π οΈ Installation
>_ System dependencies
Dependeing on the features you want to use, you might need to install the following dependencies in your system:
turbojpeg
gstreamer
** Check the gstreamr installation guide: https://docs.rs/gstreamer/latest/gstreamer/#installation
π¦ Rust
Add the following to your Cargo.toml:
[]
= "v0.1.9"
Alternatively, you can use each sub-crate separately:
[]
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
= { = "https://github.com/kornia/kornia-rs", = "v0.1.9" }
π Python
A subset of the full rust API is exposed. See the kornia documentation for more detail about the API for python functions and objects exposed by the kornia-rs Python module.
The kornia-rs library is thread-safe for use under the free-threaded Python build.
Examples: Image processing
The following example shows how to read an image, convert it to grayscale and resize it. The image is then logged to a rerun recording stream.
Checkout all the examples in the examples directory to see more use cases.
use ;
use functional as F;
Python usage
Load an image, that is converted directly to a numpy array to ease the integration with other libraries.
# load an image with using libjpeg-turbo
: =
# alternatively, load other formats
# img: np.ndarray = K.read_image_any("dog.png")
assert ==
# convert to dlpack to import to torch
=
assert ==
Write an image to disk
# load an image with using libjpeg-turbo
: =
# write the image to disk
Encode or decode image streams using the turbojpeg backend
# load image with kornia-rs
=
# encode the image with jpeg
=
# set the encoding quality
# get the encoded stream
: =
# decode back the image
=
: =
Resize an image using the kornia-rs backend with SIMD acceleration
# load image with kornia-rs
=
# resize the image
=
assert ==
π§βπ» Development
Pre-requisites: install rust and python3 in your system.
Install rustup in your system
|
Install uv to manage python dependencies
|
Install the just command runner. This tool is used to manage the development tasks.
Clone the repository in your local directory
You can check the available commands by running just in the root directory of the project.
π³ Devcontainer
This project includes a development container to provide a consistent development environment.
The devcontainer is configured to include all necessary dependencies and tools required for building and testing the kornia-rs project. It ensures that the development environment is consistent across different machines and setups.
How to use
-
Install Remote - Containers extension: In Visual Studio Code, install the
Remote - Containersextension from the Extensions view (Ctrl+Shift+X). -
Open the project in the container:
- Open the
kornia-rsproject folder in Visual Studio Code. - Press
F1and selectRemote-Containers: Reopen in Container.
- Open the
Visual Studio Code will build the container and open the project inside it. You can now develop, build, and test the project within the containerized environment.
π¦ Rust
Compile the project and run the tests
For specific tests, you can run the following command:
π Python
To build the Python wheels, we use the maturin package. Use the following command to build the wheels:
To run the tests, use the following command:
π Contributing
This is a child project of Kornia. Join the community to get in touch with us, or just sponsor the project: https://opencollective.com/kornia