Elffuss BraidHub

kikocis/ncnn public

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