#define USE_OPENCV 1
#define CPU_ONLY 1
#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif // USE_OPENCV
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "head.h"
using namespace caffe;
using std::string;
class Classifier {
public:
Classifier(const string& model_file,const string& trained_file);
float Classify(const cv::Mat& img);
private:
std::vector<float> Predict(const cv::Mat& img);
void WrapInputLayer(std::vector<cv::Mat>* input_channels);
void Preprocess(const cv::Mat& img, std::vector<cv::Mat>* input_channels);
private:
shared_ptr<Net<float> > net_;
cv::Size input_geometry_;
int num_channels_;
cv::Mat mean_;
};
Classifier::Classifier(const string& model_file,const string& trained_file) {
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif
net_.reset(new Net<float>(model_file, TEST));
net_->CopyTrainedLayersFrom(trained_file);
CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";
Blob<float>* input_layer = net_->input_blobs()[0];
num_channels_ = input_layer->channels();
CHECK(num_channels_ == 3 || num_channels_ == 1)
<< "Input layer should have 1 or 3 channels.";
input_geometry_ = cv::Size(input_layer->width(), input_layer->height());
}
float Classifier::Classify(const cv::Mat& img) {
std::vector<float> output = Predict(img);
return output[1];
}
std::vector<float> Classifier::Predict(const cv::Mat& img) {
Blob<float>* input_layer = net_->input_blobs()[0];
input_layer->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
std::vector<cv::Mat> input_channels;
WrapInputLayer(&input_channels);
Preprocess(img, &input_channels);
net_->Forward();
Blob<float>* output_layer = net_->output_blobs()[0];
const float* begin = output_layer->cpu_data();
const float* end = begin + output_layer->channels();
return std::vector<float>(begin, end);
}
void Classifier::WrapInputLayer(std::vector<cv::Mat>* input_channels) {
Blob<float>* input_layer = net_->input_blobs()[0];
int width = input_layer->width();
int height = input_layer->height();
float* input_data = input_layer->mutable_cpu_data();
for (int i = 0; i < input_layer->channels(); ++i) {
cv::Mat channel(height, width, CV_32FC1, input_data);
input_channels->push_back(channel);
input_data += width * height;
}
}
void Classifier::Preprocess(const cv::Mat& img,
std::vector<cv::Mat>* input_channels) {
cv::Mat sample;
if (img.channels() == 3 && num_channels_ == 1)
cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY);
else if (img.channels() == 4 && num_channels_ == 1)
cv::cvtColor(img, sample, cv::COLOR_BGRA2GRAY);
else if (img.channels() == 4 && num_channels_ == 3)
cv::cvtColor(img, sample, cv::COLOR_BGRA2BGR);
else if (img.channels() == 1 && num_channels_ == 3)
cv::cvtColor(img, sample, cv::COLOR_GRAY2BGR);
else
sample = img;
cv::Mat sample_resized;
if (sample.size() != input_geometry_)
cv::resize(sample, sample_resized, input_geometry_);
else
sample_resized = sample;
cv::Mat sample_float;
if (num_channels_ == 3)
sample_resized.convertTo(sample_float, CV_32FC3);
else
sample_resized.convertTo(sample_float, CV_32FC1);
vector<cv::Mat>channels_mean(3);
channels_mean[0] = cv::Mat::ones(sample_float.rows, sample_float.cols, CV_32FC1) * 104;
channels_mean[1] = cv::Mat::ones(sample_float.rows, sample_float.cols, CV_32FC1) * 117;
channels_mean[2] = cv::Mat::ones(sample_float.rows, sample_float.cols, CV_32FC1) * 123;
cv::merge(channels_mean, mean_);
cv::Mat sample_normalized;
cv::subtract(sample_float, mean_, sample_normalized);
cv::split(sample_normalized, *input_channels);
CHECK(reinterpret_cast<float*>(input_channels->at(0).data)
== net_->input_blobs()[0]->cpu_data())
<< "Input channels are not wrapping the input layer of the network.";
}
int main(int argc, char** argv) {
::google::InitGoogleLogging(argv[0]);
string model_file = "deploy.prototxt";
string trained_file = "resnet_50_1by2_nsfw.caffemodel";
Classifier classifier(model_file, trained_file);
string file = "1.jpg";
std::cout << "---------- Prediction for "<< file << " ----------" << std::endl;
cv::Mat img = cv::imread(file, -1);
CHECK(!img.empty()) << "Unable to decode image " << file;
float prediction = classifier.Classify(img);
char str_head[50] = "NSFW Score:";
char str_pre[10];
sprintf_s(str_pre, "%.4f", prediction);
std::cout << "Scores < 0.2----->safe" << std::endl;
std::cout << "Scores > 0.8----->NSFW" << std::endl;
std::cout << "binned for different NSFW levels" << std::endl;
std::cout << "Score:" << prediction << std::endl;
cv::putText(img, std::strcat(str_head,str_pre), cv::Point(10, img.rows - 20), 3, 1, cv::Scalar(0, 0, 255));
cv::imshow("result", img);
cv::waitKey();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
open_nsfw.zip
共27个文件
dll:7个
lib:4个
jpg:3个
2星 需积分: 32 81 下载量 159 浏览量
2017-02-10
14:52:40
上传
评论 4
收藏 54.1MB ZIP 举报
温馨提示
基于c++和caffe实现了色情识别,使用了resnet50,效果很赞
资源推荐
资源详情
资源评论
收起资源包目录
open_nsfw.zip (27个子文件)
open_nsfw
x64
Debug
open_nsfw.ilk 26.13MB
open_nsfw.exe 6.37MB
open_nsfw.pdb 37.25MB
open_nsfw.v12.suo 28KB
open_nsfw.sdf 65.69MB
open_nsfw.sln 1KB
open_nsfw
opencv_imgproc2410d.dll 4.26MB
gflags_nothreadsd.lib 27KB
opencv_core2410d.dll 4.73MB
resnet_50_1by2_nsfw.caffemodel 22.71MB
open_nsfw.vcxproj.filters 1KB
3.jpg 13KB
head.h 1KB
gflags.lib 26KB
open_nsfw.vcxproj.user 165B
gflagsd.lib 26KB
2.jpg 16KB
gflagsd.dll 502KB
gflags_nothreads.dll 124KB
open_nsfw.cpp 5KB
gflags_nothreadsd.dll 502KB
open_nsfw.vcxproj 10KB
1.jpg 24KB
gflags_nothreads.lib 27KB
opencv_highgui2410d.dll 4.44MB
gflags.dll 124KB
deploy.prototxt 59KB
共 27 条
- 1
watersink
- 粉丝: 5164
- 资源: 87
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页