diff --git a/examples/ssd/CMakeLists.txt b/examples/ssd/CMakeLists.txt index 2766d06a5..c91bd4771 100755 --- a/examples/ssd/CMakeLists.txt +++ b/examples/ssd/CMakeLists.txt @@ -7,6 +7,8 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/../../src) include_directories(${CMAKE_CURRENT_BINARY_DIR}) -add_executable(ssdmobilenet ssdmobilenet.cpp ) - +add_executable(ssdmobilenet ssdmobilenet.cpp) target_link_libraries(ssdmobilenet ncnn ${OpenCV_LIBS}) + +add_executable(ssdsqueezenet ssdsqueezenet.cpp) +target_link_libraries(ssdsqueezenet ncnn ${OpenCV_LIBS}) diff --git a/examples/ssd/ssdmobilenet.cpp b/examples/ssd/ssdmobilenet.cpp index fb3be107a..ee28b7b79 100755 --- a/examples/ssd/ssdmobilenet.cpp +++ b/examples/ssd/ssdmobilenet.cpp @@ -103,7 +103,7 @@ static int detect_mobilenet(cv::Mat& raw_img, float show_threshold) int main(int argc, char** argv) { - const char* imagepath = "test.jpg"; + const char* imagepath = argv[1]; cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR); if (m.empty()) diff --git a/examples/ssd/ssdsqueezenet.cpp b/examples/ssd/ssdsqueezenet.cpp new file mode 100755 index 000000000..482ddcea9 --- /dev/null +++ b/examples/ssd/ssdsqueezenet.cpp @@ -0,0 +1,141 @@ +// 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 "net.h" + +struct Object +{ + cv::Rect_ rect; + int label; + float prob; +}; + +static int detect_squeezenet(const cv::Mat& bgr, std::vector& objects) +{ + ncnn::Net squeezenet; + + // original pretrained model from https://github.com/chuanqi305/SqueezeNet-SSD + // squeezenet_ssd_voc_deploy.prototxt + // https://drive.google.com/open?id=0B3gersZ2cHIxdGpyZlZnbEQ5Snc + squeezenet.load_param("squeezenet_ssd_voc.param"); + squeezenet.load_model("squeezenet_ssd_voc.bin"); + + const int target_size = 300; + + 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] = {104.f, 117.f, 123.f}; + in.substract_mean_normalize(mean_vals, 0); + + ncnn::Extractor ex = squeezenet.create_extractor(); + ex.set_light_mode(true); + 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) +{ + static const char* class_names[] = {"background", + "aeroplane", "bicycle", "bird", "boat", + "bottle", "bus", "car", "cat", "chair", + "cow", "diningtable", "dog", "horse", + "motorbike", "person", "pottedplant", + "sheep", "sofa", "train", "tvmonitor"}; + + cv::Mat image = bgr.clone(); + + 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), CV_FILLED); + + cv::putText(image, text, cv::Point(x, y + label_size.height), + cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0)); + } + + cv::imshow("image", image); + cv::waitKey(0); +} + +int main(int argc, char** argv) +{ + const char* imagepath = argv[1]; + + cv::Mat m = cv::imread(imagepath, CV_LOAD_IMAGE_COLOR); + if (m.empty()) + { + fprintf(stderr, "cv::imread %s failed\n", imagepath); + return -1; + } + + std::vector objects; + detect_squeezenet(m, objects); + + draw_objects(m, objects); + + return 0; +}