Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎨 Image-To-Anime

Convert photos and videos into anime-style art with AnimeGANv3 (Hayao style) running on ONNX Runtime.

License: GPL v3

Two small scripts wrap an AnimeGANv3 generator exported to ONNX: imagetoanime.py stylises a single image, and videotoanime.py stylises a whole video frame by frame. The Hayao-style model (AnimeGANv3_Hayao_36.onnx) is bundled in the repository, inference runs on CPU by default, and CUDA GPU inference is available via a flag.

✨ Features

  • Photo → anime conversion via OpenCV + ONNX Runtime, output saved at the input's original resolution
  • Video → anime conversion: frame-by-frame stylisation re-encoded to MP4 (mp4v) at the source FPS and resolution
  • Bundled modelAnimeGANv3_Hayao_36.onnx ships in the repo; any other AnimeGANv3 .onnx model can be supplied with -m (models with "tiny" in the filename automatically switch to 16-pixel alignment)
  • CPU by default, GPU optional-d gpu uses the CUDAExecutionProvider when a GPU-enabled onnxruntime build is detected, otherwise falls back to CPU
  • Model-friendly resizing — inputs are resized to multiples of 8 (minimum 256 px) before inference
  • Threaded video pipeline — a daemon reader thread decodes frames into a bounded queue (max 100) while the main loop runs inference; videos larger than 1280 px on the long edge are downscaled for inference and upscaled back on output to protect GPU memory

📋 Requirements

  • Python 3
  • Third-party packages (no requirements.txt; derived from imports) — install:
pip install opencv-python numpy onnxruntime Pillow tqdm
  • Pillow ≥ 9.1 (videotoanime.py uses Image.Resampling)
  • For GPU inference: an onnxruntime-gpu build with CUDA

🛠 Installation

git clone https://github.com/mkamranr/Image-To-Anime.git
cd Image-To-Anime
pip install opencv-python numpy onnxruntime Pillow tqdm

The model AnimeGANv3_Hayao_36.onnx is included in the repository root — no separate download is needed. Note that both scripts' default -m value points to a models/ subfolder, so either create it and move the model there:

mkdir models && mv AnimeGANv3_Hayao_36.onnx models/

or pass the model path explicitly with -m AnimeGANv3_Hayao_36.onnx.

🚀 Usage

Image

python imagetoanime.py -i photo.jpg -o anime.jpg -m AnimeGANv3_Hayao_36.onnx -d cpu
Argument Default Description
-i, --input_img_path /home/ada/test_data (placeholder — always pass your own) input image
-m, --model_path models/AnimeGANv3_Hayao_36.onnx ONNX model file
-o, --output_image_path outputimage.jpg output image
-d, --device cpu cpu or gpu

Prints the image size and processing time, then writes the stylised image at the original resolution.

Video

python videotoanime.py -i input.mp4 -o out/anime.mp4 -m AnimeGANv3_Hayao_36.onnx -d cpu
Argument Default Description
-i, --input_video_path vid\1.mp4 (placeholder — always pass your own) input video
-m, --model_path models\AnimeGANv3_Hayao_36.onnx ONNX model file
-o, --output_file_path dist\outputvideo.mp4 output MP4 (its directory must already exist)
-d, --device cpu cpu or gpu

Prints a countdown of remaining frames while running, then output video: <path> when done.

⚙️ How it works

Each frame or image is resized so both dimensions are divisible by 8 (16 for "tiny" model variants, minimum 256 px), converted BGR → RGB and normalised to [-1, 1]. The AnimeGANv3 generator is executed through an onnxruntime.InferenceSession (CUDA provider when -d gpu and a GPU build is available, otherwise CPU). The network output is denormalised back to 8-bit RGB and resized to the source resolution. For video, a daemon thread reads and preprocesses frames into a bounded queue while the main loop runs inference and writes stylised frames to a cv2.VideoWriter (mp4v codec) at the source FPS; if the stream breaks mid-way the script reports "The video is broken, please upload the video again."

📁 Project structure

File Purpose
imagetoanime.py Single-image conversion CLI
videotoanime.py Video conversion CLI (threaded frame pipeline)
AnimeGANv3_Hayao_36.onnx Bundled AnimeGANv3 generator, Hayao style
LICENSE GPL-3.0

⚠️ Notes / Limitations

  • Only the Hayao-style model is bundled; other AnimeGANv3 models must be obtained separately and passed via -m.
  • The default input paths are development leftovers (/home/ada/test_data, vid\1.mp4) — always supply -i.
  • The output directory for videos is not created automatically, and the default output paths use Windows-style backslashes.
  • -d gpu silently falls back to CPU if onnxruntime was not built with GPU support; videotoanime.py pins CUDA_VISIBLE_DEVICES=0.
  • Exceptions are caught and printed rather than raised, so the scripts exit with status 0 even on failure.

📄 License

This project is licensed under the GNU General Public License v3.0 — see LICENSE.

Releases

Packages

Contributors

Languages