1 // Tencent is pleased to support the open source community by making ncnn available.
2 //
3 // Copyright (C) 2017 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 
17 #if defined(USE_NCNN_SIMPLEOCV)
18 #include "simpleocv.h"
19 #else
20 #include <opencv2/core/core.hpp>
21 #include <opencv2/highgui/highgui.hpp>
22 #include <opencv2/imgproc/imgproc.hpp>
23 #endif
24 #include <stdio.h>
25 #include <vector>
26 
27 struct Object
28 {
29     cv::Rect_<float> rect;
30     int label;
31     float prob;
32 };
33 
detect_squeezenet(const cv::Mat & bgr,std::vector<Object> & objects)34 static int detect_squeezenet(const cv::Mat& bgr, std::vector<Object>& objects)
35 {
36     ncnn::Net squeezenet;
37 
38     squeezenet.opt.use_vulkan_compute = true;
39 
40     // original pretrained model from https://github.com/chuanqi305/SqueezeNet-SSD
41     // squeezenet_ssd_voc_deploy.prototxt
42     // https://drive.google.com/open?id=0B3gersZ2cHIxdGpyZlZnbEQ5Snc
43     // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
44     squeezenet.load_param("squeezenet_ssd_voc.param");
45     squeezenet.load_model("squeezenet_ssd_voc.bin");
46 
47     const int target_size = 300;
48 
49     int img_w = bgr.cols;
50     int img_h = bgr.rows;
51 
52     ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
53 
54     const float mean_vals[3] = {104.f, 117.f, 123.f};
55     in.substract_mean_normalize(mean_vals, 0);
56 
57     ncnn::Extractor ex = squeezenet.create_extractor();
58 
59     ex.input("data", in);
60 
61     ncnn::Mat out;
62     ex.extract("detection_out", out);
63 
64     //     printf("%d %d %d\n", out.w, out.h, out.c);
65     objects.clear();
66     for (int i = 0; i < out.h; i++)
67     {
68         const float* values = out.row(i);
69 
70         Object object;
71         object.label = values[0];
72         object.prob = values[1];
73         object.rect.x = values[2] * img_w;
74         object.rect.y = values[3] * img_h;
75         object.rect.width = values[4] * img_w - object.rect.x;
76         object.rect.height = values[5] * img_h - object.rect.y;
77 
78         objects.push_back(object);
79     }
80 
81     return 0;
82 }
83 
draw_objects(const cv::Mat & bgr,const std::vector<Object> & objects)84 static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
85 {
86     static const char* class_names[] = {"background",
87                                         "aeroplane", "bicycle", "bird", "boat",
88                                         "bottle", "bus", "car", "cat", "chair",
89                                         "cow", "diningtable", "dog", "horse",
90                                         "motorbike", "person", "pottedplant",
91                                         "sheep", "sofa", "train", "tvmonitor"
92                                        };
93 
94     cv::Mat image = bgr.clone();
95 
96     for (size_t i = 0; i < objects.size(); i++)
97     {
98         const Object& obj = objects[i];
99 
100         fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
101                 obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
102 
103         cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
104 
105         char text[256];
106         sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
107 
108         int baseLine = 0;
109         cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
110 
111         int x = obj.rect.x;
112         int y = obj.rect.y - label_size.height - baseLine;
113         if (y < 0)
114             y = 0;
115         if (x + label_size.width > image.cols)
116             x = image.cols - label_size.width;
117 
118         cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
119                       cv::Scalar(255, 255, 255), -1);
120 
121         cv::putText(image, text, cv::Point(x, y + label_size.height),
122                     cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
123     }
124 
125     cv::imshow("image", image);
126     cv::waitKey(0);
127 }
128 
main(int argc,char ** argv)129 int main(int argc, char** argv)
130 {
131     if (argc != 2)
132     {
133         fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
134         return -1;
135     }
136 
137     const char* imagepath = argv[1];
138 
139     cv::Mat m = cv::imread(imagepath, 1);
140     if (m.empty())
141     {
142         fprintf(stderr, "cv::imread %s failed\n", imagepath);
143         return -1;
144     }
145 
146     std::vector<Object> objects;
147     detect_squeezenet(m, objects);
148 
149     draw_objects(m, objects);
150 
151     return 0;
152 }
153