From ebf745ee114e1a2100e52a21531fc30a245cf514 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 14 Jan 2021 19:57:04 +0000 Subject: [PATCH] xfeatures2d: disable BoostDesc/VGG in case of missing data --- modules/xfeatures2d/CMakeLists.txt | 36 ++++++++++++++----- modules/xfeatures2d/src/boostdesc.cpp | 9 +++++ modules/xfeatures2d/src/vgg.cpp | 10 ++++++ modules/xfeatures2d/test/test_features2d.cpp | 5 ++- .../test_rotation_and_scale_invariance.cpp | 8 +++++ 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/modules/xfeatures2d/CMakeLists.txt b/modules/xfeatures2d/CMakeLists.txt index 3c86ebc141b..bbc540e278c 100644 --- a/modules/xfeatures2d/CMakeLists.txt +++ b/modules/xfeatures2d/CMakeLists.txt @@ -5,13 +5,33 @@ if(HAVE_CUDA) endif() ocv_define_module(xfeatures2d opencv_core opencv_imgproc opencv_features2d opencv_calib3d OPTIONAL opencv_shape opencv_ml opencv_cudaarithm WRAP python java) -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_vgg.cmake) -include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_boostdesc.cmake) -set(DOWNLOAD_DIR "${OpenCV_BINARY_DIR}/downloads/xfeatures2d") -download_boost_descriptors("${DOWNLOAD_DIR}" boost_status) -download_vgg_descriptors("${DOWNLOAD_DIR}" vgg_status) -if(NOT boost_status OR NOT vgg_status) - ocv_module_disable(xfeatures2d) +if(NOT OPENCV_SKIP_FEATURES2D_DOWNLOADING) + include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_vgg.cmake) + include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/download_boostdesc.cmake) + set(DOWNLOAD_DIR "${OpenCV_BINARY_DIR}/downloads/xfeatures2d") + download_boost_descriptors("${DOWNLOAD_DIR}" boost_status) + download_vgg_descriptors("${DOWNLOAD_DIR}" vgg_status) + if(boost_status) + ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/boostdesc.cpp "OPENCV_XFEATURES2D_HAS_BOOST_DATA=1") + else() + message(WARNING "features2d: Boost descriptor implementation is not available due to missing data (download failed: https://github.com/opencv/opencv_contrib/issues/1301)") + endif() + if(vgg_status) + ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/vgg.cpp "OPENCV_XFEATURES2D_HAS_VGG_DATA=1") + else() + message(WARNING "features2d: VGG descriptor implementation is not available due to missing data (download failed: https://github.com/opencv/opencv_contrib/issues/1301)") + endif() + + if(boost_status OR vgg_status) + ocv_module_include_directories("${DOWNLOAD_DIR}") + endif() endif() -ocv_module_include_directories("${DOWNLOAD_DIR}") +if(TARGET opencv_test_${name}) + if(boost_status) + ocv_target_compile_definitions(opencv_test_${name} PRIVATE "OPENCV_XFEATURES2D_HAS_BOOST_DATA=1") + endif() + if(vgg_status) + ocv_target_compile_definitions(opencv_test_${name} PRIVATE "OPENCV_XFEATURES2D_HAS_VGG_DATA=1") + endif() +endif() diff --git a/modules/xfeatures2d/src/boostdesc.cpp b/modules/xfeatures2d/src/boostdesc.cpp index 3f7ee225fc9..fe672a7324f 100644 --- a/modules/xfeatures2d/src/boostdesc.cpp +++ b/modules/xfeatures2d/src/boostdesc.cpp @@ -65,6 +65,8 @@ namespace cv namespace xfeatures2d { +#ifdef OPENCV_XFEATURES2D_HAS_BOOST_DATA + /* !BoostDesc implementation */ @@ -729,9 +731,16 @@ BoostDesc_Impl::~BoostDesc_Impl() { } +#endif // OPENCV_XFEATURES2D_HAS_BOOST_DATA + Ptr BoostDesc::create( int desc, bool use_scale_orientation, float scale_factor ) { +#ifdef OPENCV_XFEATURES2D_HAS_BOOST_DATA return makePtr( desc, use_scale_orientation, scale_factor ); +#else + CV_UNUSED(desc); CV_UNUSED(use_scale_orientation); CV_UNUSED(scale_factor); + CV_Error(Error::StsNotImplemented, "The OpenCV xfeatures2d binaries is built without downloaded Boost decriptor features: https://github.com/opencv/opencv_contrib/issues/1301"); +#endif } diff --git a/modules/xfeatures2d/src/vgg.cpp b/modules/xfeatures2d/src/vgg.cpp index a187f6b1d7a..506f5d0c13d 100644 --- a/modules/xfeatures2d/src/vgg.cpp +++ b/modules/xfeatures2d/src/vgg.cpp @@ -66,6 +66,8 @@ namespace cv namespace xfeatures2d { +#ifdef OPENCV_XFEATURES2D_HAS_VGG_DATA + /* !VGG implementation */ @@ -527,10 +529,18 @@ VGG_Impl::~VGG_Impl() { } +#endif + Ptr VGG::create( int desc, float isigma, bool img_normalize, bool use_scale_orientation, float scale_factor, bool dsc_normalize ) { +#ifdef OPENCV_XFEATURES2D_HAS_VGG_DATA return makePtr( desc, isigma, img_normalize, use_scale_orientation, scale_factor, dsc_normalize ); +#else + CV_UNUSED(desc); CV_UNUSED(isigma); CV_UNUSED(img_normalize); + CV_UNUSED(use_scale_orientation); CV_UNUSED(scale_factor); CV_UNUSED(dsc_normalize); + CV_Error(Error::StsNotImplemented, "The OpenCV xfeatures2d binaries is built without downloaded VGG decriptor features: https://github.com/opencv/opencv_contrib/issues/1301"); +#endif } diff --git a/modules/xfeatures2d/test/test_features2d.cpp b/modules/xfeatures2d/test/test_features2d.cpp index 2d6ec187757..90e7fe7d2ce 100644 --- a/modules/xfeatures2d/test/test_features2d.cpp +++ b/modules/xfeatures2d/test/test_features2d.cpp @@ -1114,13 +1114,16 @@ TEST( Features2d_DescriptorExtractor_LATCH, regression ) test.safe_run(); } +#ifdef OPENCV_XFEATURES2D_HAS_VGG_DATA TEST( Features2d_DescriptorExtractor_VGG, regression ) { CV_DescriptorExtractorTest > test( "descriptor-vgg", 0.03f, VGG::create() ); test.safe_run(); } +#endif // OPENCV_XFEATURES2D_HAS_VGG_DATA +#ifdef OPENCV_XFEATURES2D_HAS_BOOST_DATA TEST( Features2d_DescriptorExtractor_BGM, regression ) { CV_DescriptorExtractorTest test( "descriptor-boostdesc-bgm", @@ -1176,7 +1179,7 @@ TEST( Features2d_DescriptorExtractor_BINBOOST_256, regression ) BoostDesc::create(BoostDesc::BINBOOST_256) ); test.safe_run(); } - +#endif // OPENCV_XFEATURES2D_HAS_BOOST_DATA /*#if CV_SSE2 TEST( Features2d_DescriptorExtractor_Calonder_uchar, regression ) diff --git a/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp b/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp index 502c8ea44ed..f841d2cafa3 100644 --- a/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp +++ b/modules/xfeatures2d/test/test_rotation_and_scale_invariance.cpp @@ -641,6 +641,7 @@ TEST(DISABLED_Features2d_RotationInvariance_Descriptor_DAISY, regression) test.safe_run(); } +#ifdef OPENCV_XFEATURES2D_HAS_VGG_DATA TEST(Features2d_RotationInvariance_Descriptor_VGG120, regression) { DescriptorRotationInvarianceTest test(KAZE::create(), @@ -676,6 +677,7 @@ TEST(Features2d_RotationInvariance_Descriptor_VGG48, regression) 0.98f); test.safe_run(); } +#endif // OPENCV_XFEATURES2D_HAS_VGG_DATA #ifdef OPENCV_ENABLE_NONFREE TEST(Features2d_RotationInvariance_Descriptor_BRIEF_64, regression) @@ -715,6 +717,7 @@ TEST(Features2d_RotationInvariance_Descriptor_FREAK, regression) test.safe_run(); } +#ifdef OPENCV_XFEATURES2D_HAS_BOOST_DATA TEST(Features2d_RotationInvariance_Descriptor_BoostDesc_BGM, regression) { DescriptorRotationInvarianceTest test(SURF::create(), @@ -777,6 +780,7 @@ TEST(Features2d_RotationInvariance_Descriptor_BoostDesc_BINBOOST_256, regression 0.98f); test.safe_run(); } +#endif // OPENCV_XFEATURES2D_HAS_BOOST_DATA /* * Detector's scale invariance check @@ -840,6 +844,7 @@ TEST(DISABLED_Features2d_ScaleInvariance_Descriptor_DAISY, regression) test.safe_run(); } +#ifdef OPENCV_XFEATURES2D_HAS_VGG_DATA TEST(Features2d_ScaleInvariance_Descriptor_VGG120, regression) { DescriptorScaleInvarianceTest test(KAZE::create(), @@ -875,8 +880,10 @@ TEST(Features2d_ScaleInvariance_Descriptor_VGG48, regression) 0.93f); test.safe_run(); } +#endif // OPENCV_XFEATURES2D_HAS_VGG_DATA #ifdef OPENCV_ENABLE_NONFREE +#ifdef OPENCV_XFEATURES2D_HAS_BOOST_DATA TEST(Features2d_ScaleInvariance_Descriptor_BoostDesc_BGM, regression) { DescriptorScaleInvarianceTest test(SURF::create(), @@ -939,6 +946,7 @@ TEST(Features2d_ScaleInvariance_Descriptor_BoostDesc_BINBOOST_256, regression) 0.98f); test.safe_run(); } +#endif // OPENCV_XFEATURES2D_HAS_BOOST_DATA #endif // NONFREE }} // namespace