1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
5
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6
// in compliance with the License. You may obtain a copy of the License at
8
// https://opensource.org/licenses/BSD-3-Clause
10
// Unless required by applicable law or agreed to in writing, software distributed
11
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13
// specific language governing permissions and limitations under the License.
17
#if defined(USE_NCNN_SIMPLEOCV)
20
#include <opencv2/core/core.hpp>
21
#include <opencv2/highgui/highgui.hpp>
22
#include <opencv2/imgproc/imgproc.hpp>
29
cv::Rect_<float> rect;
34
static int detect_mobilenet(const cv::Mat& bgr, std::vector<Object>& objects)
38
mobilenet.opt.use_vulkan_compute = true;
40
// model is converted from https://github.com/chuanqi305/MobileNet-SSD
41
// and can be downloaded from https://drive.google.com/open?id=0ByaKLD9QaPtucWk0Y0dha1VVY0U
42
// the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
43
if (mobilenet.load_param("mobilenet_ssd_voc_ncnn.param"))
45
if (mobilenet.load_model("mobilenet_ssd_voc_ncnn.bin"))
48
const int target_size = 300;
53
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
55
const float mean_vals[3] = {127.5f, 127.5f, 127.5f};
56
const float norm_vals[3] = {1.0 / 127.5, 1.0 / 127.5, 1.0 / 127.5};
57
in.substract_mean_normalize(mean_vals, norm_vals);
59
ncnn::Extractor ex = mobilenet.create_extractor();
64
ex.extract("detection_out", out);
66
// printf("%d %d %d\n", out.w, out.h, out.c);
68
for (int i = 0; i < out.h; i++)
70
const float* values = out.row(i);
73
object.label = values[0];
74
object.prob = values[1];
75
object.rect.x = values[2] * img_w;
76
object.rect.y = values[3] * img_h;
77
object.rect.width = values[4] * img_w - object.rect.x;
78
object.rect.height = values[5] * img_h - object.rect.y;
80
objects.push_back(object);
86
static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
88
static const char* class_names[] = {"background",
89
"aeroplane", "bicycle", "bird", "boat",
90
"bottle", "bus", "car", "cat", "chair",
91
"cow", "diningtable", "dog", "horse",
92
"motorbike", "person", "pottedplant",
93
"sheep", "sofa", "train", "tvmonitor"
96
cv::Mat image = bgr.clone();
98
for (size_t i = 0; i < objects.size(); i++)
100
const Object& obj = objects[i];
102
fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
103
obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
105
cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
108
sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
111
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
114
int y = obj.rect.y - label_size.height - baseLine;
117
if (x + label_size.width > image.cols)
118
x = image.cols - label_size.width;
120
cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
121
cv::Scalar(255, 255, 255), -1);
123
cv::putText(image, text, cv::Point(x, y + label_size.height),
124
cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
127
cv::imshow("image", image);
131
int main(int argc, char** argv)
135
fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
139
const char* imagepath = argv[1];
141
cv::Mat m = cv::imread(imagepath, 1);
144
fprintf(stderr, "cv::imread %s failed\n", imagepath);
148
std::vector<Object> objects;
149
detect_mobilenet(m, objects);
151
draw_objects(m, objects);