<commit_before>#!/usr/bin/env bash

###############################################################################
# Copyright 2020 The Apollo Authors. All Rights Reserved.
#
# Licensed 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.
###############################################################################
# Fail on first error.
set -e

cd "$(dirname "${BASH_SOURCE[0]}")"
. /tmp/installers/installer_base.sh

if ldconfig -p | grep -q libopencv_core ; then
    info "OpenCV was already installed"
    exit 0
fi

WORKHORSE="$1"
if [ -z "${WORKHORSE}" ]; then
    WORKHORSE="cpu"
fi

# 1) Install OpenCV via apt
# apt-get -y update && \
#    apt-get -y install \
#    libopencv-core-dev \
#    libopencv-imgproc-dev \
#    libopencv-imgcodecs-dev \
#    libopencv-highgui-dev \
#    libopencv-dev
# 2) Build OpenCV from source
# RTFM: https://src.fedoraproject.org/rpms/opencv/blob/master/f/opencv.spec
pip3_install numpy

VERSION="4.4.0"

PKG_OCV="opencv-${VERSION}.tar.gz"
CHECKSUM="bb95acd849e458be7f7024d17968568d1ccd2f0681d47fd60d34ffb4b8c52563"
DOWNLOAD_LINK="https://github.com/opencv/opencv/archive/${VERSION}.tar.gz"
download_if_not_cached "${PKG_OCV}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
tar xzf ${PKG_OCV}

PKG_CONTRIB="opencv_contrib-${VERSION}.tar.gz"
CHECKSUM="a69772f553b32427e09ffbfd0c8d5e5e47f7dab8b3ffc02851ffd7f912b76840"
DOWNLOAD_LINK="https://github.com/opencv/opencv_contrib/archive/${VERSION}.tar.gz"
download_if_not_cached "${PKG_CONTRIB}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
tar xzf ${PKG_CONTRIB}

apt-get -y update && \
    apt-get -y install \
        libjpeg-dev \
        libpng-dev \
        libtiff-dev \
        libv4l-dev \
        libeigen3-dev \
        libopenblas-dev \
        libatlas-base-dev \
        libxvidcore-dev \
        libx264-dev

# https://stackoverflow.com/questions/12427928/configure-and-build-opencv-to-custom-ffmpeg-install
export LD_LIBRARY_PATH=${SYSROOT_DIR}/lib
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${SYSROOT_DIR}/lib/pkgconfig
export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR:${SYSROOT_DIR}/lib

# libgtk-3-dev libtbb2 libtbb-dev
# -DWITH_GTK=ON -DWITH_TBB=ON

GPU_OPTIONS=
if [ "${WORKHORSE}" = "gpu" ]; then
    GPU_OPTIONS="-DWITH_CUDA=ON -DWITH_CUFFT=ON -DWITH_CUBLAS=ON -DWITH_CUDNN=ON"
    GPU_OPTIONS="${GPU_OPTIONS} -DCUDA_PROPAGATE_HOST_FLAGS=OFF"
    GPU_OPTIONS="${GPU_OPTIONS} -DCUDA_ARCH_BIN=\"${SUPPORTED_NVIDIA_SMS}\""
    # GPU_OPTIONS="${GPU_OPTIONS} -DWITH_NVCUVID=ON"
else
    GPU_OPTIONS="-DWITH_CUDA=OFF"
fi

TARGET_ARCH="$(uname -m)"
ARCH_OPTIONS=""
if [ "${TARGET_ARCH}" = "x86_64" ]; then
    ARCH_OPTIONS="-DCPU_BASELINE=SSE4"
fi

COMM_DIR="$(pwd)"

