diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8cf10e8a9..eeacf2f11 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,56 +1,68 @@ macro(ncnn_add_example name) add_executable(${name} ${name}.cpp) - target_include_directories(${name} PRIVATE ${OpenCV_INCLUDE_DIRS}) - target_link_libraries(${name} PRIVATE ncnn ${OpenCV_LIBS}) + if(OpenCV_FOUND) + target_include_directories(${name} PRIVATE ${OpenCV_INCLUDE_DIRS}) + target_link_libraries(${name} PRIVATE ncnn ${OpenCV_LIBS}) + elseif(NCNN_SIMPLEOCV) + target_compile_definitions(${name} PUBLIC USE_NCNN_SIMPLEOCV) + target_link_libraries(${name} PRIVATE ncnn) + endif() # add test to a virtual project group set_property(TARGET ${name} PROPERTY FOLDER "examples") endmacro() -find_package(OpenCV QUIET COMPONENTS opencv_world) -# for opencv 2.4 on ubuntu 16.04, there is no opencv_world but OpenCV_FOUND will be TRUE -if("${OpenCV_LIBS}" STREQUAL "") - set(OpenCV_FOUND FALSE) -endif() -if(NOT OpenCV_FOUND) - find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs videoio) -endif() -if(NOT OpenCV_FOUND) - find_package(OpenCV QUIET COMPONENTS core highgui imgproc) -endif() +if(NCNN_PIXEL) + find_package(OpenCV QUIET COMPONENTS opencv_world) + # for opencv 2.4 on ubuntu 16.04, there is no opencv_world but OpenCV_FOUND will be TRUE + if("${OpenCV_LIBS}" STREQUAL "") + set(OpenCV_FOUND FALSE) + endif() + if(NOT OpenCV_FOUND) + find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs videoio) + endif() + if(NOT OpenCV_FOUND) + find_package(OpenCV QUIET COMPONENTS core highgui imgproc) + endif() -if(NOT OpenCV_FOUND) - message(WARNING "OpenCV not found, examples won't be built") -elseif(NOT NCNN_PIXEL) - message(WARNING "NCNN_PIXEL not enabled, examples won't be built") -else() - message(STATUS "OpenCV library: ${OpenCV_INSTALL_PATH}") - message(STATUS " version: ${OpenCV_VERSION}") - message(STATUS " libraries: ${OpenCV_LIBS}") - message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") + if(OpenCV_FOUND OR NCNN_SIMPLEOCV) + if(OpenCV_FOUND) + message(STATUS "OpenCV library: ${OpenCV_INSTALL_PATH}") + message(STATUS " version: ${OpenCV_VERSION}") + message(STATUS " libraries: ${OpenCV_LIBS}") + message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") - if(${OpenCV_VERSION_MAJOR} GREATER 3) - set(CMAKE_CXX_STANDARD 11) - endif() - include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) - include_directories(${CMAKE_CURRENT_BINARY_DIR}/../src) + if(${OpenCV_VERSION_MAJOR} GREATER 3) + set(CMAKE_CXX_STANDARD 11) + endif() + endif() + + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) + include_directories(${CMAKE_CURRENT_BINARY_DIR}/../src) - ncnn_add_example(squeezenet) - ncnn_add_example(squeezenet_c_api) - ncnn_add_example(fasterrcnn) - ncnn_add_example(rfcn) - ncnn_add_example(yolov2) - ncnn_add_example(yolov3) - ncnn_add_example(yolov4) - ncnn_add_example(yolov5) - ncnn_add_example(mobilenetv2ssdlite) - ncnn_add_example(mobilenetssd) - ncnn_add_example(squeezenetssd) - ncnn_add_example(shufflenetv2) - ncnn_add_example(peleenetssd_seg) - ncnn_add_example(simplepose) - ncnn_add_example(retinaface) - ncnn_add_example(yolact) - ncnn_add_example(nanodet) - ncnn_add_example(scrfd) + ncnn_add_example(squeezenet) + ncnn_add_example(squeezenet_c_api) + ncnn_add_example(fasterrcnn) + ncnn_add_example(rfcn) + ncnn_add_example(yolov2) + ncnn_add_example(yolov3) + if(OpenCV_FOUND) + ncnn_add_example(yolov4) + endif() + ncnn_add_example(yolov5) + ncnn_add_example(mobilenetv2ssdlite) + ncnn_add_example(mobilenetssd) + ncnn_add_example(squeezenetssd) + ncnn_add_example(shufflenetv2) + ncnn_add_example(peleenetssd_seg) + ncnn_add_example(simplepose) + ncnn_add_example(retinaface) + ncnn_add_example(yolact) + ncnn_add_example(nanodet) + ncnn_add_example(scrfd) + else() + message(WARNING "OpenCV not found and NCNN_SIMPLEOCV disabled, examples won't be built") + endif() +else() + message(WARNING "NCNN_PIXEL not enabled, examples won't be built") endif() diff --git a/examples/fasterrcnn.cpp b/examples/fasterrcnn.cpp index 64208490d..733acf18a 100644 --- a/examples/fasterrcnn.cpp +++ b/examples/fasterrcnn.cpp @@ -15,9 +15,13 @@ #include "net.h" #include +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include struct Object diff --git a/examples/mobilenetssd.cpp b/examples/mobilenetssd.cpp index e4f32928a..7095ccc6e 100644 --- a/examples/mobilenetssd.cpp +++ b/examples/mobilenetssd.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/mobilenetv2ssdlite.cpp b/examples/mobilenetv2ssdlite.cpp index 6ed74e509..c1e998f9c 100644 --- a/examples/mobilenetv2ssdlite.cpp +++ b/examples/mobilenetv2ssdlite.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/mobilenetv3ssdlite.cpp b/examples/mobilenetv3ssdlite.cpp index b737f825d..9c97ffc18 100644 --- a/examples/mobilenetv3ssdlite.cpp +++ b/examples/mobilenetv3ssdlite.cpp @@ -15,9 +15,13 @@ #include "net.h" #include "platform.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include #if NCNN_VULKAN diff --git a/examples/nanodet.cpp b/examples/nanodet.cpp index 2129271f9..8da7c4abd 100644 --- a/examples/nanodet.cpp +++ b/examples/nanodet.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include #include diff --git a/examples/peleenetssd_seg.cpp b/examples/peleenetssd_seg.cpp index 5b7e90f44..5ee393f6b 100644 --- a/examples/peleenetssd_seg.cpp +++ b/examples/peleenetssd_seg.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/retinaface.cpp b/examples/retinaface.cpp index 21defbff8..342d5661d 100644 --- a/examples/retinaface.cpp +++ b/examples/retinaface.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/rfcn.cpp b/examples/rfcn.cpp index 6e982761c..71fe74d67 100644 --- a/examples/rfcn.cpp +++ b/examples/rfcn.cpp @@ -15,9 +15,13 @@ #include "net.h" #include +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include struct Object diff --git a/examples/scrfd.cpp b/examples/scrfd.cpp index 47e3e4e2a..5dc188cbe 100644 --- a/examples/scrfd.cpp +++ b/examples/scrfd.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/shufflenetv2.cpp b/examples/shufflenetv2.cpp index b7c7858df..5dd31abcf 100644 --- a/examples/shufflenetv2.cpp +++ b/examples/shufflenetv2.cpp @@ -15,8 +15,12 @@ #include "net.h" #include +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include +#endif #include #include diff --git a/examples/simplepose.cpp b/examples/simplepose.cpp index 8323292f7..0589f017d 100644 --- a/examples/simplepose.cpp +++ b/examples/simplepose.cpp @@ -15,9 +15,13 @@ #include "net.h" #include +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/squeezenet.cpp b/examples/squeezenet.cpp index 30db592a4..ec86d71e6 100644 --- a/examples/squeezenet.cpp +++ b/examples/squeezenet.cpp @@ -15,8 +15,12 @@ #include "net.h" #include +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include +#endif #include #include diff --git a/examples/squeezenet_c_api.cpp b/examples/squeezenet_c_api.cpp index 132bb1008..01add23d1 100644 --- a/examples/squeezenet_c_api.cpp +++ b/examples/squeezenet_c_api.cpp @@ -15,8 +15,12 @@ #include "c_api.h" #include +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include +#endif #include #include diff --git a/examples/squeezenetssd.cpp b/examples/squeezenetssd.cpp index 113969bf9..64d461bf3 100644 --- a/examples/squeezenetssd.cpp +++ b/examples/squeezenetssd.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/yolact.cpp b/examples/yolact.cpp index 26d6a4f22..87cfeb8f7 100644 --- a/examples/yolact.cpp +++ b/examples/yolact.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/yolov2.cpp b/examples/yolov2.cpp index c89482104..d9488f756 100644 --- a/examples/yolov2.cpp +++ b/examples/yolov2.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/yolov3.cpp b/examples/yolov3.cpp index e2f57af5f..fd581376b 100644 --- a/examples/yolov3.cpp +++ b/examples/yolov3.cpp @@ -14,9 +14,13 @@ #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif #include #include diff --git a/examples/yolov5.cpp b/examples/yolov5.cpp index c54b84008..3990fa950 100644 --- a/examples/yolov5.cpp +++ b/examples/yolov5.cpp @@ -15,9 +15,14 @@ #include "layer.h" #include "net.h" +#if defined(USE_NCNN_SIMPLEOCV) +#include "simpleocv.h" +#else #include #include #include +#endif +#include #include #include diff --git a/src/simpleocv.cpp b/src/simpleocv.cpp index 2ded7d397..f1270633d 100644 --- a/src/simpleocv.cpp +++ b/src/simpleocv.cpp @@ -211,6 +211,19 @@ bool imwrite(const std::string& path, const Mat& m, const std::vector& para return success; } +void imshow(const std::string& name, const Mat& m) +{ + NCNN_LOGE("imshow save image to %s.png", name.c_str()); + + imwrite(name + ".png", m); +} + +int waitKey(int delay) +{ + NCNN_LOGE("waitKey stub"); + return -1; +} + #if NCNN_PIXEL void resize(const Mat& src, Mat& dst, const Size& size, float sw, float sh, int flags) { diff --git a/src/simpleocv.h b/src/simpleocv.h index e01500120..19b8a7268 100644 --- a/src/simpleocv.h +++ b/src/simpleocv.h @@ -124,6 +124,11 @@ struct Point_ { } + template operator Point_<_Tp2>() const + { + return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y)); + } + _Tp x; _Tp y; }; @@ -143,6 +148,11 @@ struct Size_ { } + template operator Size_<_Tp2>() const + { + return Size_<_Tp2>(saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); + } + _Tp width; _Tp height; }; @@ -166,6 +176,11 @@ struct Rect_ { } + template operator Rect_<_Tp2>() const + { + return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); + } + _Tp x; _Tp y; _Tp width; @@ -437,6 +452,10 @@ enum ImwriteFlags NCNN_EXPORT bool imwrite(const std::string& path, const Mat& m, const std::vector& params = std::vector()); +NCNN_EXPORT void imshow(const std::string& name, const Mat& m); + +NCNN_EXPORT int waitKey(int delay = 0); + #if NCNN_PIXEL NCNN_EXPORT void resize(const Mat& src, Mat& dst, const Size& size, float sw = 0.f, float sh = 0.f, int flags = 0); #endif // NCNN_PIXEL diff --git a/toolchains/c906.toolchain.cmake b/toolchains/c906.toolchain.cmake index fa726b6fb..6002fd783 100644 --- a/toolchains/c906.toolchain.cmake +++ b/toolchains/c906.toolchain.cmake @@ -15,9 +15,12 @@ set(CMAKE_CXX_COMPILER "${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-g++") set(CMAKE_FIND_ROOT_PATH "${RISCV_ROOT_PATH}/riscv64-unknown-linux-gnu") +set(CMAKE_SYSROOT "${RISCV_ROOT_PATH}/sysroot") + set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) set(CMAKE_C_FLAGS "-march=rv64gcvxtheadc -mabi=lp64d -mtune=c906 -DRVV_SPEC_0_7 -D__riscv_zfh=1 -static") set(CMAKE_CXX_FLAGS "-march=rv64gcvxtheadc -mabi=lp64d -mtune=c906 -DRVV_SPEC_0_7 -D__riscv_zfh=1 -static") @@ -26,5 +29,5 @@ set(CMAKE_CXX_FLAGS "-march=rv64gcvxtheadc -mabi=lp64d -mtune=c906 -DRVV_SPEC_0_ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "c flags") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags") -# export RISCV_ROOT_PATH=/home/nihui/osd/riscv64-linux-x86_64-20210329 -# cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/c906.toolchain.cmake -DCMAKE_BUILD_TYPE=relwithdebinfo -DNCNN_BUILD_TESTS=ON -DNCNN_OPENMP=OFF -DNCNN_THREADS=OFF -DNCNN_RUNTIME_CPU=OFF -DNCNN_RVV=ON .. +# export RISCV_ROOT_PATH=/home/nihui/osd/riscv64-linux-x86_64-20210512 +# cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/c906.toolchain.cmake -DCMAKE_BUILD_TYPE=relwithdebinfo -DNCNN_BUILD_TESTS=ON -DNCNN_OPENMP=OFF -DNCNN_THREADS=OFF -DNCNN_RUNTIME_CPU=OFF -DNCNN_RVV=ON -DNCNN_SIMPLEOCV=ON -DNCNN_BUILD_EXAMPLES=ON ..