Elffuss BraidHub

kikocis/ncnn public

master · 4.9 KiBRawPermalink
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
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
27class Noop : public ncnn::Layer
28{
29};
30DEFINE_LAYER_CREATOR(Noop)
31
32struct Object
33{
34 cv::Rect_<float> rect;
35 int label;
36 float prob;
37};
38
39static int detect_mobilenetv2(const cv::Mat& bgr, std::vector<Object>& objects)
40{
41 ncnn::Net mobilenetv2;
42
43 mobilenetv2.opt.use_vulkan_compute = true;
44
45 mobilenetv2.register_custom_layer("Silence", Noop_layer_creator);
46
47 // original pretrained model from https://github.com/chuanqi305/MobileNetv2-SSDLite
48 // https://github.com/chuanqi305/MobileNetv2-SSDLite/blob/master/ssdlite/voc/deploy.prototxt
49 // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
50 if (mobilenetv2.load_param("mobilenetv2_ssdlite_voc.param"))
51 exit(-1);
52 if (mobilenetv2.load_model("mobilenetv2_ssdlite_voc.bin"))
53 exit(-1);
54
55 const int target_size = 300;
56
57 int img_w = bgr.cols;
58 int img_h = bgr.rows;
59
60 ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
61
62 const float mean_vals[3] = {127.5f, 127.5f, 127.5f};
63 const float norm_vals[3] = {1.0 / 127.5, 1.0 / 127.5, 1.0 / 127.5};
64 in.substract_mean_normalize(mean_vals, norm_vals);
65
66 ncnn::Extractor ex = mobilenetv2.create_extractor();
67
68 ex.input("data", in);
69
70 ncnn::Mat out;
71 ex.extract("detection_out", out);
72
73 // printf("%d %d %d\n", out.w, out.h, out.c);
74 objects.clear();
75 for (int i = 0; i < out.h; i++)
76 {
77 const float* values = out.row(i);
78
79 Object object;
80 object.label = values[0];
81 object.prob = values[1];
82 object.rect.x = values[2] * img_w;
83 object.rect.y = values[3] * img_h;
84 object.rect.width = values[4] * img_w - object.rect.x;
85 object.rect.height = values[5] * img_h - object.rect.y;
86
87 objects.push_back(object);
88 }
89
90 return 0;
91}
92
93static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
94{
95 static const char* class_names[] = {"background",
96 "aeroplane", "bicycle", "bird", "boat",
97 "bottle", "bus", "car", "cat", "chair",
98 "cow", "diningtable", "dog", "horse",
99 "motorbike", "person", "pottedplant",
100 "sheep", "sofa", "train", "tvmonitor"
101 };
102
103 cv::Mat image = bgr.clone();
104
105 for (size_t i = 0; i < objects.size(); i++)
106 {
107 const Object& obj = objects[i];
108
109 fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
110 obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
111
112 cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
113
114 char text[256];
115 sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
116
117 int baseLine = 0;
118 cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
119
120 int x = obj.rect.x;
121 int y = obj.rect.y - label_size.height - baseLine;
122 if (y < 0)
123 y = 0;
124 if (x + label_size.width > image.cols)
125 x = image.cols - label_size.width;
126
127 cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
128 cv::Scalar(255, 255, 255), -1);
129
130 cv::putText(image, text, cv::Point(x, y + label_size.height),
131 cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
132 }
133
134 cv::imshow("image", image);
135 cv::waitKey(0);
136}
137
138int main(int argc, char** argv)
139{
140 if (argc != 2)
141 {
142 fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
143 return -1;
144 }
145
146 const char* imagepath = argv[1];
147
148 cv::Mat m = cv::imread(imagepath, 1);
149 if (m.empty())
150 {
151 fprintf(stderr, "cv::imread %s failed\n", imagepath);
152 return -1;
153 }
154
155 std::vector<Object> objects;
156 detect_mobilenetv2(m, objects);
157
158 draw_objects(m, objects);
159
160 return 0;
161}