1 // Tencent is pleased to support the open source community by making ncnn available.
2 //
3 // Copyright (C) 2018 THL A29 Limited, a Tencent company. All rights reserved.
4 //
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
7 //
8 // https://opensource.org/licenses/BSD-3-Clause
9 //
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.
14 
15 #include "net.h"
16 #include "platform.h"
17 
18 #if defined(USE_NCNN_SIMPLEOCV)
19 #include "simpleocv.h"
20 #else
21 #include <opencv2/core/core.hpp>
22 #include <opencv2/highgui/highgui.hpp>
23 #include <opencv2/imgproc/imgproc.hpp>
24 #endif
25 #include <stdio.h>
26 #include <vector>
27 #if NCNN_VULKAN
28 #include "gpu.h"
29 #endif // NCNN_VULKAN
30 
31 template<class T>
clamp(const T & v,const T & lo,const T & hi)32 const T& clamp(const T& v, const T& lo, const T& hi)
33 {
34     assert(!(hi < lo));
35     return v < lo ? lo : hi < v ? hi : v;
36 }
37 
38 struct Object
39 {
40     cv::Rect_<float> rect;
41     int label;
42     float prob;
43 };
44 
detect_mobilenetv3(const cv::Mat & bgr,std::vector<Object> & objects)45 static int detect_mobilenetv3(const cv::Mat& bgr, std::vector<Object>& objects)
46 {
47     ncnn::Net mobilenetv3;
48 
49 #if NCNN_VULKAN
50     mobilenetv3.opt.use_vulkan_compute = true;
51 #endif // NCNN_VULKAN
52 
53     // converted ncnn model from https://github.com/ujsyehao/mobilenetv3-ssd
54     mobilenetv3.load_param("./mobilenetv3_ssdlite_voc.param");
55     mobilenetv3.load_model("./mobilenetv3_ssdlite_voc.bin");
56 
57     const int target_size = 300;
58 
59     int img_w = bgr.cols;
60     int img_h = bgr.rows;
61 
62     ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_size, target_size);
63 
64     const float mean_vals[3] = {123.675f, 116.28f, 103.53f};
65     const float norm_vals[3] = {1.0f, 1.0f, 1.0f};
66     in.substract_mean_normalize(mean_vals, norm_vals);
67 
68     ncnn::Extractor ex = mobilenetv3.create_extractor();
69 
70     ex.input("input", in);
71 
72     ncnn::Mat out;
73     ex.extract("detection_out", out);
74 
75     //     printf("%d %d %d\n", out.w, out.h, out.c);
76     objects.clear();
77     for (int i = 0; i < out.h; i++)
78     {
79         const float* values = out.row(i);
80 
81         Object object;
82         object.label = values[0];
83         object.prob = values[1];
84 
85         // filter out cross-boundary
86         float x1 = clamp(values[2] * target_size, 0.f, float(target_size - 1)) / target_size * img_w;
87         float y1 = clamp(values[3] * target_size, 0.f, float(target_size - 1)) / target_size * img_h;
88         float x2 = clamp(values[4] * target_size, 0.f, float(target_size - 1)) / target_size * img_w;
89         float y2 = clamp(values[5] * target_size, 0.f, float(target_size - 1)) / target_size * img_h;
90 
91         object.rect.x = x1;
92         object.rect.y = y1;
93         object.rect.width = x2 - x1;
94         object.rect.height = y2 - y1;
95 
96         objects.push_back(object);
97     }
98 
99     return 0;
100 }
101 
draw_objects(const cv::Mat & bgr,const std::vector<Object> & objects)102 static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
103 {
104     static const char* class_names[] = {"background",
105                                         "aeroplane", "bicycle", "bird", "boat",
106                                         "bottle", "bus", "car", "cat", "chair",
107                                         "cow", "diningtable", "dog", "horse",
108                                         "motorbike", "person", "pottedplant",
109                                         "sheep", "sofa", "train", "tvmonitor"
110                                        };
111 
112     cv::Mat image = bgr.clone();
113 
114     for (size_t i = 0; i < objects.size(); i++)
115     {
116         if (objects[i].prob > 0.6)
117         {
118             const Object& obj = objects[i];
119 
120             fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
121                     obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
122 
123             cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
124 
125             char text[256];
126             sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
127 
128             int baseLine = 0;
129             cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
130 
131             int x = obj.rect.x;
132             int y = obj.rect.y - label_size.height - baseLine;
133             if (y < 0)
134                 y = 0;
135             if (x + label_size.width > image.cols)
136                 x = image.cols - label_size.width;
137 
138             cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
139                           cv::Scalar(255, 255, 255), -1);
140 
141             cv::putText(image, text, cv::Point(x, y + label_size.height),
142                         cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
143         }
144     }
145 
146     cv::imshow("image", image);
147     cv::waitKey(0);
148 }
149 
main(int argc,char ** argv)150 int main(int argc, char** argv)
151 {
152     if (argc != 2)
153     {
154         fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
155         return -1;
156     }
157 
158     const char* imagepath = argv[1];
159 
160     cv::Mat m = cv::imread(imagepath, 1);
161     if (m.empty())
162     {
163         fprintf(stderr, "cv::imread %s failed\n", imagepath);
164         return -1;
165     }
166 
167     std::vector<Object> objects;
168     detect_mobilenetv3(m, objects);
169 
170     draw_objects(m, objects);
171 
172     return 0;
173 }
174