自建nsfwjs-api图片健康鉴别接口
开发文档
API 方法
POST /classify
POST /classify-many
POST /classify
请求示例
POST /classify HTTP/1.1 Content-Type: multipart/form-data
图片应在
image
字段中提供。响应示例
HTTP/1.1 200 OK Content-Type: application/json
{ "porn": 0.59248286485672, "sexy": 0.39802199602127075, "hentai": 0.006243097595870495, "neutral": 0.0031403270550072193, "drawing": 0.00011181648733327165 }
POST /classify-many
请求示例
POST /classify-many HTTP/1.1 Content-Type: multipart/form-data
图片应在
images
字段中提供。响应示例
HTTP/1.1 200 OK Content-Type: application/json
[ { "porn": 0.3996206820011139, "neutral": 0.388679563999176, "sexy": 0.19470958411693573, "hentai": 0.015063910745084286, "drawing": 0.001926235854625702 }, { "sexy": 0.8366416692733765, "porn": 0.13645528256893158, "neutral": 0.0222245492041111, "hentai": 0.004213324282318354, "drawing": 0.0004651622730307281 }, { "sexy": 0.8017168045043945, "porn": 0.1770564466714859, "neutral": 0.015829339623451233, "hentai": 0.005097625777125359, "drawing": 0.00029983260901644826 } ]
php+html对接示例
<?php
/*
* Author:云猫
* Blog:lwcat.cn
* Claude 3.5美化
*/
define('UPLOAD_DIR', __DIR__ . '/uploads/');
define('API_URL', 'https://apicheckimg.czzu.cn/classify');
define('MAX_FILE_SIZE', 10 * 1024 * 1024); // 限制图片大小为10MB以内
function handleError($message, $type = 'error') {
return [
'status' => false,
'type' => $type,
'message' => $message
];
}
function processImageClassification($file) {
if ($file['size'] > MAX_FILE_SIZE) {
return handleError('文件大小不能超过5MB');
}
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($file['type'], $allowedTypes)) {
return handleError('只支持 JPG、PNG 和 GIF 格式的图片');
}
if (!is_dir(UPLOAD_DIR)) {
if (!mkdir(UPLOAD_DIR, 0755, true)) {
return handleError('无法创建上传目录');
}
}
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = uniqid('img_') . '.' . $extension;
$targetPath = UPLOAD_DIR . $filename;
if (!move_uploaded_file($file['tmp_name'], $targetPath)) {
return handleError('文件上传失败');
}
try {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => API_URL,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POSTFIELDS => ['image' => new CURLFile($targetPath)]
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
$result = json_decode($response, true);
if (!$result) {
throw new Exception('API返回数据解析失败');
}
$sexyScore = isset($result['sexy']) ? floatval($result['sexy']) : 0;
$status = $sexyScore <= 0.1;
return [
'status' => true,
'type' => $status ? 'success' : 'warning',
'message' => $status ? '图片内容健康' : '图片内容不适宜',
'details' => $result,
'score' => $sexyScore
];
} catch (Exception $e) {
return handleError('处理失败:' . $e->getMessage());
} finally {
curl_close($ch);
@unlink($targetPath);
}
}
$result = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['image'])) {
$result = processImageClassification($_FILES['image']);
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>图片内容检测系统</title>
<link rel="stylesheet" href="https://cdn.lwcat.cn/layui/css/layui.css">
<style>
.container { max-width: 800px; margin: 40px auto; padding: 20px; }
.upload-area {
border: 2px dashed #e2e2e2;
padding: 20px;
text-align: center;
margin: 20px 0;
border-radius: 4px;
}
.upload-area:hover { border-color: #009688; }
.result-card {
margin-top: 20px;
padding: 20px;
border-radius: 4px;
display: none;
}
.details-box {
background: #f8f8f8;
padding: 15px;
border-radius: 4px;
margin-top: 10px;
}
.score-label {
font-size: 24px;
margin: 10px 0;
}
#uploadForm { margin-bottom: 20px; }
.preview-image {
max-width: 300px;
max-height: 300px;
margin: 10px auto;
display: none;
}
</style>
</head>
<body>
<div class="layui-container container">
<div class="layui-card">
<div class="layui-card-header">
<h2>图片内容检测系统</h2>
</div>
<div class="layui-card-body">
<form id="uploadForm" action="" method="post" enctype="multipart/form-data">
<div class="upload-area" id="dropZone">
<input type="hidden" name="MAX_FILE_SIZE" value="<?= MAX_FILE_SIZE ?>">
<input type="file" name="image" id="imageInput" accept="image/*" style="display: none;">
<img id="preview" class="preview-image">
<div class="layui-text">
<p>点击或拖拽图片至此处上传</p>
<p class="layui-text-muted">支持 JPG、PNG、GIF 格式,最大 5MB</p>
</div>
</div>
<button class="layui-btn layui-btn-fluid" type="submit">开始检测</button>
</form>
<?php if ($result): ?>
<div class="result-card" style="display: block;">
<div class="layui-bg-<?= $result['type'] ?>" style="padding: 15px;">
<h3><?= htmlspecialchars($result['message']) ?></h3>
<?php if ($result['status'] && isset($result['score'])): ?>
<div class="score-label">
评分:<?= number_format($result['score'] * 100, 1) ?>%
</div>
<?php endif; ?>
</div>
<?php if ($result['status'] && isset($result['details'])): ?>
<div class="details-box">
<pre><?= htmlspecialchars(json_encode($result['details'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)) ?></pre>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
<script src="https://cdn.lwcat.cn/layui/layui.js"></script>
<script>
layui.use(['upload', 'layer'], function(){
const dropZone = document.getElementById('dropZone');
const imageInput = document.getElementById('imageInput');
const preview = document.getElementById('preview');
dropZone.onclick = () => imageInput.click();
dropZone.ondragover = (e) => {
e.preventDefault();
dropZone.classList.add('layui-border-green');
};
dropZone.ondragleave = () => {
dropZone.classList.remove('layui-border-green');
};
dropZone.ondrop = (e) => {
e.preventDefault();
dropZone.classList.remove('layui-border-green');
if (e.dataTransfer.files.length) {
imageInput.files = e.dataTransfer.files;
updatePreview();
}
};
imageInput.onchange = updatePreview;
function updatePreview() {
const file = imageInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
preview.src = e.target.result;
preview.style.display = 'block';
};
reader.readAsDataURL(file);
}
}
});
</script>
</body>
</html>
评论 (0)