头像识别情头扫一扫 (识别头像是不是情头)

长沙耍耍 06-27 阅读:18 评论:0

输入头像链接或上传头像文件,即可识别该头像是否是情头。

头像识别情头扫一扫 (识别头像是不是情头)

javascript // 获取元素 const form = document.getElementById('form'); const result = document.getElementById('result');// 添加提交事件监听器 form.addEventListener('submit', async (event) => {event.preventDefault();// 获取头像链接或文件const url = document.getElementById('url').value;const file = document.getElementById('file').files[0];// 判断是链接还是文件if (url) {// 根据链接识别头像const response = await fetch(`/api/avatar?url=${url}`);const data = await response.json();displayResult(data);} else if (file) {// 根据文件识别头像const formData = new FormData();formData.append('avatar', file);const response = await fetch(`/api/avatar`, {method: 'POST',body: formData,});const data = await response.json();displayResult(data);} else {alert('请输入头像链接或上传头像文件。');} });// 显示识别结果 const displayResult = (data) => {let html = ''; if (data.isCouple) {html += `

该头像是一对情头的其中一个。

`;} else {html += `

该头像不是情头。

`;}if (data.couples) {html += '
    ';for (const couple of data.couples) {html += ` `;}html += '
';}result.innerHTML = html; }; bash安装依赖 npm install express multer创建服务器 const express = require('express'); const multer = require('multer'); const app = express();创建上传中间件 const upload = multer();路由 app.get('/api/avatar', async (req, res) => {const { url } = req.query;const response = await fetch(url);const buffer = await response.buffer();const isCouple = await isCoupleAvatar(buffer);const couples = [];if (isCouple) {const otherUrl = url.replace('left', 'right').replace('right', 'left');const otherResponse = await fetch(otherUrl);const otherBuffer = await otherResponse.buffer();couples.push(url, otherUrl);}res.json({ isCouple, couples }); });app.post('/api/avatar', upload.single('avatar'), async (req, res) => {const isCouple = await isCoupleAvatar(req.file.buffer);const couples = [];if (isCouple) {// ...}res.json({ isCouple, couples }); });启动服务器 app.listen(3000);
版权声明

本文仅代表作者观点,不代表长沙桑拿立场。
本文系作者授权发表,未经许可,不得转载。