Elffuss BraidHub

kikocis/ncnn public

master · 5.3 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#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
31template<class T>
32const 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
38struct Object
39{
40 cv::Rect_<float> rect;
41 int label;
42 float prob;
43};
44
45static 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 if (mobilenetv3.load_param("./mobilenetv3_ssdlite_voc.param"))
55 exit(-1);
56 if (mobilenetv3.load_model("./mobilenetv3_ssdlite_voc.bin"))
57 exit(-1);
58
59 const int target_size = 300;
60
61 int img_w = bgr.cols;
62 int img_h = bgr.rows;
63
64 ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR2RGB, bgr.cols, bgr.rows, target_size, target_size);
65
66 const float mean_vals[3] = {123.675f, 116.28f, 103.53f};
67 const float norm_vals[3] = {1.0f, 1.0f, 1.0f};
68 in.substract_mean_normalize(mean_vals, norm_vals);
69
70 ncnn::Extractor ex = mobilenetv3.create_extractor();
71
72 ex.input("input", in);
73
74 ncnn::Mat out;
75 ex.extract("detection_out", out);
76
77 // printf("%d %d %d\n", out.w, out.h, out.c);
78 objects.clear();
79 for (int i = 0; i < out.h; i++)
80 {
81 const float* values = out.row(i);
82
83 Object object;
84 object.label = values[0];
85 object.prob = values[1];
86
87 // filter out cross-boundary
88 float x1 = clamp(values[2] * target_size, 0.f, float(target_size - 1)) / target_size * img_w;
89 float y1 = clamp(values[3] * target_size, 0.f, float(target_size - 1)) / target_size * img_h;
90 float x2 = clamp(values[4] * target_size, 0.f, float(target_size - 1)) / target_size * img_w;
91 float y2 = clamp(values[5] * target_size, 0.f, float(target_size - 1)) / target_size * img_h;
92
93 object.rect.x = x1;
94 object.rect.y = y1;
95 object.rect.width = x2 - x1;
96 object.rect.height = y2 - y1;
97
98 objects.push_back(object);
99 }
100
101 return 0;
102}
103
104static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
105{
106 static const char* class_names[] = {"background",
107 "aeroplane", "bicycle", "bird", "boat",
108 "bottle", "bus", "car", "cat", "chair",
109 "cow", "diningtable", "dog", "horse",
110 "motorbike", "person", "pottedplant",
111 "sheep", "sofa", "train", "tvmonitor"
112 };
113
114 cv::Mat image = bgr.clone();
115
116 for (size_t i = 0; i < objects.size(); i++)
117 {
118 if (objects[i].prob > 0.6)
119 {
120 const Object& obj = objects[i];
121
122 fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
123 obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
124
125 cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
126
127 char text[256];
128 sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
129
130 int baseLine = 0;
131 cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
132
133 int x = obj.rect.x;
134 int y = obj.rect.y - label_size.height - baseLine;
135 if (y < 0)
136 y = 0;
137 if (x + label_size.width > image.cols)
138 x = image.cols - label_size.width;
139
140 cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
141 cv::Scalar(255, 255, 255), -1);
142
143 cv::putText(image, text, cv::Point(x, y + label_size.height),
144 cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
145 }
146 }
147
148 cv::imshow("image", image);
149 cv::waitKey(0);
150}
151
152int main(int argc, char** argv)
153{
154 if (argc != 2)
155 {
156 fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
157 return -1;
158 }
159
160 const char* imagepath = argv[1];
161
162 cv::Mat m = cv::imread(imagepath, 1);
163 if (m.empty())
164 {
165 fprintf(stderr, "cv::imread %s failed\n", imagepath);
166 return -1;
167 }
168
169 std::vector<Object> objects;
170 detect_mobilenetv3(m, objects);
171
172 draw_objects(m, objects);
173
174 return 0;
175}