惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
B
Blog
U
Unit 42
有赞技术团队
有赞技术团队
博客园 - 聂微东
GbyAI
GbyAI
宝玉的分享
宝玉的分享
F
Full Disclosure
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
Jina AI
Jina AI
Martin Fowler
Martin Fowler
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
P
Proofpoint News Feed
A
About on SuperTechFans
I
InfoQ
博客园 - 【当耐特】
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
Project Zero
Project Zero
WordPress大学
WordPress大学
小众软件
小众软件
AWS News Blog
AWS News Blog
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
I
Intezer
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks
T
Tenable Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes

kekxv 技术日志

基于 kekxv/gitea-pages 与 Gitea Actions 构建静态站点托管服务 Json简单工具 在Windows上运行Code Server:结合WSL打造你的云端VS Code开发环境 安卓sdkmanager工具换源 boost bazel starter bazel 供应商模式 PVE引导丢失修复 警惕c++内置变量指针 关于内网springboot启动慢记录 网页转换为chrome插件 nginx代理的一种使用方式 YOLOv8 训练自己的数据 luckfox-交叉编译之bazel gitea actions CICD 自动化 Linux限制进程使用率 影音中心Jellyfin快速部署 OCR & 人脸算法 -- opencv dnn tensorflow gpu 安装(ubuntu22.04) 深度学习记录-简单
NSFW图像检测
kekxv · 2024-09-11 · via kekxv 技术日志

随着网络的发展,越来越多的场景被发掘,例如用户交互。用户交互除了使用文字之外,还能使用图片,这就涉及到图像的检测,毕竟不能把所有的图都接受并显示出来,
那和twitter有什么区别

原始的模型在github上:https://github.com/GantMan/nsfw_model ;模型类型分别为:MobilenetInception,有兴趣的可以查询相关信息。

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



#include <iostream>
#include <vector>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

class NsfwDnn {
public:
enum ModelType {
UNKNOWN,
TENSORFLOW,
ONNX,
};

const std::vector<string> labels{"drawings", "hentai", "neutral", "porn", "sexy"};

private:
cv::dnn::Net net;
int size = 224;
ModelType type = TENSORFLOW;
public:
explicit NsfwDnn() = default;

~NsfwDnn() = default;

bool loadModel(const std::string &model_path, ModelType type = UNKNOWN) {
this->type = type;
if (type == TENSORFLOW) {
size = 224;
net = cv::dnn::readNetFromTensorflow(model_path);
} else if (type == ONNX) {
size = 299;
net = cv::dnn::readNetFromONNX(model_path);
}
return !empty();
}

bool loadModel(const std::vector<unsigned char> &model_buff, const ModelType type) {
this->type = type;
if (type == TENSORFLOW) {
size = 224;
net = cv::dnn::readNetFromTensorflow(model_buff);
} else if (type == ONNX) {
size = 299;
net = cv::dnn::readNetFromONNX(model_buff);
}
return !empty();
}

bool empty() const { return net.empty(); }

std::vector<std::tuple<string, float> > detect(const std::vector<unsigned char> &bin) {
if (bin.empty())return {};
const auto image = imdecode(bin, cv::IMREAD_COLOR);
if (image.empty())return {};
return detect(image);
}

std::vector<std::tuple<string, float> > detect(const cv::Mat &frame) {
std::vector<std::tuple<string, float> > result;
if (empty())return result;

const int w = frame.cols;
const int h = frame.rows;
const int _max = std::max(h, w);
cv::Mat image = cv::Mat::zeros(cv::Size(_max, _max), CV_8UC3);
cv::Rect roi(0, 0, w, h);
frame.copyTo(image(roi));


cv::Mat blob;
cv::dnn::blobFromImage(image, blob, 1.0 / 255.0, cv::Size(size, size), cv::Scalar(), true, false);
if (size == 299) {
blob = blob.reshape(1, cv::dnn::MatShape({1, size, size, 3}));
}
net.setInput(blob);

std::vector<cv::Mat> outputs;
net.forward(outputs, net.getUnconnectedOutLayersNames());

const int rows = outputs[0].size[1];
const auto rest = reinterpret_cast<float *>(outputs[0].data);
result.reserve(rows);
for (int i = 0; i < rows; i++) {
result.emplace_back(labels[i], *(rest + i));
}
return result;
}
};

模型地址在 https://github.com/GantMan/nsfw_model/releases 进行下载;使用loadModel进行加载,调用detect进行分类。