-
Notifications
You must be signed in to change notification settings - Fork 350
/
install_opencv_local.sh
executable file
·301 lines (269 loc) · 10.8 KB
/
install_opencv_local.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env bash
# Author: Luigi Freda
# ====================================================
function print_blue(){
printf "\033[34;1m"
printf "$@ \n"
printf "\033[0m"
}
function print_red(){
printf "\033[31;1m"
printf "$@ \n"
printf "\033[0m"
}
function check_package(){
package_name=$1
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $package_name |grep "install ok installed")
#echo "checking for $package_name: $PKG_OK"
if [ "" == "$PKG_OK" ]; then
#echo "$package_name is not installed"
echo 1
else
echo 0
fi
}
function install_package(){
do_install=$(check_package $1)
if [ $do_install -eq 1 ] ; then
sudo apt-get install -y $1
fi
}
function install_packages(){
for var in "$@"
do
install_package "$var"
done
}
function get_usable_cuda_version(){
version="$1"
if [[ "$version" != *"cuda"* ]]; then
version="cuda-${version}"
fi
# check if we have two dots in the version, check if the folder exists otherwise remove last dot
if [[ $version =~ ^[a-zA-Z0-9-]+\.[0-9]+\.[0-9]+$ ]]; then
if [ ! -d /usr/local/$version ]; then
version="${version%.*}" # remove last dot
fi
fi
echo $version
}
# ====================================================
export TARGET_FOLDER=thirdparty
export OPENCV_VERSION="4.10.0" # OpenCV version to download and install. See tags in https://github.com/opencv/opencv
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
STARTING_DIR=`pwd`
# ====================================================
print_blue "Configuring and building $TARGET_FOLDER/opencv ..."
#pip install --upgrade pip
pip uninstall -y opencv-python
pip uninstall -y opencv-contrib-python
set -e
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
version=$(lsb_release -a 2>&1) # ubuntu version
else
version=$OSTYPE
echo "OS: $version"
fi
if [ ! -d $TARGET_FOLDER ]; then
mkdir $TARGET_FOLDER
fi
# set CUDA
#export CUDA_VERSION="cuda-11.8" # must be an installed CUDA path in /usr/local;
# if available, you can use the simple path "/usr/local/cuda" which should be a symbolic link to the last installed cuda version
CUDA_ON=ON
if [[ -n "$CUDA_VERSION" ]]; then
CUDA_VERSION=$(get_usable_cuda_version $CUDA_VERSION)
echo using CUDA $CUDA_VERSION
if [ ! -d /usr/local/$CUDA_VERSION ]; then
echo CUDA $CUDA_VERSION does not exist
CUDA_ON=OFF
fi
else
if [ -d /usr/local/cuda ]; then
CUDA_VERSION="cuda" # use last installed CUDA path
echo using CUDA $CUDA_VERSION
else
print_red "Warning: CUDA $CUDA_VERSION not found and will not be used!"
CUDA_ON=OFF
fi
fi
echo CUDA_ON: $CUDA_ON
export PATH=/usr/local/$CUDA_VERSION/bin${PATH:+:${PATH}} # this is for having the right nvcc in the path
export LD_LIBRARY_PATH=/usr/local/$CUDA_VERSION/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} # this is for libs
WITH_APPLE_FRAMEWORK=OFF
WITH_PROTOBUF=ON
if [[ "$version" == *"darwin"* ]]; then
#WITH_APPLE_FRAMEWORK=ON # this will make opencv generate a single libopencv_world.so without the separate modules
CUDA_ON=OFF
WITH_PROTOBUF=OFF # I am getting a protobuf version error on my mac
fi
WITH_NEON=OFF
arch=$(uname -m)
if [[ "$arch" == "arm64" || "$arch" == "aarch64" || "$arch" == arm* ]]; then
WITH_NEON=ON
fi
# pre-installing some required packages
if [[ ! -d $TARGET_FOLDER/opencv ]]; then
if [[ $version != *"darwin"* ]]; then
sudo apt-get update
sudo apt-get install -y pkg-config libglew-dev libtiff5-dev zlib1g-dev libjpeg-dev libeigen3-dev libtbb-dev libgtk2.0-dev libopenblas-dev
sudo apt-get install -y curl software-properties-common unzip
sudo apt-get install -y build-essential cmake
if [[ "$CUDA_ON" == "ON" ]]; then
if [[ $version == *"24.04"* ]] ; then
install_packages libcudnn-dev
else
install_packages libcudnn8 libcudnn8-dev # check and install otherwise this is going to update to the latest version (and that's not we necessary want to do)
fi
fi
if [[ $version == *"22.04"* || $version == *"24.04"* ]] ; then
sudo apt install -y libtbb-dev libeigen3-dev
sudo apt install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev
sudo add-apt-repository -y "deb http://security.ubuntu.com/ubuntu xenial-security main" # for libjasper-dev
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32 # for libjasper-dev
sudo apt update
sudo apt install -y libjasper-dev
sudo apt install -y libv4l-dev libdc1394-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm \
libopencore-amrnb-dev libopencore-amrwb-dev libxine2-dev
fi
if [[ $version == *"20.04"* ]] ; then
sudo apt install -y libtbb-dev libeigen3-dev
sudo apt install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" # for libjasper-dev
sudo apt install -y libjasper-dev
sudo apt install -y libv4l-dev libdc1394-22-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm \
libopencore-amrnb-dev libopencore-amrwb-dev libxine2-dev
fi
if [[ $version == *"18.04"* ]] ; then
sudo apt-get install -y libpng-dev
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" # for libjasper-dev
sudo apt-get install -y libjasper-dev
fi
if [[ $version == *"16.04"* ]] ; then
sudo apt-get install -y libpng12-dev libjasper-dev
fi
DO_INSTALL_FFMPEG=$(check_package ffmpeg)
if [ $DO_INSTALL_FFMPEG -eq 1 ] ; then
echo "installing ffmpeg and its dependencies"
sudo apt-get install -y libavcodec-dev libavformat-dev libavutil-dev libpostproc-dev libswscale-dev
fi
else
brew install pkg-config
brew install glew
brew install cmake
brew install suitesparse
brew install lapack
brew install libtiff zlib jpeg eigen tbb glew libpng webp x264 ffmpeg
fi
fi
# now let's download and compile opencv and opencv_contrib
# N.B: if you want just to update cmake settings and recompile then remove "opencv/install" and "opencv/build/CMakeCache.txt"
cd $TARGET_FOLDER
#if [ ! -d opencv/install ]; then
if [ ! -f opencv/install/lib/libopencv_core.so ]; then
if [ ! -d opencv ]; then
wget https://github.com/opencv/opencv/archive/$OPENCV_VERSION.zip
sleep 1
unzip $OPENCV_VERSION.zip
rm $OPENCV_VERSION.zip
cd opencv-$OPENCV_VERSION
wget https://github.com/opencv/opencv_contrib/archive/$OPENCV_VERSION.zip
sleep 1
unzip $OPENCV_VERSION.zip
rm $OPENCV_VERSION.zip
cd ..
mv opencv-$OPENCV_VERSION opencv
fi
echo "entering opencv"
cd opencv
mkdir -p build
mkdir -p install
cd build
echo "I am in "$(pwd)
machine="$(uname -m)"
echo OS: $version
if [[ "$machine" == "x86_64" || "$machine" == "x64" || $version == "darwin"* ]]; then
# standard configuration
echo "building laptop/desktop config under $version"
# as for the flags and consider this nice reference https://gist.github.com/raulqf/f42c718a658cddc16f9df07ecc627be7
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="`pwd`/../install" \
-DOPENCV_EXTRA_MODULES_PATH="`pwd`/../opencv_contrib-$OPENCV_VERSION/modules" \
-DWITH_QT=ON \
-DWITH_GTK=OFF \
-DWITH_OPENGL=ON \
-DWITH_TBB=ON \
-DWITH_V4L=ON \
-DWITH_CUDA=$CUDA_ON \
-DWITH_CUBLAS=$CUDA_ON \
-DWITH_CUFFT=$CUDA_ON \
-DCUDA_FAST_MATH=$CUDA_ON \
-DWITH_CUDNN=$CUDA_ON \
-DOPENCV_DNN_CUDA=$CUDA_ON \
-DCUDA_ARCH_BIN="5.3 6.0 6.1 7.0 7.5 8.6" \
-DBUILD_opencv_cudacodec=OFF \
-DENABLE_FAST_MATH=1 \
-DBUILD_NEW_PYTHON_SUPPORT=ON \
-DBUILD_DOCS=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
-DINSTALL_PYTHON_EXAMPLES=OFF \
-DINSTALL_C_EXAMPLES=OFF \
-DBUILD_EXAMPLES=OFF \
-DOPENCV_ENABLE_NONFREE=ON \
-DBUILD_opencv_java=OFF \
-DBUILD_opencv_python3=ON \
-Wno-deprecated-gpu-targets \
-DENABLE_NEON=${WITH_NEON:-OFF} \
-DBUILD_PROTOBUF=${WITH_PROTOBUF:-OFF} \
-DAPPLE_FRAMEWORK=${WITH_APPLE_FRAMEWORK:-OFF} \
-DPYTHON_INCLUDE_DIR=$(python -c "import sysconfig; print(sysconfig.get_path('include'))") \
-DPYTHON_LIBRARY=$(python -c "import sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
..
else
# Nvidia Jetson aarch64
echo "building NVIDIA Jetson config"
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="`pwd`/../install" \
-DOPENCV_EXTRA_MODULES_PATH="`pwd`/../opencv_contrib-$OPENCV_VERSION/modules" \
-DWITH_QT=ON \
-DWITH_GTK=OFF \
-DWITH_OPENGL=ON \
-DWITH_TBB=ON \
-DWITH_V4L=ON \
-DWITH_CUDA=ON \
-DWITH_CUBLAS=ON \
-DWITH_CUFFT=ON \
-DCUDA_FAST_MATH=ON \
-DCUDA_ARCH_BIN="6.2" \
-DCUDA_ARCH_PTX="" \
-DBUILD_opencv_cudacodec=OFF \
-DENABLE_NEON=ON \
-DENABLE_FAST_MATH=ON \
-DBUILD_NEW_PYTHON_SUPPORT=ON \
-DBUILD_DOCS=OFF \
-DBUILD_TESTS=OFF \
-DBUILD_PERF_TESTS=OFF \
-DINSTALL_PYTHON_EXAMPLES=OFF \
-DINSTALL_C_EXAMPLES=OFF \
-DBUILD_EXAMPLES=OFF \
-Wno-deprecated-gpu-targets ..
fi
make -j8
make install
cd ..
echo "deploying built cv2 python module"
PYTHON_VERSION=$(python -c "import sys; print(f\"{sys.version_info.major}.{sys.version_info.minor}\")")
PYTHON_SITE_PACKAGES=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
PYTHON_SOURCE_FOLDER=$(pwd)/install/lib/python$PYTHON_VERSION/site-packages/cv2
if [[ -d "$PYTHON_SITE_PACKAGES" && -d "$PYTHON_SOURCE_FOLDER" ]]; then
echo "copying built python cv2 module from $PYTHON_SOURCE_FOLDER to $PYTHON_SITE_PACKAGES"
cp -r $PYTHON_SOURCE_FOLDER $PYTHON_SITE_PACKAGES
else
echo "ERROR: failed to copy build python cv2 module from $PYTHON_SOURCE_FOLDER to $PYTHON_SITE_PACKAGES"
fi
fi
cd $STARTING_DIR
echo "...done with opencv"