diff --git a/modules/objdetect/include/opencv2/objdetect/mcc_checker_detector.hpp b/modules/objdetect/include/opencv2/objdetect/mcc_checker_detector.hpp index 39fa6b8d48..267b872601 100644 --- a/modules/objdetect/include/opencv2/objdetect/mcc_checker_detector.hpp +++ b/modules/objdetect/include/opencv2/objdetect/mcc_checker_detector.hpp @@ -29,7 +29,9 @@ #ifndef OPENCV_OBJDETECT_MCC_CHECKER_DETECTOR_HPP #define OPENCV_OBJDETECT_MCC_CHECKER_DETECTOR_HPP #include +#ifdef HAVE_OPENCV_DNN #include +#endif #include //--------------------------------------------------------------- @@ -241,7 +243,9 @@ public: * */ CV_WRAP static Ptr create(); - /** @brief Set the net which will be used to find the approximate + +#ifdef HAVE_OPENCV_DNN + /** @brief Set the net which will be used to find the approximate * bounding boxes for the color charts. And returns the implementation of the CCheckerDetector. * * It is not necessary to use this, but this usually results in @@ -251,6 +255,7 @@ public: * the function will return false. */ CV_WRAP static Ptr create(const dnn::Net &net); +#endif /** @brief Draws the checker to the given image. * @param img image in color space BGR @@ -280,6 +285,7 @@ public: CV_WRAP virtual void setColorChartType(ColorChart chartType) = 0; +#ifdef HAVE_OPENCV_DNN /** @brief Enables or disables the use of the neural network for detection. * @param useDnn Boolean flag to indicate whether to use neural network (true) or not (false). */ @@ -287,6 +293,7 @@ public: CV_WRAP virtual void setUseDnnModel(bool useDnn) = 0; CV_WRAP virtual bool getUseDnnModel() const = 0; +#endif CV_WRAP virtual const DetectorParametersMCC& getDetectionParams() const = 0; diff --git a/modules/objdetect/misc/python/pyopencv_cchecker.hpp b/modules/objdetect/misc/python/pyopencv_cchecker.hpp deleted file mode 100644 index 39c2b8bd00..0000000000 --- a/modules/objdetect/misc/python/pyopencv_cchecker.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "opencv2/objdetect/mcc_checker_detector.hpp" - -typedef std::vector> vector_Ptr_CChecker; -typedef dnn::Net dnn_Net; diff --git a/modules/objdetect/misc/python/pyopencv_objdetect.hpp b/modules/objdetect/misc/python/pyopencv_objdetect.hpp index e441b8adc9..57c624de86 100644 --- a/modules/objdetect/misc/python/pyopencv_objdetect.hpp +++ b/modules/objdetect/misc/python/pyopencv_objdetect.hpp @@ -3,5 +3,9 @@ #include "opencv2/objdetect.hpp" typedef QRCodeEncoder::Params QRCodeEncoder_Params; +typedef std::vector> vector_Ptr_CChecker; +#ifdef HAVE_OPENCV_DNN +typedef dnn::Net dnn_Net; +#endif #endif diff --git a/modules/objdetect/src/mcc/checker_detector.cpp b/modules/objdetect/src/mcc/checker_detector.cpp index caf65d3e56..71f4c91178 100644 --- a/modules/objdetect/src/mcc/checker_detector.cpp +++ b/modules/objdetect/src/mcc/checker_detector.cpp @@ -44,10 +44,13 @@ Ptr CCheckerDetector::create() { return makePtr(); } + +#ifdef HAVE_OPENCV_DNN Ptr CCheckerDetector::create(const dnn::Net& net) { return makePtr(net); } +#endif CCheckerDetectorImpl:: CCheckerDetectorImpl() @@ -251,6 +254,7 @@ bool CCheckerDetectorImpl:: { m_checkers.clear(); +#ifdef HAVE_OPENCV_DNN if (this->net.empty() || !m_useDnn) { return _no_net_process(image, nc, regionsOfInterest); @@ -471,6 +475,9 @@ bool CCheckerDetectorImpl:: m_checkers.resize(min(nc, (int)m_checkers.size())); return !m_checkers.empty(); +#else + return _no_net_process(image, nc, regionsOfInterest); +#endif } @@ -491,7 +498,7 @@ void CCheckerDetectorImpl::setColorChartType(ColorChart chartType) { this->m_chartType = chartType; } - +#ifdef HAVE_OPENCV_DNN void CCheckerDetectorImpl::setUseDnnModel(bool useDnn) { this->m_useDnn = useDnn; @@ -501,7 +508,7 @@ bool CCheckerDetectorImpl::getUseDnnModel() const { return m_useDnn; } - +#endif const DetectorParametersMCC& CCheckerDetectorImpl::getDetectionParams() const { return m_params; @@ -1428,8 +1435,6 @@ void CCheckerDetectorImpl:: { // color chart classic model CChartModel cccm(m_chartType); - Mat lab; - size_t N; std::vector fbox = cccm.box; std::vector cellchart = cccm.cellchart; @@ -1439,7 +1444,7 @@ void CCheckerDetectorImpl:: Mat mask(im_rgb.size(), CV_8U); mask.setTo(Scalar::all(0)); std::vector bch(4), bcht(4); - N = cellchart.size() / 4; + size_t N = cellchart.size() / 4; // Create table charts information // |p_size|average|stddev|max|min| diff --git a/modules/objdetect/src/mcc/checker_detector.hpp b/modules/objdetect/src/mcc/checker_detector.hpp index a50f73ef6d..7153a9433e 100644 --- a/modules/objdetect/src/mcc/checker_detector.hpp +++ b/modules/objdetect/src/mcc/checker_detector.hpp @@ -45,9 +45,11 @@ class CCheckerDetectorImpl : public CCheckerDetector public: CCheckerDetectorImpl(); +#ifdef HAVE_OPENCV_DNN CCheckerDetectorImpl(const dnn::Net& _net){ net = _net; } +#endif virtual ~CCheckerDetectorImpl(); bool process(InputArray image, const std::vector ®ionsOfInterest, @@ -72,9 +74,11 @@ public: virtual void setColorChartType(ColorChart chartType) CV_OVERRIDE; +#ifdef HAVE_OPENCV_DNN virtual void setUseDnnModel(bool useDnn) CV_OVERRIDE; virtual bool getUseDnnModel() const CV_OVERRIDE; +#endif virtual const DetectorParametersMCC& getDetectionParams() const CV_OVERRIDE; @@ -164,10 +168,14 @@ protected: // methods pipeline protected: std::vector> m_checkers; +#ifdef HAVE_OPENCV_DNN dnn::Net net; + bool m_useDnn = true; +#else + bool m_useDnn = false; +#endif DetectorParametersMCC m_params = DetectorParametersMCC(); ColorChart m_chartType; - bool m_useDnn = true; private: // methods aux void get_subbox_chart_physical( diff --git a/modules/objdetect/src/mcc/precomp.hpp b/modules/objdetect/src/mcc/precomp.hpp index a470c502a1..f1671d306f 100644 --- a/modules/objdetect/src/mcc/precomp.hpp +++ b/modules/objdetect/src/mcc/precomp.hpp @@ -34,7 +34,9 @@ #include #include #include +#ifdef HAVE_OPENCV_DNN #include +#endif #include #include diff --git a/samples/cpp/macbeth_chart_detection.cpp b/samples/cpp/macbeth_chart_detection.cpp index f253d29751..a351a1d869 100644 --- a/samples/cpp/macbeth_chart_detection.cpp +++ b/samples/cpp/macbeth_chart_detection.cpp @@ -1,32 +1,48 @@ #include #include #include +#ifdef HAVE_OPENCV_DNN #include -#include #include "../dnn/common.hpp" +#endif +#include using namespace std; using namespace cv; +#ifdef HAVE_OPENCV_DNN using namespace cv::dnn; +#endif using namespace mcc; const string about = "This sample demonstrates mcc checker detection with DNN based model and thresholding (default) techniques.\n\n" "To run default:\n" "\t ./example_cpp_macbeth_chart_detection --input=path/to/your/input/image/or/video (don't give --input flag if want to use device camera)\n" +#ifdef HAVE_OPENCV_DNN "With DNN model:\n" "\t ./example_cpp_macbeth_chart_detection mcc --input=path/to/your/input/image/or/video\n\n" - "Model path can also be specified using --model argument. And config path can be specified using --config. Download it using python download_models.py mcc from dnn samples directory\n\n"; + "Model path can also be specified using --model argument. And config path can be specified using --config. Download it using python download_models.py mcc from dnn samples directory\n\n" +#else + "Note: DNN-based detection is not available in this build.\n\n" +#endif + ; const string param_keys = "{ help h | | Print help message. }" +#ifdef HAVE_OPENCV_DNN "{ @alias | | An alias name of model to extract preprocessing parameters from models.yml file. }" "{ zoo | ../dnn/models.yml | An optional path to file with preprocessing parameters }" +#endif "{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera.}" "{ type | 0 | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl, default:0 }" "{ num_charts | 1 | Maximum number of charts in the image }" +#ifdef HAVE_OPENCV_DNN "{ model | | Path to the model file for using dnn model. }"; +#else + ; +#endif +#ifdef HAVE_OPENCV_DNN const string backend_keys = format( "{ backend | default | Choose one of computation backends: " "default: automatically (by default), " @@ -45,8 +61,15 @@ const string target_keys = format( "vulkan: Vulkan, " "cuda: CUDA, " "cuda_fp16: CUDA fp16 (half-float preprocess) }"); +#endif -string keys = param_keys + backend_keys + target_keys; +// Initialize keys before use +string keys = param_keys; +static void initKeys() { +#ifdef HAVE_OPENCV_DNN + keys += backend_keys + target_keys; +#endif +} static bool processFrame(const Mat& frame, Ptr detector, Mat& src, Mat& tgt, int nc){ Mat imageCopy = frame.clone(); @@ -67,6 +90,7 @@ static bool processFrame(const Mat& frame, Ptr detector, Mat& int main(int argc, char *argv[]) { + initKeys(); CommandLineParser parser(argc, argv, keys); parser.about(about); @@ -76,6 +100,8 @@ int main(int argc, char *argv[]) parser.printMessage(); return -1; } + +#ifdef HAVE_OPENCV_DNN string modelName = parser.get("@alias"); string zooFile = parser.get("zoo"); const char* path = getenv("OPENCV_SAMPLES_DATA_PATH"); @@ -88,22 +114,26 @@ int main(int argc, char *argv[]) keys += genPreprocArguments(modelName, zooFile); parser = CommandLineParser(argc, argv, keys); +#endif int t = parser.get("type"); CV_Assert(0 <= t && t <= 2); ColorChart chartType = ColorChart(t); +#ifdef HAVE_OPENCV_DNN const string sha1 = parser.get("sha1"); const string model_path = findModel(parser.get("model"), sha1); const string config_sha1 = parser.get("config_sha1"); const string pbtxt_path = findModel(parser.get("config"), config_sha1); const string backend = parser.get("backend"); const string target = parser.get("target"); +#endif int nc = parser.get("num_charts"); Ptr detector; +#ifdef HAVE_OPENCV_DNN if (model_path != "" && pbtxt_path != ""){ EngineType engine = ENGINE_AUTO; if (backend != "default" || target != "cpu"){ @@ -119,6 +149,9 @@ int main(int argc, char *argv[]) else{ detector = CCheckerDetector::create(); } +#else + detector = CCheckerDetector::create(); +#endif detector->setColorChartType(chartType); bool isVideo = true;