forked from luigifreda/plvs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_local_opencv.sh
executable file
·218 lines (194 loc) · 7.29 KB
/
install_local_opencv.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
#!/usr/bin/env bash
# ====================================================
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 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.8.0" # 4.7.0 4.3.0 4.2.0 4.0.0 3.4.6 3.4.4 3.4.3 3.4.2 3.4.1
# ====================================================
print_blue "Configuring and building $TARGET_FOLDER/opencv ..."
set -e
STARTING_DIR=`pwd`
version=$(lsb_release -a 2>&1) # ubuntu version
if [ ! -d $TARGET_FOLDER ]; then
mkdir $TARGET_FOLDER
fi
# set CUDA
#export CUDA_VERSION="cuda-11.6" # 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
# pre-installing some required packages
if [ ! -d $TARGET_FOLDER/opencv ]; 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
sudo apt-get install -y libcudnn8 libcudnn8-dev
fi
if [[ $version == *"20.04"* ]] || [[ $version == *"22.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
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)"
if [ "$machine" == "x86_64" ]; then
# standard configuration
echo "building x86_64 config"
# 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 ..
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
fi
cd $STARTING_DIR
echo "...done with opencv"