Alternative to infinitered/nsfwjs
This project demonstrates how to classify images using a pre-trained model with ONNX Runtime Web.
Here is an example of how to use the provided load
function to classify an image:
import { load } from '@l4ph/nsfwts';
const image = document.getElementById('your-image-id') as HTMLImageElement;
const modelUrl = 'path-to-your-model.onnx';
load(image, modelUrl).then(results => {
console.log('Classification Results:', results);
}).catch(error => {
console.error('Error during classification:', error);
});
Classifies an input image using the specified ONNX model.
image
: AnHTMLImageElement
representing the image to be classified.modelUrl
: A string representing the URL to the ONNX model.
A Promise
that resolves to a Record<string, number>
, where the keys are the classification labels and the values are the corresponding probabilities.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Classification Example</title>
</head>
<body>
<img id="your-image-id" src="path-to-your-image.jpg" alt="Image for Classification">
<script type="module">
import { load } from '@l4ph/nsfwts';
const image = document.getElementById('your-image-id');
const modelUrl = 'path-to-your-model.onnx';
load(image, modelUrl).then(results => {
console.log('Classification Results:', results);
}).catch(error => {
console.error('Error during classification:', error);
});
</script>
</body>
</html>
This project is licensed under the MIT License. See the LICENSE file for details.