diff --git a/Makefile b/Makefile index cd6610581893..331c0b46c510 100644 --- a/Makefile +++ b/Makefile @@ -129,7 +129,7 @@ ifdef CAFFE_PATH endif ifndef LINT_LANG - LINT_LANG="all" + LINT_LANG = "all" endif ifeq ($(USE_MKLDNN), 1) @@ -146,11 +146,33 @@ endif # setup opencv ifeq ($(USE_OPENCV), 1) - CFLAGS += -DMXNET_USE_OPENCV=1 $(shell pkg-config --cflags opencv) - LDFLAGS += $(filter-out -lopencv_ts, $(shell pkg-config --libs opencv)) + CFLAGS += -DMXNET_USE_OPENCV=1 + ifneq ($(USE_OPENCV_INC_PATH), NONE) + CFLAGS += -I$(USE_OPENCV_INC_PATH)/include + ifeq ($(USE_OPENCV_LIB_PATH), NONE) +$(error Please add the path of OpenCV shared library path into `USE_OPENCV_LIB_PATH`, when `USE_OPENCV_INC_PATH` is not NONE) + endif + LDFLAGS += -L$(USE_OPENCV_LIB_PATH) + ifneq ($(wildcard $(USE_OPENCV_LIB_PATH)/libopencv_imgcodecs.*),) + LDFLAGS += -lopencv_imgcodecs + endif + ifneq ($(wildcard $(USE_OPENCV_LIB_PATH)/libopencv_highgui.*),) + LDFLAGS += -lopencv_highgui + endif + else + ifeq ("$(shell pkg-config --exists opencv4; echo $$?)", "0") + OPENCV_LIB = opencv4 + else + OPENCV_LIB = opencv + endif + CFLAGS += $(shell pkg-config --cflags $(OPENCV_LIB)) + LDFLAGS += $(shell pkg-config --libs-only-L $(OPENCV_LIB)) + LDFLAGS += $(filter -lopencv_imgcodecs -lopencv_highgui, $(shell pkg-config --libs-only-l $(OPENCV_LIB))) + endif + LDFLAGS += -lopencv_imgproc -lopencv_core BIN += bin/im2rec else - CFLAGS+= -DMXNET_USE_OPENCV=0 + CFLAGS += -DMXNET_USE_OPENCV=0 endif ifeq ($(USE_OPENMP), 1) diff --git a/make/config.mk b/make/config.mk index 8a1aa2c165c4..f9ac4cf10048 100644 --- a/make/config.mk +++ b/make/config.mk @@ -89,6 +89,10 @@ USE_NCCL_PATH = NONE # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE #whether use libjpeg-turbo for image decode without OpenCV wrapper USE_LIBJPEG_TURBO = 0 diff --git a/make/crosscompile.jetson.mk b/make/crosscompile.jetson.mk index 171f846d20dd..3db2b98847f3 100644 --- a/make/crosscompile.jetson.mk +++ b/make/crosscompile.jetson.mk @@ -89,6 +89,10 @@ USE_NCCL_PATH = NONE # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 0 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE #whether use libjpeg-turbo for image decode without OpenCV wrapper USE_LIBJPEG_TURBO = 0 diff --git a/make/maven/maven_darwin_mkl.mk b/make/maven/maven_darwin_mkl.mk index f5b77ae784ad..f68f1565f990 100644 --- a/make/maven/maven_darwin_mkl.mk +++ b/make/maven/maven_darwin_mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=apple # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 0 diff --git a/make/maven/maven_linux_cu90mkl.mk b/make/maven/maven_linux_cu90mkl.mk index 661f444ceac3..3d3b2c48c58b 100644 --- a/make/maven/maven_linux_cu90mkl.mk +++ b/make/maven/maven_linux_cu90mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/maven/maven_linux_cu92mkl.mk b/make/maven/maven_linux_cu92mkl.mk index ecd252769216..ab93801b0b56 100644 --- a/make/maven/maven_linux_cu92mkl.mk +++ b/make/maven/maven_linux_cu92mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/maven/maven_linux_mkl.mk b/make/maven/maven_linux_mkl.mk index 6cb9f326bb7d..dfe5065b9a9c 100644 --- a/make/maven/maven_linux_mkl.mk +++ b/make/maven/maven_linux_mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 0 diff --git a/make/osx.mk b/make/osx.mk index 3e2e592323f4..7e32d81a5d71 100644 --- a/make/osx.mk +++ b/make/osx.mk @@ -75,6 +75,10 @@ USE_CUDNN = 0 # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # use openmp for parallelization # apple-clang by default does not have openmp built-in diff --git a/make/pip/pip_darwin_cpu.mk b/make/pip/pip_darwin_cpu.mk index 87a967933f9e..2c80c42387ad 100644 --- a/make/pip/pip_darwin_cpu.mk +++ b/make/pip/pip_darwin_cpu.mk @@ -58,6 +58,10 @@ USE_BLAS=apple # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 0 diff --git a/make/pip/pip_darwin_mkl.mk b/make/pip/pip_darwin_mkl.mk index 339c95fdd502..2fac512140d0 100644 --- a/make/pip/pip_darwin_mkl.mk +++ b/make/pip/pip_darwin_mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=apple # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 0 diff --git a/make/pip/pip_linux_cpu.mk b/make/pip/pip_linux_cpu.mk index d680f6da38d2..9ed88bb5bd1b 100644 --- a/make/pip/pip_linux_cpu.mk +++ b/make/pip/pip_linux_cpu.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 0 diff --git a/make/pip/pip_linux_cu100.mk b/make/pip/pip_linux_cu100.mk index 0f3f84a417a8..f3bea65a7260 100644 --- a/make/pip/pip_linux_cu100.mk +++ b/make/pip/pip_linux_cu100.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu100mkl.mk b/make/pip/pip_linux_cu100mkl.mk index b4792e3d81bb..4dfcb213feb1 100644 --- a/make/pip/pip_linux_cu100mkl.mk +++ b/make/pip/pip_linux_cu100mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu75.mk b/make/pip/pip_linux_cu75.mk index ff05ec80c68d..c686c20db456 100644 --- a/make/pip/pip_linux_cu75.mk +++ b/make/pip/pip_linux_cu75.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu75mkl.mk b/make/pip/pip_linux_cu75mkl.mk index a8259205a5d7..ec87676c6c96 100644 --- a/make/pip/pip_linux_cu75mkl.mk +++ b/make/pip/pip_linux_cu75mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu80.mk b/make/pip/pip_linux_cu80.mk index 7abf09f02eb6..72a04deeb400 100644 --- a/make/pip/pip_linux_cu80.mk +++ b/make/pip/pip_linux_cu80.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu80mkl.mk b/make/pip/pip_linux_cu80mkl.mk index be42cca0ef2b..1bd295138936 100644 --- a/make/pip/pip_linux_cu80mkl.mk +++ b/make/pip/pip_linux_cu80mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu90.mk b/make/pip/pip_linux_cu90.mk index 12bc7741bef4..b831d6586795 100644 --- a/make/pip/pip_linux_cu90.mk +++ b/make/pip/pip_linux_cu90.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu90mkl.mk b/make/pip/pip_linux_cu90mkl.mk index 770f292598c3..8ab13075b3d0 100644 --- a/make/pip/pip_linux_cu90mkl.mk +++ b/make/pip/pip_linux_cu90mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu91.mk b/make/pip/pip_linux_cu91.mk index 9e399f97d18f..cb66394683a9 100644 --- a/make/pip/pip_linux_cu91.mk +++ b/make/pip/pip_linux_cu91.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu91mkl.mk b/make/pip/pip_linux_cu91mkl.mk index 80ef690a00e5..a94874503c92 100644 --- a/make/pip/pip_linux_cu91mkl.mk +++ b/make/pip/pip_linux_cu91mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu92.mk b/make/pip/pip_linux_cu92.mk index b0731addbc2d..8796a3bbdaa7 100644 --- a/make/pip/pip_linux_cu92.mk +++ b/make/pip/pip_linux_cu92.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_cu92mkl.mk b/make/pip/pip_linux_cu92mkl.mk index 768fcc9c1cd0..b9766ec36dc8 100644 --- a/make/pip/pip_linux_cu92mkl.mk +++ b/make/pip/pip_linux_cu92mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 1 diff --git a/make/pip/pip_linux_mkl.mk b/make/pip/pip_linux_mkl.mk index ea11061e1e82..95146ee225ba 100644 --- a/make/pip/pip_linux_mkl.mk +++ b/make/pip/pip_linux_mkl.mk @@ -58,6 +58,10 @@ USE_BLAS=openblas # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 1 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDA during compile USE_CUDA = 0 diff --git a/make/readthedocs.mk b/make/readthedocs.mk index 0a45e6f03151..b33dd3c5d21f 100644 --- a/make/readthedocs.mk +++ b/make/readthedocs.mk @@ -36,6 +36,10 @@ USE_CUDA_PATH = NONE # you can disable it, however, you will not able to use # imbin iterator USE_OPENCV = 0 +# Add OpenCV include path, in which the directory `opencv2` exists +USE_OPENCV_INC_PATH = NONE +# Add OpenCV shared library path, in which the shared library exists +USE_OPENCV_LIB_PATH = NONE # whether use CUDNN R3 library USE_CUDNN = 0 diff --git a/src/io/image_aug_default.cc b/src/io/image_aug_default.cc index 5fb0b0f21e7e..01c0a7ab8c60 100644 --- a/src/io/image_aug_default.cc +++ b/src/io/image_aug_default.cc @@ -32,6 +32,7 @@ #include "../common/utils.h" #if MXNET_USE_OPENCV +#include "./opencv_compatibility.h" // Registers namespace dmlc { DMLC_REGISTRY_ENABLE(::mxnet::io::ImageAugmenterReg); diff --git a/src/io/image_det_aug_default.cc b/src/io/image_det_aug_default.cc index 91711bfb0359..74e51b51603b 100644 --- a/src/io/image_det_aug_default.cc +++ b/src/io/image_det_aug_default.cc @@ -203,6 +203,7 @@ std::vector ListDefaultDetAugParams() { } #if MXNET_USE_OPENCV +#include "./opencv_compatibility.h" using Rect = cv::Rect_; #ifdef _MSC_VER diff --git a/src/io/image_io.cc b/src/io/image_io.cc index ab55c1a4c901..2196983928bb 100644 --- a/src/io/image_io.cc +++ b/src/io/image_io.cc @@ -41,6 +41,7 @@ #if MXNET_USE_OPENCV #include + #include "./opencv_compatibility.h" #endif // MXNET_USE_OPENCV namespace mxnet { diff --git a/src/io/opencv_compatibility.h b/src/io/opencv_compatibility.h new file mode 100644 index 000000000000..7f42328497ed --- /dev/null +++ b/src/io/opencv_compatibility.h @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file opencv_compatibility.h + * \brief To be compatible with multiple versions of opencv + */ +#ifndef MXNET_IO_OPENCV_COMPATIBILITY_H_ +#define MXNET_IO_OPENCV_COMPATIBILITY_H_ + +#if MXNET_USE_OPENCV +#include + +#if CV_VERSION_MAJOR >= 4 +#include +#define CV_RGB2GRAY cv::COLOR_RGB2GRAY +#define CV_BGR2GRAY cv::COLOR_BGR2GRAY + +#define CV_GRAY2RGB cv::COLOR_GRAY2RGB +#define CV_GRAY2BGR cv::COLOR_GRAY2BGR + +#define CV_RGB2HLS cv::COLOR_RGB2HLS +#define CV_BGR2HLS cv::COLOR_BGR2HLS + +#define CV_HLS2RGB cv::COLOR_HLS2RGB +#define CV_HLS2BGR cv::COLOR_HLS2BGR + +#define CV_RGB2BGR cv::COLOR_RGB2BGR +#define CV_BGR2RGB cv::COLOR_BGR2RGB + +#define CV_INTER_LINEAR cv::INTER_LINEAR +#define CV_INTER_NEAREST cv::INTER_NEAREST + +#define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR +#define CV_IMWRITE_PNG_COMPRESSION cv::IMWRITE_PNG_COMPRESSION +#define CV_IMWRITE_JPEG_QUALITY cv::IMWRITE_JPEG_QUALITY + +#endif // CV_VERSION_MAJOR >= 4 + +#endif // MXNET_USE_OPENCV + +#endif // MXNET_IO_OPENCV_COMPATIBILITY_H_ diff --git a/tools/im2rec.cc b/tools/im2rec.cc index 915b78029c87..989b3147830d 100644 --- a/tools/im2rec.cc +++ b/tools/im2rec.cc @@ -39,6 +39,7 @@ #include #include #include +#include "../src/io/opencv_compatibility.h" #include "../src/io/image_recordio.h" #include /*!