NSFWPY 🔥 (nsfwpy-onnx)
High-performance, CPU-optimized Python 3.14 port of NSFWJS using ONNX Runtime.
Classify static images (JPEG, PNG, WEBP) and Animated WebP / GIF / APNG formats into 5 canonical NSFW categories (Drawing, Hentai, Neutral, Porn, Sexy).
🌟 Key Features
- 🐍 Python 3.14 Native: Fully compatible with Python 3.10+ and Python 3.14.
- 🎞️ Animated WebP & GIF Support: Keyframe sampling and frame-aggregated safety classification for Animated WebP, GIF, and APNG formats.
- ⚡ CPU Optimized: Powered by
onnxruntimetuned for multi-threaded CPU execution with graph optimization. - 📦 HuggingFace Auto-Download: Automatically downloads missing ONNX models from Hugging Face (
expertskb/nsfwpy). - 🪶 Ultra Lightweight: Minimal dependencies (
numpy,pillow,onnxruntime,click). No heavy web servers required. - 🛠️ CLI Tool Included: Classify images directly from your terminal.
📥 Installation
Install directly from PyPI:
pip install nsfwpy-onnx
Or install from GitHub:
git clone https://github.com/expertskb/nsfwpy-onnx.git
cd nsfwpy-onnx
python3.14 -m venv .venv
source .venv/bin/activate
pip install -e .
🚀 Quick Usage
1. Python Library Usage
import nsfwpy
# Load model (auto-downloads from HuggingFace if missing)
model = nsfwpy.load_model("mobilenet_v2")
# Classify static image or animated WebP / GIF
results = model.classify("sample.jpg", top_k=5)
print(results)
# Output:
# [
# {'className': 'Neutral', 'probability': 0.6280},
# {'className': 'Drawing', 'probability': 0.2822},
# {'className': 'Sexy', 'probability': 0.0548},
# {'className': 'Porn', 'probability': 0.0246},
# {'className': 'Hentai', 'probability': 0.0104}
# ]
# Batch image classification
batch_results = model.classify_batch(["img1.jpg", "anim2.gif"])
2. Command Line Interface (CLI)
# Classify a local image file or URL
nsfwpy classify path/to/image.jpg
# Output formatted JSON
nsfwpy classify sample.jpg --json-out
# Choose a specific model architecture
nsfwpy classify sample.jpg --model inception_v3
🧠 Supported Models
| Model Name | Backbone | Accuracy | Size | Auto-Download Link |
|---|---|---|---|---|
mobilenet_v2 (Default) |
MobileNet V2 | Fast | ~14 MB | HF Download |
mobilenet_v3 |
MobileNet V3 | Balanced | ~22 MB | HF Download |
inception_v3 |
Inception V3 | High Precision | ~95 MB | HF Download |
📂 Example Scripts Index
Professional production-ready scripts are included under examples/:
examples/01_basic_classification.py: Local image classification.examples/02_url_classification.py: Remote image URL classification.examples/03_batch_classification.py: High-throughput batch benchmarking.examples/04_pil_and_bytes_classification.py: In-memory PIL and bytes buffer processing.examples/05_custom_model_and_threading.py: Model backbones & ONNX CPU thread tuning.examples/06_animated_webp_gif_classification.py: Animated WebP & GIF frame-aggregated classification.
Run any example using:
python examples/01_basic_classification.py
📜 License
MIT License
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nsfwpy_onnx-1.0.6.tar.gz.
File metadata
- Download URL: nsfwpy_onnx-1.0.6.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
868aa3c55eac43f66e600a4f0b2f27fc2a4994d3cc2d7e143236c276ae5d5a25
|
|
| MD5 |
1bcd5a9b5dfab9ed593031031437c6f1
|
|
| BLAKE2b-256 |
7582144e2d355cc5c4151b8061898d5188b4593cf1051474a4c02b381a2fd376
|
File details
Details for the file nsfwpy_onnx-1.0.6-py3-none-any.whl.
File metadata
- Download URL: nsfwpy_onnx-1.0.6-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e49a041269aa0d468ded9c1c7100337fd9adb2dcaf3070bd0abbbfa669c20a3
|
|
| MD5 |
8d2804a563d79cd497e32326b98bfdf2
|
|
| BLAKE2b-256 |
1849e263bd437243c5221a85824eeaa94439750c2ee5c08c4893dafb0c5f84eb
|