Elffuss BraidHub

kikocis/ncnn public

master · 4.7 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_mobilenet(const cv::Mat& bgr, std::vector<Object>& objects)
35{
36 ncnn::Net mobilenet;
37
38 mobilenet.opt.use_vulkan_compute = true;
39
40 // model is converted from https://github.com/chuanqi305/MobileNet-SSD
41 // and can be downloaded from https://drive.google.com/open?id=0ByaKLD9QaPtucWk0Y0dha1VVY0U
42 // the ncnn model https://github.com/nihui/ncnn-assets/tree/master/models
43 if (mobilenet.load_param("mobilenet_ssd_voc_ncnn.param"))
44 exit(-1);
45 if (mobilenet.load_model("mobilenet_ssd_voc_ncnn.bin"))
46 exit(-1);
47
48 const int target_size = 300;
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] = {127.5f, 127.5f, 127.5f};
56 const float norm_vals[3] = {1.0 / 127.5, 1.0 / 127.5, 1.0 / 127.5};
57 in.substract_mean_normalize(mean_vals, norm_vals);
58
59 ncnn::Extractor ex = mobilenet.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
83 return 0;
84}
85
86static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects)
87{
88 static const char* class_names[] = {"background",
89 "aeroplane", "bicycle", "bird", "boat",
90 "bottle", "bus", "car", "cat", "chair",
91 "cow", "diningtable", "dog", "horse",
92 "motorbike", "person", "pottedplant",
93 "sheep", "sofa", "train", "tvmonitor"
94 };
95
96 cv::Mat image = bgr.clone();
97
98 for (size_t i = 0; i < objects.size(); i++)
99 {
100 const Object& obj = objects[i];
101
102 fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
103 obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
104
105 cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0));
106
107 char text[256];
108 sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100);
109
110 int baseLine = 0;
111 cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
112
113 int x = obj.rect.x;
114 int y = obj.rect.y - label_size.height - baseLine;
115 if (y < 0)
116 y = 0;
117 if (x + label_size.width > image.cols)
118 x = image.cols - label_size.width;
119
120 cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
121 cv::Scalar(255, 255, 255), -1);
122
123 cv::putText(image, text, cv::Point(x, y + label_size.height),
124 cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0));
125 }
126
127 cv::imshow("image", image);
128 cv::waitKey(0);
129}
130
131int main(int argc, char** argv)
132{
133 if (argc != 2)
134 {
135 fprintf(stderr, "Usage: %s [imagepath]\n", argv[0]);
136 return -1;
137 }
138
139 const char* imagepath = argv[1];
140
141 cv::Mat m = cv::imread(imagepath, 1);
142 if (m.empty())
143 {
144 fprintf(stderr, "cv::imread %s failed\n", imagepath);
145 return -1;
146 }
147
148 std::vector<Object> objects;
149 detect_mobilenet(m, objects);
150
151 draw_objects(m, objects);
152
153 return 0;
154}