<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>NSFWJS 鉴黄</title> <!-- TensorFlow.js --> <script src="https://netnr.eu.org/@tensorflow/tfjs@2.8.6/dist/tf.min.js"></script> <!-- Load the NSFWJS library --> <script src="https://netnr.eu.org/nsfwjs@2.4.2/dist/nsfwjs.min.js"></script> </head> <body> <input class="flag-file" type="file" accept="image/*"> <button class="flag-btn" >鉴别</button><br> <img class="flag-img" src="/favicon.ico" alt="Thumb preview..." style="max-height:10em;max-width:100%"> </body> </html>
img { max-width: 300px; max-height: 300px; }
const domImg = document.querySelector(".flag-img"); const domBtn = document.querySelector(".flag-btn"); domBtn.addEventListener("click", function () { domBtn.textContent = "Loading ..."; nsfwjs.load('https://netnr.eu.org/@clicks/nsfwjs@3.0.3/example/nsfw_demo/public/quant_nsfw_mobilenet/').then(function (model) { domBtn.textContent = "Ready ..."; model.classify(domImg).then(function (predictions) { console.table(predictions); domBtn.textContent = "Done!"; let domOut = document.createElement("div"); domOut.innerHTML = JSON.stringify(predictions); document.body.appendChild(domOut); }) }) }); document.querySelector(".flag-file").addEventListener("change", function () { domBtn.textContent = "鉴别"; var file = this.files[0]; if (file && file.image.startsWith("image/")) { var reader = new FileReader(); reader.onload = function () { domImg.src = reader.result; } reader.readAsDataURL(file); } });