@opennsfwjs/opennsfwjs

OpenNSFW.js

A nudity classifier for web browsers utilizing Yahoo's OpenNSFW model


Class OpenNSFW

A image classifier that determines an image's inappropriateness (in regards to nudity) with percent certainty. This percentage roughly correlates to the amount of nudity present in an image.

Utilizes a ported version of Yahoo's OpenNSFW model.

Constructors

  • Initialize the model handler. Be sure to call this.load() after initializing.

    Parameters

    • nsfw_threshold: number = .21

      The threshhold percentage (as a decimal) at which the image should be considered NSFW

    Returns OpenNSFW

Methods

  • Run the OpenNSFW model against an image

    Returns

    A promise that resolves to a single result or an array of results in the format: {nsfw_confidence = (0.0 - 1.0), is_nsfw = (nsfw_confidence > this.nsfw_threshold)}

    Parameters

    • images: Images | Images[]

      A single image or array of images of these types: ImageData, HTMLImageElement, HTMLCanvasElement, ImageBitmap

    Returns Promise<NSFWResult | NSFWResult[]>

  • Preprocess and classify an image-like object using the model

    Returns

    The NSFWResult representing the resulting classification

    Parameters

    • image: Images

      An image-like object to run against the model

    Returns Promise<NSFWResult>

  • Provides additional access to the model's load promise

    Returns

    A promise that resolves once the model load has completed

    Returns Promise<void>

  • Fetches an image from the provided URL and converts it to ImageData

    Returns

    A promise that resolves to ImageData containing the provided image

    Parameters

    • image_src: string

      The URL at which the image resides

    Returns Promise<ImageData>

  • Check the browser's IndexedDB storage for the model

    Returns

    A promise thats resolves to a boolean representing whether or not the model was cached

    Returns Promise<boolean>

  • Gets the load state of the model

    Returns

    A boolean representing whether or not the model has loaded

    Returns boolean

  • Load the model either from the project's GitHub repo or from the local cache (if it exists)

    Returns

    A void promise that resolves once the load has completed

    Parameters

    • save_after_loaded: boolean = true

      Whether or not to save the model locally immediately after loading (see this.save())

    Returns Promise<void>

  • Changes the image into a format and shape that is compatible with the model

    Returns

    A tensor representation of the preprocessed image

    Parameters

    • image: ImageData | HTMLImageElement | HTMLCanvasElement | ImageBitmap

      An image-like object on which to run the preprocessing

    Returns Tensor<Rank>

  • Feeds the model initial data to ensure subsequent classifications are fast. The longer run time of the first image is unfortunately intrinsic to this variety of ML model.

    Returns

    A void promise that resolves when the priming finishes

    Returns Promise<void>

  • Save the model locally for future use (IndexedDB)

    Returns

    A void promise that resolves once the save is complete

    Returns Promise<void>

Generated using TypeDoc