pushd "opencv-${VERSION}"
    mkdir build && cd build
        cmake .. \
            -DCMAKE_INSTALL_PREFIX="${SYSROOT_DIR}" \
            -DCMAKE_BUILD_TYPE=Release \
            -DBUILD_SHARED_LIBS=ON          \
            -DENABLE_PRECOMPILED_HEADERS=OFF \
            -DOPENCV_EXTRA_MODULES_PATH="${COMM_DIR}/opencv_contrib-${VERSION}/modules" \
            -DOPENCV_GENERATE_PKGCONFIG=ON  \
            -DBUILD_EXAMPLES=OFF \
            -DBUILD_DOCS=OFF    \
            -DBUILD_TESTS=OFF   \
            -DBUILD_PERF_TESTS=OFF  \
            -DBUILD_JAVA=OFF     \
            -DBUILD_PROTOBUF=OFF \
            -DPROTOBUF_UPDATE_FILES=ON \
            -DINSTALL_C_EXAMPLES=OFF   \
            -DWITH_EIGEN=ON    \
            -DWITH_FFMPEG=ON   \
            -DWITH_OPENMP=ON   \
            -DWITH_LIBV4L=ON   \
            -DWITH_OPENCL=ON   \
            -DWITH_GTK=OFF     \
            -DWITH_TBB=OFF     \
            -DOpenGL_GL_PREFERENCE=GLVND \
            -DBUILD_opencv_python2=OFF  \
            -DBUILD_opencv_python3=ON   \
            -DBUILD_NEW_PYTHON_SUPPORT=ON \
            -DPYTHON_DEFAULT_EXECUTABLE="$(which python3)" \
            -DOPENCV_PYTHON3_INSTALL_PATH="/usr/local/lib/python$(py3_version)/dist-packages" \
            -DOPENCV_ENABLE_NONFREE=ON \
            "${GPU_OPTIONS}" \
            "${ARCH_OPTIONS}" \
            -DCV_TRACE=OFF

        make -j$(nproc)
        make install
popd

ldconfig
rm -rf ${PKG_OCV} ${PKG_CONTRIB} "opencv-${VERSION}" "opencv_contrib-${VERSION}"<commit_msg>Docker: disable IPP/ITT in OpenCV installer<commit_after>#!/usr/bin/env bash

###############################################################################
# Copyright 2020 The Apollo Authors. All Rights Reserved.
#
# Licensed 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.
###############################################################################
# Fail on first error.
set -e

cd "$(dirname "${BASH_SOURCE[0]}")"
. /tmp/installers/installer_base.sh

if ldconfig -p | grep -q libopencv_core ; then
    info "OpenCV was already installed"
    exit 0
fi

WORKHORSE="$1"
if [ -z "${WORKHORSE}" ]; then
    WORKHORSE="cpu"
fi

# 1) Install OpenCV via apt
# apt-get -y update && \
#    apt-get -y install \
#    libopencv-core-dev \
#    libopencv-imgproc-dev \
#    libopencv-imgcodecs-dev \
#    libopencv-highgui-dev \
#    libopencv-dev
# 2) Build OpenCV from source
# RTFM: https://src.fedoraproject.org/rpms/opencv/blob/master/f/opencv.spec
pip3_install numpy

VERSION="4.4.0"

PKG_OCV="opencv-${VERSION}.tar.gz"
CHECKSUM="bb95acd849e458be7f7024d17968568d1ccd2f0681d47fd60d34ffb4b8c52563"
DOWNLOAD_LINK="https://github.com/opencv/opencv/archive/${VERSION}.tar.gz"
download_if_not_cached "${PKG_OCV}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
tar xzf ${PKG_OCV}

PKG_CONTRIB="opencv_contrib-${VERSION}.tar.gz"
CHECKSUM="a69772f553b32427e09ffbfd0c8d5e5e47f7dab8b3ffc02851ffd7f912b76840"
DOWNLOAD_LINK="https://github.com/opencv/opencv_contrib/archive/${VERSION}.tar.gz"
download_if_not_cached "${PKG_CONTRIB}" "${CHECKSUM}" "${DOWNLOAD_LINK}"
tar xzf ${PKG_CONTRIB}

apt-get -y update && \
    apt-get -y install \
        libjpeg-dev \
        libpng-dev \
        libtiff-dev \
        libv4l-dev \
        libeigen3-dev \
        libopenblas-dev \
        libatlas-base-dev \
        libxvidcore-dev \
        libx264-dev

