Elffuss BraidHub

kikocis/ncnn public

master · 6.2 KiBRawPermalink
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
27struct Object
28{
29 cv::Rect_<float> rect;
30 int label;
31 float prob;
32};
33
34static int detect_peleenet(const cv::Mat& bgr, std::vector<Object>& objects, ncnn::Mat& resized)
35{
36 ncnn::Net peleenet;
37
38 peleenet.opt.use_vulkan_compute = true;
39
40 // model is converted from https://github.com/eric612/MobileNet-YOLO
41 // and can be downloaded from https://drive.google.com/open?id=1Wt6jKv13sBRMHgrGAJYlOlRF-o80pC0g
42 // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
43 if (peleenet.load_param("pelee.param"))
44 exit(-1);
45 if (peleenet.load_model("pelee.bin"))
46 exit(-1);
47
48 const int target_size = 304;
49
50 int img_w = bgr.cols;
51 int img_h = bgr.rows;
52
53 ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size);
54
55 const float mean_vals[3] = {103.9f, 116.7f, 123.6f};
56 const float norm_vals[3] = {0.017f, 0.017f, 0.017f};
57 in.substract_mean_normalize(mean_vals, norm_vals);
58
59 ncnn::Extractor ex = peleenet.create_extractor();
60
61 ex.input("data", in);
62
63 ncnn::Mat out;
64 ex.extract("detection_out", out);
65
66 // printf("%d %d %d\n", out.w, out.h, out.c);
67 objects.clear();
68 for (int i = 0; i < out.h; i++)
69 {
70 const float* values = out.row(i);
71
72 Object object;
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;
79
80 objects.push_back(object);
81 }
82 ncnn::Mat seg_out;
83 ex.extract("sigmoid", seg_out);
84 resize_bilinear(seg_out, resized, img_w, img_h);
85 //resize_bicubic(seg_out,resized,img_w,img_h); // sharpness
86 return 0;
87}
88
89static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects, ncnn::Mat map)
90{
91 static const char* class_names[] = {"background",
92 "person", "rider", "car", "bus",
93 "truck", "bike", "motor",
94 "traffic light", "traffic sign", "train"
95 };
96
97 cv::Mat image = bgr.clone();
98 const int color[] = {128, 255, 128, 244, 35, 232};
99 const int color_count = sizeof(color) / sizeof(int);
100
101 for (size_t i = 0; i < objects.size(); i++)
102 {
103 const Object& obj = objects[i];
104
105 fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
106 obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
107
108 cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
109
110 char text[256];
111 sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
112
113 int baseLine = 0;
114 cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
115
116 int x = obj.rect.x;
117 int y = obj.rect.y - label_size.height - baseLine;
118 if (y < 0)
119 y = 0;
120 if (x + label_size.width > image.cols)
121 x = image.cols - label_size.width;
122
123 cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
124 cv::Scalar(255, 255, 255), -1);
125
126 cv::putText(image, text, cv::Point(x, y + label_size.height),
127 cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
128 }
129 int width = map.w;
130 int height = map.h;
131 int size = map.c;
132 int img_index2 = 0;
133 float threshold = 0.45;
134 const float* ptr2 = map;
135 for (int i = 0; i < height; i++)
136 {
137 unsigned char* ptr1 = image.ptr<unsigned char>(i);
138 int img_index1 = 0;
139 for (int j = 0; j < width; j++)
140 {
141 float maxima = threshold;
142 int index = -1;
143 for (int c = 0; c < size; c++)
144 {
145 //const float* ptr3 = map.channel(c);
146 const float* ptr3 = ptr2 + c * width * height;
147 if (ptr3[img_index2] > maxima)
148 {
149 maxima = ptr3[img_index2];
150 index = c;
151 }
152 }
153 if (index > -1)
154 {
155 int color_index = (index)*3;
156 if (color_index < color_count)
157 {
158 int b = color[color_index];
159 int g = color[color_index + 1];
160 int r = color[color_index + 2];
161 ptr1[img_index1] = b / 2 + ptr1[img_index1] / 2;
162 ptr1[img_index1 + 1] = g / 2 + ptr1[img_index1 + 1] / 2;
163 ptr1[img_index1 + 2] = r / 2 + ptr1[img_index1 + 2] / 2;
164 }
165 }
166 img_index1 += 3;
167 img_index2++;
168 }
169 }
170 cv::imshow("image", image);
171 cv::waitKey(0);
172}
173
174int main(int argc, char** argv)
175{
176 if (argc != 2)
177 {
178 fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
179 return -1;
180 }
181
182 const char* imagepath = argv[1];
183
184 cv::Mat m = cv::imread(imagepath, 1);
185 if (m.empty())
186 {
187 fprintf(stderr, "cv::imread %s failed\n", imagepath);
188 return -1;
189 }
190
191 std::vector<Object> objects;
192 ncnn::Mat seg_out;
193 detect_peleenet(m, objects, seg_out);
194
195 draw_objects(m, objects, seg_out);
196
197 return 0;
198}