Skip to content

Commit

Permalink
compatibility with opencv4 (apache#14313)
Browse files Browse the repository at this point in the history
* compatibility with opencv4

* update makefile

* update Makefile

* fix Makefile

* fix lib path

* update make.mk

* add -lopencv_highgui

* retrigger CI

* update makefile

* remove libs-L

* Fix OpenCV linking order

* try to fix the bug of linking static libraries
  • Loading branch information
wkcn authored and haohuw committed Jun 23, 2019
1 parent e8f233c commit 1aa4570
Show file tree
Hide file tree
Showing 30 changed files with 185 additions and 4 deletions.
30 changes: 26 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ ifdef CAFFE_PATH
endif

ifndef LINT_LANG
LINT_LANG="all"
LINT_LANG = "all"
endif

ifeq ($(USE_MKLDNN), 1)
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions make/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/crosscompile.jetson.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/maven/maven_darwin_mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/maven/maven_linux_cu90mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/maven/maven_linux_cu92mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/maven/maven_linux_mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/osx.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_darwin_cpu.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_darwin_mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cpu.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu100.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu100mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu75.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu75mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu80.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu80mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu90.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu90mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu91.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu91mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu92.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_cu92mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/pip/pip_linux_mkl.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions make/readthedocs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/io/image_aug_default.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/io/image_det_aug_default.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ std::vector<dmlc::ParamFieldInfo> ListDefaultDetAugParams() {
}

#if MXNET_USE_OPENCV
#include "./opencv_compatibility.h"
using Rect = cv::Rect_<float>;

#ifdef _MSC_VER
Expand Down
1 change: 1 addition & 0 deletions src/io/image_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#if MXNET_USE_OPENCV
#include <opencv2/opencv.hpp>
#include "./opencv_compatibility.h"
#endif // MXNET_USE_OPENCV

namespace mxnet {
Expand Down
59 changes: 59 additions & 0 deletions src/io/opencv_compatibility.h
Original file line number Diff line number Diff line change
@@ -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 <opencv2/core/version.hpp>

#if CV_VERSION_MAJOR >= 4
#include <opencv2/opencv.hpp>
#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_
Loading

0 comments on commit 1aa4570

Please sign in to comment.