Skip to content

Commit

Permalink
feat: opencv optional build
Browse files Browse the repository at this point in the history
  • Loading branch information
DRM-Free committed Jun 13, 2024
1 parent 2de8d4c commit ce9b9a7
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ while (("$#")); do
esac
done

# Check arguments for incompatibility
if [[ -v DEEPDETECT_OPENCV4_BUILD_PATH ]] && [[ -v BUILD_OPENCV ]]; then
echo "Please choose between setting DEEPDETECT_OPENCV4_BUILD_PATH or BUILD_OPENCV"
echo "Use DEEPDETECT_OPENCV4_BUILD_PATH if you wish to use a custom opencv build"
echo "Use BUILD_OPENCV if you wish for opencv to be built for you"
exit
fi

# Deepdetect platform selector
select_platform() {
clear
Expand Down Expand Up @@ -92,7 +100,6 @@ select_cpu_build_profile() {
read -p "Enter choice : " choice
DEEPDETECT_BUILD=${deepdetect_cpu_build_profiles[$choice]}
}

# Deepdetect select GPU build profile
select_gpu_build_profile() {
clear
Expand Down Expand Up @@ -121,23 +128,75 @@ show_interactive_platform_selector() {
fi
}

if [[ ${BUILD_OPENCV} == "ON" ]]; then
echo "Opencv will be built from source"
# Build OpenCV 4 with CUDA
DEEPDETECT_OPENCV4_BUILD_PATH="$(git rev-parse --show-toplevel)/build/opencv/opencv-4.7.0/build"
if [ ! -d opencv ]; then
echo "Downloading opencv"
mkdir opencv && cd opencv && wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/4.7.0.zip && wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/4.7.0.zip && unzip opencv.zip && unzip opencv_contrib.zip
cd "opencv-4.7.0" && mkdir build && cd build
else
cd $DEEPDETECT_OPENCV4_BUILD_PATH
fi
cmake -D CMAKE_BUILD_TYPE=DEBUG \
-D CMAKE_INSTALL_PREFIX=/tmp/ \
-D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs \
-D CMAKE_CXX_FLAGS="-Wl,--allow-shlib-undefined" \
-D WITH_TBB=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=OFF \
-D CUDA_ARCH_BIN="6.1 7.5 8.6" \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D WITH_NVCUVID=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.7.0/modules \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_opencv_cudev=ON \
-D BUILD_opencv_core=ON \
-D BUILD_opencv_imgproc=ON \
-D BUILD_opencv_imgcodecs=ON \
-D BUILD_opencv_videoio=ON \
-D BUILD_opencv_highgui=ON ..

make -j20
make install
cd ../../..
else
echo "Using custom opencv"
fi

# Build functions
cpu_build() {

case ${DEEPDETECT_BUILD} in

"tf")
cmake .. -DUSE_TF=ON -DUSE_TF_CPU_ONLY=ON -DUSE_SIMSEARCH=ON -DUSE_TSNE=ON -DUSE_NCNN=OFF -DUSE_CPU_ONLY=ON -DRELEASE=${DEEPDETECT_RELEASE}
cmake .. -DUSE_TF=ON -DUSE_TF_CPU_ONLY=ON -DUSE_SIMSEARCH=ON -DUSE_TSNE=ON -DUSE_NCNN=OFF -DUSE_CPU_ONLY=ON -DRELEASE=${DEEPDETECT_RELEASE} -DOpenCV_DIR=${DEEPDETECT_OPENCV4_BUILD_PATH}
make -j6
;;

"armv7")
cmake .. -DUSE_NCNN=ON -DRPI3=ON -DUSE_HDF5=OFF -DUSE_TORCH=OFF -DRELEASE=${DEEPDETECT_RELEASE}
cmake .. -DUSE_NCNN=ON -DRPI3=ON -DUSE_HDF5=OFF -DUSE_TORCH=OFF -DRELEASE=${DEEPDETECT_RELEASE} -DOpenCV_DIR=${DEEPDETECT_OPENCV4_BUILD_PATH}
make -j6
;;

*)
cmake .. -DUSE_XGBOOST=ON -DUSE_CAFFE=ON -DUSE_CPU_ONLY=ON -DUSE_SIMSEARCH=ON -DUSE_TSNE=ON -DUSE_NCNN=ON -DRELEASE=${DEEPDETECT_RELEASE}
cmake .. -DUSE_XGBOOST=ON -DUSE_CAFFE=ON -DUSE_CPU_ONLY=ON -DUSE_SIMSEARCH=ON -DUSE_TSNE=ON -DUSE_NCNN=ON -DRELEASE=${DEEPDETECT_RELEASE} -DOpenCV_DIR=${DEEPDETECT_OPENCV4_BUILD_PATH}
make -j6
;;
esac
Expand All @@ -146,14 +205,17 @@ cpu_build() {

gpu_build() {
local extra_flags=
local default_flags="-DUSE_FAISS=ON -DUSE_CUDNN=ON -DUSE_XGBOOST=ON -DUSE_SIMSEARCH=ON -DUSE_TSNE=ON -DUSE_CAFFE=ON"
local default_flags="-DUSE_FAISS=OFF -DUSE_CUDNN=ON -DUSE_XGBOOST=OFF -DUSE_SIMSEARCH=OFF -DUSE_TSNE=OFF -DUSE_TORCH=ON -DUSE_OPENCV_VERSION=4 -DOpenCV_DIR=${DEEPDETECT_OPENCV4_BUILD_PATH}"

case ${DEEPDETECT_BUILD} in
"tf") extra_flags="$default_flags -DUSE_TF=ON" ;;
"caffe2") extra_flags="$default_flags -DUSE_CAFFE2=ON" ;;
"tensorrt") extra_flags="-DUSE_TENSORRT=ON -DUSE_TORCH=OFF -DUSE_CUDA_CV=ON -DUSE_OPENCV_VERSION=4 -DOpenCV_DIR=${DEEPDETECT_OPENCV4_BUILD_PATH}";;
*) extra_flags="$default_flags";;
esac
cmake .. $extra_flags -DCUDA_ARCH_FLAGS="${DEEPDETECT_CUDA_ARCH_FLAGS}" -DCUDA_ARCH="${DEEPDETECT_CUDA_ARCH}" "-DRELEASE=${DEEPDETECT_RELEASE}"
echo $extra_flags

cmake .. $extra_flags -DCUDA_ARCH_FLAGS="${DEEPDETECT_CUDA_ARCH_FLAGS}" -DCUDA_ARCH="${DEEPDETECT_CUDA_ARCH}" -DRELEASE="${DEEPDETECT_RELEASE}" -DOpenCV_DIR="${DEEPDETECT_OPENCV4_BUILD_PATH}"
make -j6
}

Expand Down

0 comments on commit ce9b9a7

Please sign in to comment.