18
#if defined(USE_NCNN_SIMPLEOCV)
21
#include <opencv2/core/core.hpp>
22
#include <opencv2/highgui/highgui.hpp>
27
static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores)
31
squeezenet.opt.use_vulkan_compute = true;
34
if (squeezenet.load_param("squeezenet_v1.1.param"))
36
if (squeezenet.load_model("squeezenet_v1.1.bin"))
39
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, 227, 227);
41
const float mean_vals[3] = {104.f, 117.f, 123.f};
42
in.substract_mean_normalize(mean_vals, 0);
44
ncnn::Extractor ex = squeezenet.create_extractor();
49
ex.extract("prob", out);
51
cls_scores.resize(out.w);
52
for (int j = 0; j < out.w; j++)
54
cls_scores[j] = out[j];
60
static int print_topk(const std::vector<float>& cls_scores, int topk)
63
int size = cls_scores.size();
64
std::vector<std::pair<float, int> > vec;
66
for (int i = 0; i < size; i++)
68
vec[i] = std::make_pair(cls_scores[i], i);
71
std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
72
std::greater<std::pair<float, int> >());
75
for (int i = 0; i < topk; i++)
77
float score = vec[i].first;
78
int index = vec[i].second;
79
fprintf(stderr, "%d = %f\n", index, score);
85
int main(int argc, char** argv)
89
fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
93
const char* imagepath = argv[1];
95
cv::Mat m = cv::imread(imagepath, 1);
98
fprintf(stderr, "cv::imread %s failed\n", imagepath);
102
std::vector<float> cls_scores;
103
detect_squeezenet(m, cls_scores);
105
print_topk(cls_scores, 3);