// Tencent is pleased to support the open source community by making ncnn available. // // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. // // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except // in compliance with the License. You may obtain a copy of the License at // // https://opensource.org/licenses/BSD-3-Clause // // Unless required by applicable law or agreed to in writing, software distributed // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. #include #include #include #include #include #include "platform.h" #include "net.h" #if NCNN_VULKAN #include "gpu.h" #endif // NCNN_VULKAN struct Object { cv::Rect_ rect; int label; float prob; }; static int detect_peleenet(const cv::Mat& bgr, std::vector& objects,ncnn::Mat &resized) { ncnn::Net peleenet; #if NCNN_VULKAN peleenet.opt.use_vulkan_compute = true; #endif // NCNN_VULKAN // model is converted from https://github.com/eric612/MobileNet-YOLO // and can be downloaded from https://drive.google.com/open?id=1Wt6jKv13sBRMHgrGAJYlOlRF-o80pC0g peleenet.load_param("pelee.param"); peleenet.load_model("pelee.bin"); const int target_size = 304; int img_w = bgr.cols; int img_h = bgr.rows; ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, target_size, target_size); const float mean_vals[3] = {103.9f, 116.7f, 123.6f}; const float norm_vals[3] = {0.017f,0.017f,0.017f}; in.substract_mean_normalize(mean_vals, norm_vals); ncnn::Extractor ex = peleenet.create_extractor(); // ex.set_num_threads(4); ex.input("data", in); ncnn::Mat out; ex.extract("detection_out",out); // printf("%d %d %d\n", out.w, out.h, out.c); objects.clear(); for (int i=0; i& objects,ncnn::Mat map) { static const char* class_names[] = {"background", "person","rider", "car","bus", "truck","bike","motor", "traffic light","traffic sign","train"}; cv::Mat image = bgr.clone(); const int color[] = {128,255,128,244,35,232}; const int color_count = sizeof(color) / sizeof(int); for (size_t i = 0; i < objects.size(); i++) { const Object& obj = objects[i]; fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob, obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height); cv::rectangle(image, obj.rect, cv::Scalar(255, 0, 0)); char text[256]; sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); int x = obj.rect.x; int y = obj.rect.y - label_size.height - baseLine; if (y < 0) y = 0; if (x + label_size.width > image.cols) x = image.cols - label_size.width; cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)), cv::Scalar(255, 255, 255), -1); cv::putText(image, text, cv::Point(x, y + label_size.height), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0)); } int width = map.w; int height = map.h; int size = map.c; int img_index2 = 0; float threshold = 0.45; const float* ptr2 = map; for (int i = 0; i < height; i++) { unsigned char* ptr1 = image.ptr(i); int img_index1 = 0; for (int j = 0; j < width; j++) { float maxima = threshold; int index = -1; for (int c = 0; c < size; c++) { //const float* ptr3 = map.channel(c); const float* ptr3 = ptr2 + c*width*height; if(ptr3[img_index2]>maxima) { maxima = ptr3[img_index2]; index = c; } } if(index > -1) { int color_index = (index)*3; if(color_index objects; ncnn::Mat seg_out; detect_peleenet(m, objects, seg_out); #if NCNN_VULKAN ncnn::destroy_gpu_instance(); #endif // NCNN_VULKAN draw_objects(m, objects, seg_out); return 0; }