# https://stackoverflow.com/questions/12427928/configure-and-build-opencv-to-custom-ffmpeg-install
export LD_LIBRARY_PATH=${SYSROOT_DIR}/lib
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:${SYSROOT_DIR}/lib/pkgconfig
export PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR:${SYSROOT_DIR}/lib

# libgtk-3-dev libtbb2 libtbb-dev
# -DWITH_GTK=ON -DWITH_TBB=ON

GPU_OPTIONS=
if [ "${WORKHORSE}" = "gpu" ]; then
    GPU_OPTIONS="-DWITH_CUDA=ON -DWITH_CUFFT=ON -DWITH_CUBLAS=ON -DWITH_CUDNN=ON"
    GPU_OPTIONS="${GPU_OPTIONS} -DCUDA_PROPAGATE_HOST_FLAGS=OFF"
    GPU_OPTIONS="${GPU_OPTIONS} -DCUDA_ARCH_BIN=\"${SUPPORTED_NVIDIA_SMS}\""
    # GPU_OPTIONS="${GPU_OPTIONS} -DWITH_NVCUVID=ON"
else
    GPU_OPTIONS="-DWITH_CUDA=OFF"
fi

TARGET_ARCH="$(uname -m)"
ARCH_OPTIONS=""
if [ "${TARGET_ARCH}" = "x86_64" ]; then
    ARCH_OPTIONS="-DCPU_BASELINE=SSE4"
fi

COMM_DIR="$(pwd)"

pushd "opencv-${VERSION}"
    mkdir build && cd build
        cmake .. \
            -DCMAKE_INSTALL_PREFIX="${SYSROOT_DIR}" \
            -DCMAKE_BUILD_TYPE=Release \
            -DBUILD_SHARED_LIBS=ON          \
            -DENABLE_PRECOMPILED_HEADERS=OFF \
            -DOPENCV_EXTRA_MODULES_PATH="${COMM_DIR}/opencv_contrib-${VERSION}/modules" \
            -DOPENCV_GENERATE_PKGCONFIG=ON  \
            -DBUILD_EXAMPLES=OFF \
            -DBUILD_DOCS=OFF    \
            -DBUILD_TESTS=OFF   \
            -DBUILD_PERF_TESTS=OFF  \
            -DBUILD_JAVA=OFF     \
            -DBUILD_PROTOBUF=OFF \
            -DPROTOBUF_UPDATE_FILES=ON \
            -DINSTALL_C_EXAMPLES=OFF   \
            -DWITH_IPP=OFF  \
            -DWITH_ITT=OFF  \
            -DWITH_EIGEN=ON \
            -DWITH_FFMPEG=ON   \
            -DWITH_OPENMP=ON   \
            -DWITH_LIBV4L=ON   \
            -DWITH_OPENCL=ON   \
            -DWITH_GTK=OFF  \
            -DWITH_TBB=OFF  \
            -DOpenGL_GL_PREFERENCE=GLVND \
            -DBUILD_opencv_python2=OFF  \
            -DBUILD_opencv_python3=ON   \
            -DBUILD_NEW_PYTHON_SUPPORT=ON \
            -DPYTHON_DEFAULT_EXECUTABLE="$(which python3)" \
            -DOPENCV_PYTHON3_INSTALL_PATH="/usr/local/lib/python$(py3_version)/dist-packages" \
            -DOPENCV_ENABLE_NONFREE=ON \
            "${GPU_OPTIONS}" \
            "${ARCH_OPTIONS}" \
            -DCV_TRACE=OFF

        make -j$(nproc)
        make install
popd

ldconfig
rm -rf ${PKG_OCV} ${PKG_CONTRIB} "opencv-${VERSION}" "opencv_contrib-${VERSION}"
{'fraction_non_alphanumeric': 0.1534690397527443, 'fraction_numerical': 0.02579132473622509, 'mean_word_length': 2.952822240943555, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4879845', 'n_tokens_mistral': 3603, 'n_tokens_neox': 3433, 'n_words': 550}