Public
Like
vt-nsfw-detector
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
nsfw-detector.http.tsx
https://jmas--36c223389bcd11f0ba930224a6c84d84.web.val.run
A Val Town script that detects NSFW (Not Safe For Work) content in images using the nsfwjs library.
- HTTP API Endpoint: Accepts POST requests with JPEG image data
- NSFW Classification: Uses TensorFlow.js and nsfwjs for accurate content detection
- Multiple Categories: Detects Porn, Sexy, and Hentai content
- Confidence Scoring: Returns probability scores for each classification
- CORS Enabled: Supports cross-origin requests
- Error Handling: Comprehensive error handling and validation
- Backend Processing: Uses Deno-compatible image processing libraries
POST /nsfw-detector.http.tsx
Send a POST request with form data containing an image file in the image
field.
curl -X POST \ -F "image=@/path/to/your/image.jpg" \ https://your-val-town-url.val.run/nsfw-detector.http.tsx
{ "predictions": [ { "className": "Neutral", "probability": 0.85 }, { "className": "Porn", "probability": 0.12 }, { "className": "Sexy", "probability": 0.03 } ], "isNSFW": false, "confidence": 0.12, "processingTime": 1250 }
- predictions: Array of all classification results with probabilities
- isNSFW: Boolean indicating if content is considered NSFW (threshold: 0.5)
- confidence: Highest probability among NSFW categories (Porn, Sexy, Hentai)
- processingTime: Time taken to process the image in milliseconds
The model can classify images into these categories:
- Neutral: Safe, non-sexual content
- Drawing: Artistic drawings/illustrations
- Porn: Explicit sexual content
- Sexy: Suggestive but not explicit content
- Hentai: Explicit anime/manga content
{ "error": "Method not allowed. Use POST to upload images for NSFW detection." }
{ "error": "No image provided. Please include an 'image' field in your POST request." }
{ "error": "Unsupported file type. Please upload a JPEG image file." }
{ "error": "Internal server error during NSFW detection", "details": "Error message details" }
- Model: Uses nsfwjs v2.4.2 with TensorFlow.js v4.15.0
- Image Processing: Uses jpeg-js library to decode JPEG images and convert to tensors for nsfwjs compatibility
- Model Loading: Attempts multiple model sources for Val Town compatibility
- Performance: Model is loaded once and cached for subsequent requests
- Memory Management: Properly disposes tensors to prevent memory leaks
- Threshold: Content is considered NSFW if any NSFW category has probability > 0.5
This script is designed to run on Val Town. Simply upload the nsfw-detector.http.tsx
file to your Val Town account and it will be available as an HTTP endpoint.
This project uses the nsfwjs library for content detection. Please refer to their license terms for usage restrictions.