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.
- Nudity Detection: Detects various types of nudity in images and videos.
- Blur Functionality: Blurs detected nudity to ensure safe content sharing.
- Exception Rules: Configurable rules to control which parts should be blurred.
- Logging: Generates logs with detailed detection information.
-
Clone the Repository:
git clone https://github.com/im-syn/safevision.git cd safevision
-
Install Dependencies:
pip install -r requirements.txt
-
Download the Model:
- Ensure the
best.onnx
model is placed in theModels
directory.
- Ensure the
-i
or--input
: Path to the input image or video (required).-o
or--output
: Path to save the censored image or video. If not provided, a default path will be used.-b
or--blur
: Apply blur to NSFW regions instead of drawing boxes.-e
or--exception
: Path to the blur exception rules file.-fbr
or--full_blur_rule
: Number of exposed boxes to trigger full image blur.
-
Detect nudity and draw boxes:
python main.py -i path/to/media.jpg
-
Detect nudity and blur detected regions:
python main.py -i path/to/media.jpg -b
-
Specify output path:
python main.py -i path/to/media.jpg -o path/to/output.jpg
-
Use exception rules file:
python main.py -i path/to/media.jpg -e path/to/BlurException.rule
-
Set full blur rule and use exception rules file:
python main.py -i path/to/media.jpg -e path/to/BlurException.rule -fbr 25
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.
-vo
or--video_output
: Path to the video output folder. Default is 'video_output'.-r
or--rule
: Blur rule in the format 'percentage/count'.
-
Process video and draw boxes:
python main.py -i path/to/video.mp4 -t video
-
Process video and blur detected regions:
python main.py -i path/to/video.mp4 -t video -b
-
Specify video output folder:
python main.py -i path/to/video.mp4 -t video -vo path/to/video_output
-
Use blur rule:
python main.py -i path/to/video.mp4 -t video -r 50/10
The video processing follows a similar structure to image processing, with additional steps for handling frames and creating videos.
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:
-
Image/Video Preprocessing:
- Reads and preprocesses the image or video frames, resizing and normalizing them for the model.
-
Model Inference:
- The
NudeDetector
class uses ONNX Runtime to load the model and perform inference to detect nudity.
- The
-
Postprocessing:
- Processes the model output to extract detected bounding boxes and labels.
-
Blurring and Drawing Boxes:
- Applies blurring to detected nudity or draws bounding boxes based on user configuration.
- Reads the image or video frames using OpenCV.
- Resizes and pads the image or video frames to maintain aspect ratio.
- Normalizes the image or video frame data for model input.
- Initializes the ONNX model for inference.
- Loads exception rules for blurring.
- Transposes and processes model output to extract bounding boxes and class labels.
- Detects nudity in the image or video frames.
- Applies blurring or draws bounding boxes based on user options.
- Saves the processed image or video frames to the specified output path.
The script ensures necessary directories exist for saving output images and videos.
- Processed Images/Videos:
- Blur Folder: Contains all the output images with nudity blurred based on the applied rules.
- Output Folder: Stores debug images with detected boxes and blurring based on the rules.
- Prosses Folder: Contains images with only the detected boxes, without any blurring.
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.