-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
101 lines (87 loc) · 3.35 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片加载页面</title>
<style>
.container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.image {
width: 25%;
padding: 10px;
box-sizing: border-box;
}
.image img {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
<!-- Load TensorFlow.js. This is required -->
<script src="https://unpkg.com/@tensorflow/[email protected]" type="text/javascript"></script>
<script src="https://unpkg.com/[email protected]" type="text/javascript"></script>
</head>
<body>
<div class="container" id="container"></div>
<script>
window.onload = function () {
// Load the model.
tf.setBackend("cpu"); //! https://github.com/tensorflow/tfjs/issues/1644
nfsw = nsfwjs.load()
const container = document.getElementById('container');
// 设置图片加载函数
function loadImage(data) {
nfsw.then((model) => {
const img = new Image();
img.crossOrigin = "anonymous";
// some image here
img.src = data.imageURL;
// 不让img显示,可以将其设置为display:none
img.style.display = 'none';
img.onload = function () {
// Classify the image.
model.classify(img).then((predictions) => {
if ((predictions[0].className === "Hentai" && predictions[0].probability > 0.5) || (predictions[0].className === "Porn" && predictions[0].probability > 0.5)) {
console.log(data.imageURL);
console.log("Predictions", predictions);
img.remove();
delete img;
return;
}
img.remove();
delete img;
const image = document.createElement('div');
image.className = 'image';
const img2 = document.createElement('img');
img2.src = data.imageURL;
image.appendChild(img2);
container.appendChild(image);
});
}
});
}
// 设置请求函数
function requestData() {
const eventSource = new EventSource('https://image.pollinations.ai/feed');
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
// 加载图片
loadImage(data);
};
eventSource.onerror = () => {
console.log('请求错误');
};
eventSource.onopen = () => {
console.log('请求连接打开');
};
}
// 开始请求
requestData();
};
</script>
</body>
</html>