im-syn / SafeVision

SafeVision is a professional Python script designed to detect and blur nudity in both videos and images.
Apache License 2.0
4 stars 0 forks source link
ai detection ml model nsfw-detection nsfw-recognition onnx onnxruntime py python safevision script

SafeVision

GitHub Temp

SafeVision is a sophisticated Python script designed to detect and blur nudity in both images and videos. By harnessing advanced computer vision and deep learning techniques, SafeVision ensures that potentially sensitive or inappropriate content is effectively obscured, promoting safer and more appropriate media sharing and consumption. This README file provides comprehensive instructions on setting up and utilizing SafeVision effectively.

Features

Installation

  1. Clone the Repository:

    git clone https://github.com/im-syn/safevision.git
    cd safevision
  2. Install Dependencies:

    pip install -r requirements.txt
  3. Download the Model:

    • Ensure the best.onnx model is placed in the Models directory.

Usage

Demo Image

Command-line Arguments

Example Commands

  1. Detect nudity and draw boxes:

    python main.py -i path/to/media.jpg
  2. Detect nudity and blur detected regions:

    python main.py -i path/to/media.jpg -b
  3. Specify output path:

    python main.py -i path/to/media.jpg -o path/to/output.jpg
  4. Use exception rules file:

    python main.py -i path/to/media.jpg -e path/to/BlurException.rule
  5. Set full blur rule and use exception rules file:

    python main.py -i path/to/media.jpg -e path/to/BlurException.rule -fbr 25

Blur Exception Rules File (BlurException.rule)

FACE_MALE = false
FACE_FEMALE = false
ARMPITS_COVERED = false
FEET_COVERED = false
BUTTOCKS_COVERED = false
BELLY_COVERED = false
FEMALE_GENITALIA_COVERED = false
FEMALE_BREAST_COVERED = false
ANUS_COVERED = false
MALE_GENITALIA_EXPOSED = true
MALE_BREAST_EXPOSED = true
ANUS_EXPOSED = true
FEET_EXPOSED = false
ARMPITS_EXPOSED = true
FEMALE_GENITALIA_EXPOSED = true
FEMALE_BREAST_EXPOSED = true
BUTTOCKS_EXPOSED = true
BELLY_EXPOSED = false

In the BlurException.rule file, false indicates that the corresponding body part will not be blurred, while true indicates that it will be blurred.

Video Processing

Additional Command-line Arguments for Video

Example Commands for Video Processing

  1. Process video and draw boxes:

    python main.py -i path/to/video.mp4 -t video
  2. Process video and blur detected regions:

    python main.py -i path/to/video.mp4 -t video -b
  3. Specify video output folder:

    python main.py -i path/to/video.mp4 -t video -vo path/to/video_output
  4. Use blur rule:

    python main.py -i path/to/video.mp4 -t video -r 50/10

How Video Processing Works

The video processing follows a similar structure to image processing, with additional steps for handling frames and creating videos.

How It Works

Overview

The script processes an input image or video to detect and blur nudity based on a trained ONNX model. It includes the following main components:

  1. Image/Video Preprocessing:

    • Reads and preprocesses the image or video frames, resizing and normalizing them for the model.
  2. Model Inference:

    • The NudeDetector class uses ONNX Runtime to load the model and perform inference to detect nudity.
  3. Postprocessing:

    • Processes the model output to extract detected bounding boxes and labels.
  4. Blurring and Drawing Boxes:

    • Applies blurring to detected nudity or draws bounding boxes based on user configuration.

Detailed Code Explanation

Image/Video Preprocessing

Model Inference

Postprocessing

Blurring and Drawing Boxes

Creating Directories

The script ensures necessary directories exist for saving output images and videos.

Output

Conclusion

SafeVision provides a robust solution for detecting and blurring nudity in images and videos, making it a valuable tool for content moderation and safe media sharing. Follow the instructions in this README to set up and use SafeVision effectively. If you have any questions or issues, please refer to the documentation or contact me.