### Video Scene Detection with PySceneDetect Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Employ PySceneDetect to identify scene transitions in videos using content-aware algorithms. This enables frame-accurate segmentation for per-scene analysis. Install with `pip install scenedetect[opencv]`. ```python # pip install scenedetect[opencv] from scenedetect import detect, ContentDetector, split_video_ffmpeg video_path = "movie_trailer.mp4" # Detect scenes with content-aware algorithm (threshold=27.0 is a common default) scene_list = detect(video_path, ContentDetector(threshold=27.0)) print(f"Detected {len(scene_list)} scenes:") for i, (start, end) in enumerate(scene_list): duration_s = (end - start).get_seconds() print(f" Scene {i+1:02d}: {start.get_timecode()} → {end.get_timecode()} ({duration_s:.2f}s)") # Output: # Detected 12 scenes: # Scene 01: 00:00:00.000 → 00:00:04.167 (4.17s) # Scene 02: 00:00:04.167 → 00:00:09.042 (4.88s) # ... # Optionally split into individual scene clips for per-scene moderation split_video_ffmpeg(video_path, scene_list, output_dir="scenes/") ``` -------------------------------- ### NSFW Image Detection with LAION Safety Toolkit Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Use the LAION Safety Toolkit for fast NSFW detection in images, trained on the LAION-5B dataset. Ensure the library is installed from its GitHub repository. ```python # pip install git+https://github.com/LAION-AI/LAION-SAFETY from laion_safety import NSFWDetector from PIL import Image detector = NSFWDetector() image_paths = ["image1.jpg", "image2.jpg", "image3.png"] images = [Image.open(p).convert("RGB") for p in image_paths] results = detector.predict(images) # returns list of probabilities (0=safe, 1=nsfw) for path, prob in zip(image_paths, results): label = "NSFW" if prob > 0.5 else "SAFE" print(f"{label} ({prob:.3f}) {path}") # Output: # SAFE (0.021) image1.jpg # NSFW (0.887) image2.jpg # SAFE (0.043) image3.png ``` -------------------------------- ### Extract Audio from Video using MoviePy Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Extracts audio from a video file and saves it as a WAV file with a specified sample rate. Requires 'moviepy' to be installed. ```python clip = VideoFileClip("movie.mp4") clip.audio.write_audiofile("movie_audio.wav", fps=16000) ``` -------------------------------- ### Video Action Recognition with SlowFast (3D CNN) Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Perform action classification using the SlowFast network via PyTorchVideo. Requires installation of pytorchvideo and provides predictions for Kinetics-400 classes. ```python # SlowFast inference via PyTorchVideo for content moderation # Install: pip install pytorchvideo import torch from pytorchvideo.models.hub import slowfast_r50 from pytorchvideo.transforms import ( ApplyTransformToKey, ShortSideScale, UniformTemporalSubsample, ) from torchvision.transforms import Compose, Lambda import json, urllib.request model = slowfast_r50(pretrained=True) model.eval() # SlowFast expects a list of [slow_pathway, fast_pathway] tensors # slow: 8 frames @ 224×224, fast: 32 frames @ 224×224 slow = torch.randn(1, 3, 8, 224, 224) fast = torch.randn(1, 3, 32, 224, 224) with torch.no_grad(): preds = model([slow, fast]) # shape: (1, 400) top5 = preds[0].topk(5) # Load Kinetics-400 class list url = "https://dl.fbaipublicfiles.com/pyslowfast/dataset/class_names/kinetics_classnames.json" with urllib.request.urlopen(url) as f: class_names = {v: k for k, v in json.load(f).items()} print("Top-5 actions:", [class_names[i.item()] for i in top5.indices]) # Output: Top-5 actions: ['punching person (boxing)', 'kicking', ...] ``` -------------------------------- ### Video Action Recognition with Video Swin Transformer Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Extract features for violence detection using the Video Swin Transformer. Requires installation of mmaction2 and specific configuration/checkpoint files. ```python # Video Swin Transformer feature extraction for violence detection # Install: pip install mmaction2 from mmaction.apis import init_recognizer, inference_recognizer config_file = "configs/recognition/swin/swin-base_imagenet-pretrain_8xb8-amp-32x2x1-30e_kinetics400-rgb.py" checkpoint = "https://download.openmmlab.com/mmaction/v1.0/recognition/swin/..." model = init_recognizer(config_file, checkpoint, device="cuda:0") # Single video inference video_path = "clip.mp4" result = inference_recognizer(model, video_path) # Map Kinetics-400 action labels to content moderation categories VIOLENCE_KINETICS_LABELS = { "punching person (boxing)", "kicking", "wrestling", "sword fighting", "shooting gun", } top_labels = {result.pred_label[i]: result.pred_score[i].item() for i in range(5)} is_violent = any(lbl in VIOLENCE_KINETICS_LABELS for lbl in top_labels) print("Violence detected:", is_violent) print("Top predictions:", top_labels) # Output: # Violence detected: True # Top predictions: {'punching person (boxing)': 0.91, 'wrestling': 0.04, ...} ``` -------------------------------- ### LLM Pseudo-Labeler for Wildlife Trafficking Detection Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Uses OpenAI's GPT-4o model to screen marketplace listings for potential illegal wildlife trafficking. Requires an OpenAI client setup and provides JSON output with trafficking status, confidence, species, and reason. ```python from openai import OpenAI client = OpenAI() SYSTEM_PROMPT = """You are an expert in wildlife trade law (CITES/IUCN). Given a marketplace listing, determine whether it likely involves illegal wildlife trafficking. Respond with JSON: {"trafficking": true/false, "confidence": 0-1, "species": "...", "reason": "..."}""" def screen_listing(title: str, description: str, image_url: str | None = None) -> dict: """ LLM-based pseudo-labeler for wildlife trafficking detection. Achieves up to 95% F1 per ACM 2025 paper when used to train a downstream classifier. """ content = [{"type": "text", "text": f"Title: {title}\n\nDescription: {description}"}] if image_url: content.append({"type": "image_url", "image_url": {"url": image_url}}) response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": content}, ], response_format={"type": "json_object"}, ) import json return json.loads(response.choices[0].message.content) ``` -------------------------------- ### Multilingual Profanity Detection with SafeText Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Utilize the SafeText library for detecting and censoring profanity in text across multiple languages. Ensure the library is installed via pip. ```python # pip install safetext from safetext import SafeText st = SafeText(language="en") texts = [ "This is a perfectly fine comment.", "What the f*** is wrong with you!", "Please keep the discussion civil.", ] for text in texts: result = st.check_profanity(text) if result["has_profanity"]: filtered = st.censor_profanity(text) print(f"FLAGGED | original='{text}' | censored='{filtered}'") else: print(f"CLEAN | '{text}'") # Output: # CLEAN | 'This is a perfectly fine comment.' # FLAGGED | original='What the f*** is wrong with you!' | censored='What the **** is wrong with you!' # CLEAN | 'Please keep the discussion civil.' ``` -------------------------------- ### Extract Frames from Video using MoviePy Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Extracts uniformly sampled frames from a video at a specified frames per second (fps) rate. Useful for downstream NSFW classification. Ensure 'moviepy' is installed. ```python from moviepy.editor import VideoFileClip import numpy as np import os def extract_frames_for_moderation( video_path: str, output_dir: str, fps: float = 1.0, # 1 frame per second for efficiency ) -> list[str]: """ Extracts uniformly sampled frames from a video at `fps` rate. Returns list of saved frame paths for downstream NSFW classifier. """ os.makedirs(output_dir, exist_ok=True) clip = VideoFileClip(video_path) saved = [] for t in np.arange(0, clip.duration, 1.0 / fps): frame = clip.get_frame(t) # numpy (H, W, 3) uint8 frame_path = os.path.join(output_dir, f"frame_{t:.2f}s.jpg") clip.save_frame(frame_path, t=t) saved.append(frame_path) clip.close() print(f"Extracted {len(saved)} frames from '{video_path}' → '{output_dir}'") return saved ``` -------------------------------- ### Zero-Shot NSFW Image Screening using CLIP Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Leverages CLIP for zero-shot content screening of images. It takes an image path, preprocesses it, and uses CLIP's text encoder to classify the image against predefined content labels. Requires CLIP model and PyTorch. ```python # Using CLIP (Learning transferable visual models from natural language supervision, 2021) # for zero-shot NSFW image screening — a common production pattern import clip import torch from PIL import Image device = "cuda" if torch.cuda.is_available() else "cpu" model, preprocess = clip.load("ViT-B/32", device=device) CONTENT_LABELS = [ "a safe, family-friendly image", "an image containing nudity", "an image containing graphic violence or gore", "an image depicting illegal drug use", "an image containing hate symbols", ] def zero_shot_content_screen(image_path: str) -> dict[str, float]: image = preprocess(Image.open(image_path)).unsqueeze(0).to(device) text = clip.tokenize(CONTENT_LABELS).to(device) with torch.no_grad(): img_features = model.encode_image(image) text_features = model.encode_text(text) logits_per_image, _ = model(image, text) probs = logits_per_image.softmax(dim=-1)[0].cpu().numpy() return {label: round(float(prob), 4) for label, prob in zip(CONTENT_LABELS, probs)} ``` -------------------------------- ### Load and Use OMNIVORE Model for Video/Image Analysis Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Load the OMNIVORE model from torch.hub for simultaneous analysis of images, videos, and depth maps. Ensure the model is set to evaluation mode. ```python # Using OMNIVORE (Meta, 2022) — a single model for images, videos, and depth # Available via torch.hub; useful for video-level content moderation import torch model = torch.hub.load( "facebookresearch/omnivore", model="omnivore_swinB_imagenet21k", pretrained=True, ) model.eval() # Video input: (B, C, T, H, W) — 8 frames at 224×224 video_tensor = torch.zeros(1, 3, 8, 224, 224) # Image input: (B, C, 1, H, W) image_tensor = torch.zeros(1, 3, 1, 224, 224) with torch.no_grad(): video_logits = model(video_tensor, input_type="video") image_logits = model(image_tensor, input_type="image") print("Video logits shape:", video_logits.shape) # torch.Size([1, 400]) Kinetics-400 classes print("Image logits shape:", image_logits.shape) # torch.Size([1, 1000]) ImageNet-1k classes ``` -------------------------------- ### Detect Wildlife and Objects with MegaDetector v5 Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Loads the MegaDetector v5 model for detecting animals, persons, and vehicles in camera trap images. Requires PyTorchWildlife and Pillow. Uses CUDA if available, otherwise CPU. ```python # pip install PytorchWildlife from PytorchWildlife.models import detection as pw_detection from PytorchWildlife.data import transforms as pw_transforms from PIL import Image import torch # Load MegaDetector v5 (trained on millions of camera-trap images) device = "cuda" if torch.cuda.is_available() else "cpu" detector = pw_detection.MegaDetectorV5(device=device, pretrained=True) transform = pw_transforms.MegaDetector_v5_Transform( target_size=detector.IMAGE_SIZE, stride=detector.STRIDE, ) image = Image.open("camera_trap_image.jpg").convert("RGB") tensor, _ = transform(image) with torch.no_grad(): results = detector.single_image_detection( tensor, image.size, "camera_trap_image.jpg", conf_thres=0.2 ) for det in results["detections"].xyxy[0]: x1, y1, x2, y2, conf, cls = det.tolist() category = {0: "animal", 1: "person", 2: "vehicle"}[int(cls)] print(f"Detected {category} at [{x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f}] conf={conf:.2f}") ``` -------------------------------- ### Define Violence Datasets Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Lists datasets for violence detection, including text, video, and multimodal inputs. Labels range from binary to fine-grained scene attributes. ```python violence_datasets = [ { "name": "Violence detection dataset", "year": 2020, "url": "https://github.com/airtlab/A-Dataset-for-Automatic-Violence-Detection-in-Videos", "modality": ["video"], "task": ["video classification"], "labels": ["violent", "not-violent"], }, { "name": "Violent Scenes Dataset", "year": 2014, "url": "https://www.interdigital.com/data_sets/violent-scenes-dataset", "modality": ["video"], "task": ["video classification"], "labels": ["blood", "fire", "gun", "gore", "fight"], }, { "name": "VSD2014", "year": 2014, "url": "http://www.cp.jku.at/datasets/VSD2014/VSD2014.zip", "modality": ["video"], "task": ["video classification"], "labels": ["blood", "fire", "gun", "gore", "fight"], }, { "name": "Movie script severity dataset", "year": 2021, "url": "https://github.com/RiTUAL-UH/Predicting-Severity-in-Movie-Scripts", "modality": ["text"], "task": ["text classification"], "labels": ["frightening", "mild", "moderate", "severe"], }, ] ``` -------------------------------- ### EfficientNet for Nudity Classification Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Fine-tunes a pre-trained EfficientNet-B4 backbone for nudity classification using a specific preprocessing pipeline. Suitable for general content moderation tasks classifying images into categories like safe, sexy, nude, hentai, or drawings. ```python # Pattern from "State-of-the-Art in Nudity Classification: A Comparative Analysis" # (IEEE ICASSPW 2023) — fine-tuning an EfficientNet backbone for nudity classification from torchvision import models, transforms from PIL import Image import torch # 1. Load pre-trained EfficientNet-B4 and adapt the head backbone = models.efficientnet_b4(weights="IMAGENET1K_V1") num_ftrs = backbone.classifier[1].in_features backbone.classifier[1] = torch.nn.Linear(num_ftrs, 5) # Classes: safe, sexy, nude, hentai, drawings (LSPD taxonomy) # 2. Preprocessing pipeline preprocess = transforms.Compose([ transforms.Resize(380), transforms.CenterCrop(380), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]) # 3. Inference def classify_image(image_path: str, model, class_names: list[str]) -> dict: img = Image.open(image_path).convert("RGB") tensor = preprocess(img).unsqueeze(0) model.eval() with torch.no_grad(): probs = torch.softmax(model(tensor), dim=-1)[0] return {cls: round(prob.item(), 4) for cls, prob in zip(class_names, probs)} classes = ["safe", "sexy", "nude", "hentai", "drawings"] # result = classify_image("sample.jpg", backbone, classes) # Example output: {'safe': 0.9821, 'sexy': 0.0102, 'nude': 0.0041, 'hentai': 0.002, 'drawings': 0.0016} ``` -------------------------------- ### Multi-modal Genre Classifier using CLIP, PANNs, and MLP Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt A PyTorch module for multi-label movie genre classification. It fuses embeddings from CLIP (visual/text) and PANNs (audio) using a sequential MLP. Designed for late-fusion architectures and requires pre-computed embeddings. ```python # Pattern from "Effectively leveraging Multi-modal Features for Movie Genre Classification" # (2022) — CLIP image + PANNs audio + CLIP text, fused via MLP import torch import torch.nn as nn GENRES = ["action", "comedy", "drama", "horror", "romance", "sci-fi", "thriller", "animation", "documentary"] class CLIPPANNsGenreClassifier(nn.Module): """ Multi-label genre classifier fusing CLIP visual/text embeddings with PANNs audio embeddings (MovieNet benchmark). """ def __init__(self, clip_dim=512, panns_dim=2048, num_genres=9): super().__init__() self.fusion = nn.Sequential( nn.Linear(clip_dim * 2 + panns_dim, 1024), nn.GELU(), nn.Dropout(0.4), nn.Linear(1024, num_genres), # BCEWithLogitsLoss for multi-label ) def forward(self, clip_img_emb, clip_txt_emb, panns_emb): fused = torch.cat([clip_img_emb, clip_txt_emb, panns_emb], dim=-1) return self.fusion(fused) # raw logits; apply sigmoid for probabilities model = CLIPPANNsGenreClassifier() model.eval() with torch.no_grad(): logits = model(torch.randn(1, 512), torch.randn(1, 512), torch.randn(1, 2048)) probs = torch.sigmoid(logits)[0] predicted = [GENRES[i] for i, p in enumerate(probs) if p > 0.5] print("Predicted genres:", predicted) ``` -------------------------------- ### Supervised Contrastive Loss for Feature Learning Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Implement supervised contrastive loss to pull embeddings of the same class together and push different classes apart. This is a backbone-agnostic method useful for learning discriminative feature spaces. ```python import torch import torch.nn.functional as F def supervised_contrastive_loss( features: torch.Tensor, # (N, D) L2-normalised embeddings labels: torch.Tensor, # (N,) integer class labels temperature: float = 0.07, ) -> torch.Tensor: """ Supervised contrastive loss — pulls embeddings of the same class together and pushes different classes apart. Backbone-agnostic. """ N = features.shape[0] sim_matrix = torch.matmul(features, features.T) / temperature # (N, N) # Mask out self-similarities mask_self = torch.eye(N, dtype=torch.bool, device=features.device) sim_matrix = sim_matrix.masked_fill(mask_self, float("-inf")) # Positive mask: same label, different sample label_matrix = labels.unsqueeze(0) == labels.unsqueeze(1) # (N, N) positive_mask = label_matrix & ~mask_self log_probs = F.log_softmax(sim_matrix, dim=-1) loss = -(log_probs * positive_mask.float()).sum(dim=-1) / positive_mask.float().sum(dim=-1).clamp(min=1) return loss.mean() # Example: batch of 8 embeddings, 3 content classes (safe=0, nsfw=1, violence=2) features = F.normalize(torch.randn(8, 128), dim=-1) labels = torch.tensor([0, 0, 1, 1, 2, 2, 0, 1]) loss = supervised_contrastive_loss(features, labels) print(f"Contrastive loss: {loss.item():.4f}") # Output: Contrastive loss: 1.8432 (decreases as embeddings cluster by class) ``` -------------------------------- ### Filter Image Nudity Datasets Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Selects image-only datasets relevant for nudity detection from a predefined list. Ensure the 'modality' includes 'image' and labels contain 'nude'. ```python datasets = [ { "name": "LSPD", "year": 2022, "url": "https://sites.google.com/uit.edu.vn/LSPD", "modality": ["image", "video"], "task": ["image/video classification", "instance segmentation"], "labels": ["porn", "normal", "sexy", "hentai", "drawings", "female/male genital", "female breast", "anus"], }, { "name": "Nudenet", "year": 2019, "url": "https://github.com/notAI-tech/NudeNet", "modality": ["image"], "task": ["image classification"], "labels": ["nude", "not nude"], }, { "name": "Adult content dataset", "year": 2017, "paper": "https://arxiv.org/abs/1612.09506", "modality": ["image"], "task": ["image classification"], "labels": ["nude", "not nude"], }, { "name": "Substance use dataset", "year": 2017, "paper": "https://web.cs.ucdavis.edu/~hpirsiav/papers/substance_ictai17.pdf", "modality": ["image"], "task": ["image classification"], "labels": ["drug related", "not drug related"], }, ] # Filter for image-only nudity datasets image_nudity_datasets = [ d for d in datasets if "image" in d["modality"] and any("nude" in lbl for lbl in d["labels"]) ] print(image_nudity_datasets) # Output: [{'name': 'Nudenet', ...}, {'name': 'Adult content dataset', ...}] ``` -------------------------------- ### Multi-Modal Trailer Content Rating Model Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Implements a multi-modal fusion model using concatenated video, text, and audio embeddings for trailer content rating. Useful for classifying movie trailers into categories like red, yellow, or green. ```python # Architectural pattern from "A Case Study of Deep Learning-Based Multi-Modal Methods # for Labeling the Presence of Questionable Content in Movie Trailers" (2021) # Model: multi-modal concat + MLP # Features: CNN+LSTM video, BERT+DeepMoji text, MFCC audio # Dataset: MM-Trailer | Task: red / yellow / green rating import torch import torch.nn as nn class MultiModalContentRater(nn.Module): """ Fuses video, text, and audio embeddings for trailer content rating. Follows the concat+MLP pattern described in the MM-Trailer paper. """ def __init__(self, video_dim=512, text_dim=768, audio_dim=128, num_classes=3): super().__init__() fused_dim = video_dim + text_dim + audio_dim self.classifier = nn.Sequential( nn.Linear(fused_dim, 256), nn.ReLU(), nn.Dropout(0.3), nn.Linear(256, num_classes), # red, yellow, green ) def forward(self, video_emb, text_emb, audio_emb): # video_emb: (B, 512) – CNN+LSTM over sampled frames # text_emb: (B, 768) – BERT [CLS] token of transcript # audio_emb: (B, 128) – MFCC statistics fused = torch.cat([video_emb, text_emb, audio_emb], dim=-1) return self.classifier(fused) # Inference example model = MultiModalContentRater() model.eval() with torch.no_grad(): video_emb = torch.randn(1, 512) text_emb = torch.randn(1, 768) audio_emb = torch.randn(1, 128) logits = model(video_emb, text_emb, audio_emb) label = ["red", "yellow", "green"][logits.argmax(dim=-1).item()] print(f"Predicted rating: {label}") # Expected output: Predicted rating: green (or red/yellow depending on random weights) ``` -------------------------------- ### Video Frame Extraction with MoviePy Source: https://context7.com/fcakyon/content-moderation-deep-learning/llms.txt Leverage MoviePy for preprocessing video content by extracting frames, audio segments, or subclips. This is a foundational step for video moderation pipelines. ```python # MoviePy example for frame extraction would go here. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.