-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile.opencv
More file actions
264 lines (231 loc) · 10.4 KB
/
Dockerfile.opencv
File metadata and controls
264 lines (231 loc) · 10.4 KB
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
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/
#! IMPORTANT: Please make sure that your NVIDIA Jetson device is running the latest JetPack version.
#! This is important since the image mounts various libraries (e.g., libcudnn, CUDA) from the host system.
#! NOTE: This image builds OpenCV without the NVIDIA Video Codec SDK by default due to copyright issues.
#! Thus, the image provided at ghcr.io/appsolves/lanepilot/opencv_base:latest does not include the necessary dependencies.
#! To build the image with the NVIDIA Video Codec SDK, do the following:
#! 1. Create a directory named video_codec_sdk in the same directory as this Dockerfile.
#! 2. Download the NVIDIA Video Codec SDK zip file and place it in the video_codec_sdk directory.
#! 3. Run the `scripts/build_opencv.sh` script to build the image locally with the NVIDIA Video Codec SDK included.
ARG L4T_TRT_VERSION=r10.3.0
ARG BASE_IMAGE_VERSION=r36.2.0
ARG PLATFORM=linux/arm64
### Builder image ###
FROM --platform=${PLATFORM} nvcr.io/nvidia/l4t-tensorrt:${L4T_TRT_VERSION}-devel AS opencv-builder
# Compute capability is set for NVIDIA Jetson Orin Nano by default
ARG CUDA_COMPUTE_CAPABILITY=8.7
# Install OpenCV Contrib modules
ARG OPENCV_CONTRIB=1
# Set to non-interactive mode to avoid prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive
# Install dependencies and required packages
RUN apt-get update -y --fix-missing && apt-get upgrade -y --fix-missing
RUN apt-get install -y build-essential ninja-build git wget unzip pkg-config zlib1g-dev \
python3-dev python-dev-is-python3 \
gstreamer1.0-tools libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-good1.0-dev \
libtbb2 cmake libjpeg8-dev libjpeg-turbo8-dev \
libglew-dev python3-pip libtiff-dev \
libavcodec-dev libavformat-dev libswscale-dev libswresample-dev \
libxvidcore-dev libx264-dev \
libtbb-dev libxine2-dev \
libv4l-dev v4l-utils qv4l2 \
libtesseract-dev libpostproc-dev \
libvorbis-dev libaom-dev libdav1d-dev \
libyuv-dev libjpeg-dev libpng-dev \
libfaac-dev libmp3lame-dev libtheora-dev \
libopencore-amrnb-dev libopencore-amrwb-dev \
libopenblas-dev libatlas-base-dev libblas-dev \
liblapack-dev liblapacke-dev libeigen3-dev gfortran \
libhdf5-dev libprotobuf-dev protobuf-compiler \
libgoogle-glog-dev libgflags-dev && \
apt-get clean && rm -rf /var/lib/apt/lists/*
# Install libavif
WORKDIR $HOME
RUN git clone https://github.com/AOMediaCodec/libavif.git \
&& cd libavif && mkdir build && cd build \
&& cmake .. -DCMAKE_BUILD_TYPE=Release -G Ninja \
&& ninja && ninja install \
&& cd $HOME \
&& rm -rf $HOME/libavif
# Install cuDNN
RUN wget https://developer.download.nvidia.com/compute/cudnn/redist/cudnn/linux-$(uname -m)/cudnn-linux-$(uname -m)-9.10.0.56_cuda12-archive.tar.xz \
&& tar -xvf cudnn-linux-$(uname -m)-9.10.0.56_cuda12-archive.tar.xz \
&& cd cudnn-linux-$(uname -m)-9.10.0.56_cuda12-archive \
&& cp include/cudnn*.h /usr/local/cuda/include \
&& cp lib/libcudnn* /usr/local/cuda/lib64 \
&& cd $HOME \
&& rm -rf $HOME/cudnn-linux-$(uname -m)-9.10.0.56_cuda12-archive \
&& rm -rf $HOME/cudnn-linux-$(uname -m)-9.10.0.56_cuda12-archive.tar.xz
# Clone OpenCV-Python
WORKDIR /opt
RUN git clone --recursive https://github.com/opencv/opencv-python.git
WORKDIR /opt/opencv-python
RUN git submodule update --init --recursive
# Fool git
RUN git config --global user.email "test@nonexistent.com" && \
git config --global user.name "Test User"
# Update NvEncoder.cpp for OpenCV and commit changes
RUN wget "https://raw.githubusercontent.com/opencv/opencv_contrib/refs/heads/4.x/modules/cudacodec/src/NvEncoder.cpp" -O opencv_contrib/modules/cudacodec/src/NvEncoder.cpp
WORKDIR /opt/opencv-python/opencv_contrib
RUN git add modules/cudacodec/src/NvEncoder.cpp && git commit -m "Update cudacodec submodule to latest version"
WORKDIR /opt/opencv-python
RUN git add opencv_contrib && git commit -m "Updated submodule pointer to latest custom version"
# Make sure to build for numpy>=2.0.0
RUN pip install --no-cache-dir -U pip && \
pip install --no-cache-dir -U "numpy>=2.0.0"
# Check NumPy version
RUN python3 -c "import numpy; print('NumPy for build:', numpy.__version__, '| Include:', numpy.get_include())"
# Set environment variables for OpenCV build
ENV ENABLE_CONTRIB=${OPENCV_CONTRIB}
ENV ENABLE_HEADLESS=1
ENV CFLAGS="--std=c++17"
ENV CXXFLAGS="--std=c++17"
ENV CMAKE_ARGS="-D CMAKE_BUILD_TYPE=Release \
-D WITH_OPENCL=ON \
-D CUDA_ARCH_BIN=${CUDA_COMPUTE_CAPABILITY} \
-D CUDA_ARCH_PTX=${CUDA_COMPUTE_CAPABILITY} \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D CUDNN_INCLUDE_DIR='/usr/local/cuda/include' \
-D CUDNN_LIBRARY='/usr/local/cuda/lib64/libcudnn.so' \
-D WITH_CUBLAS=ON \
-D WITH_AVIF=ON \
-D WITH_JPEG=ON \
-D BUILD_JPEG=OFF \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D CMAKE_CUDA_STANDARD=17 \
-D CMAKE_CUDA_STANDARD_REQUIRED=ON \
-D OPENCV_DNN_CUDA=ON \
-D WITH_QT=OFF \
-D WITH_VTK=OFF \
-D WITH_OPENMP=ON \
-D BUILD_TIFF=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TESTS=OFF \
-D WITH_EIGEN=ON \
-D WITH_PROTOBUF=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_EXAMPLES=OFF \
-D BUILD_opencv_java=OFF \
-D CMAKE_CXX_STANDARD=17 \
-D CMAKE_CXX_STANDARD_REQUIRED=ON \
-D CMAKE_VERBOSE_MAKEFILE=ON \
-D WITH_PYTHON=ON \
-D WITH_NUMPY=ON \
-D OPENCV_GAPI_GSTREAMER=ON \
-D WITH_GSTREAMER=ON"
# Install NVIDIA Video Codec SDK
COPY . /tmp
RUN if [ -d /tmp/video_codec_sdk ] && ls /tmp/video_codec_sdk/*.zip 1>/dev/null 2>&1; then \
mkdir -p /usr/local/cuda/include/ && \
unzip /tmp/video_codec_sdk/*.zip -d /tmp/video_codec_sdk && \
rm /tmp/video_codec_sdk/*.zip && \
cp -rf /tmp/video_codec_sdk/*/Lib/linux/stubs/$(uname -m)/*.so /usr/local/cuda/lib64/ && \
cp -rf /tmp/video_codec_sdk/*/Interface/*.h /usr/local/cuda/include/ && \
echo "NVIDIA Video Codec SDK installed successfully." && \
export CUDA_CODEC_ENABLED=1 ; \
else \
mkdir -p /tmp/video_codec_sdk && \
echo "No video_codec_sdk zip found, skipping NVIDIA Video Codec SDK installation." && \
export CUDA_CODEC_ENABLED=0 ; \
fi
# Ensure that the NVIDIA Video Codec SDK is available
ENV LD_LIBRARY_PATH=/usr/local/cuda/lib64/:${LD_LIBRARY_PATH:-}
RUN LIBCUDA=$(find / -name libcuda.so -print -quit) && \
if [ -f "$LIBCUDA" ]; then \
ln -sf "$LIBCUDA" /usr/local/cuda/lib64/libcuda.so && ldconfig; \
else \
echo "ERROR: libcuda.so not found!" && exit 1; \
fi
# Build OpenCV
RUN pip wheel . --verbose --wheel-dir /opt/opencv-install
### Final image ###
FROM --platform=${PLATFORM} nvcr.io/nvidia/l4t-base:${BASE_IMAGE_VERSION} AS base
LABEL org.opencontainers.image.authors="contact@appsolves.dev"
LABEL org.opencontainers.image.source=https://github.com/AppSolves/LanePilot
LABEL org.opencontainers.image.description="An image that contains and/or mounts CUDA, CUDNN, TensorRT, PyTorch, OpenCV (Python) and GStreamer runtime dependencies."
LABEL org.opencontainers.image.licenses=https://raw.githubusercontent.com/AppSolves/LanePilot/refs/heads/v2/LICENSE.md
# Set default python version to 3.10
ARG PYTHON_VERSION=3.10
ARG TORCH_INSTALL=https://developer.download.nvidia.cn/compute/redist/jp/v61/pytorch/torch-2.5.0a0+872d972e41.nv24.08.17622132-cp310-cp310-linux_aarch64.whl
# Update environment variables
ENV PATH=/usr/local/bin:${PATH:-}
ENV LD_LIBRARY_PATH=/hostlibs/all:/hostlibs/cuda/lib64/:/usr/local/cuda/lib64/:${LD_LIBRARY_PATH:-}
ENV PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH:-}
ENV PYTHONPATH=/usr/local/python:${PYTHONPATH:-}
# Update dependencies
RUN ldconfig && \
apt-get update -y --fix-missing && \
apt-get upgrade -y --fix-missing && \
apt-get install -y --no-install-recommends \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
gstreamer1.0-tools \
gstreamer1.0-x \
gstreamer1.0-alsa \
gstreamer1.0-gl \
gstreamer1.0-pulseaudio \
libhdf5-103 \
libgoogle-glog0v5 \
libtesseract4 \
libtbb12 \
libatlas3-base \
libyuv0 \
iproute2 \
gosu \
python3 python3-pip
# Copy NVIDIA Video Codec SDK
COPY --from=opencv-builder /tmp/video_codec_sdk /tmp/video_codec_sdk
RUN if [ -d /tmp/video_codec_sdk ] && ls /tmp/video_codec_sdk/*.so 1>/dev/null 2>&1; then \
mkdir -p /usr/local/cuda/include/ && \
mkdir -p /usr/local/cuda/lib64/ && \
cp -rf /tmp/video_codec_sdk/*/Lib/linux/stubs/$(uname -m)/*.so /usr/local/cuda/lib64/ && \
cp -rf /tmp/video_codec_sdk/*/Interface/*.h /usr/local/cuda/include/ && \
echo "NVIDIA Video Codec SDK installed successfully." && \
export CUDA_CODEC_ENABLED=1 ; \
else \
echo "No video_codec_sdk zip found, skipping NVIDIA Video Codec SDK installation." ; \
export CUDA_CODEC_ENABLED=0 ; \
fi
# Copy libavif
COPY --from=opencv-builder /usr/local/lib/libavif* /usr/local/lib/
# Copy OpenCV and python bindings
COPY --from=opencv-builder /opt/opencv-install /opt/opencv-install
WORKDIR /
RUN pip install --no-cache-dir -U pip && \
opencv_wheel=$(ls /opt/opencv-install/opencv_contrib_python_headless-*.whl | head -n1) && \
pip install --force-reinstall --upgrade --no-cache-dir "$opencv_wheel" && \
rm -rf /opt/opencv-install
# Install PyTorch
RUN pip install --no-cache-dir -U ${TORCH_INSTALL}
# Copy root files
COPY --from=root /LICENSE.md /LICENSE.md
# Clean up
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# Remove headers, static libs, pkgconfig, docs and other dev stuff to save space
RUN rm -rf /usr/local/include \
/usr/local/lib/*.a \
/usr/local/lib/*.la \
/usr/local/lib/*.cmake \
/usr/local/lib/pkgconfig \
/usr/local/lib/cmake \
/usr/local/share \
/usr/local/doc
# Strip binaries to reduce size
RUN find /usr/local -type f -executable ! -name '*.so*' -exec strip --strip-unneeded {} + 2>/dev/null || true
# Remove pip cache and __pycache__
RUN rm -rf /root/.cache /root/.pip /usr/local/lib/python*/dist-packages/*/__pycache__
# Set the default command to run when starting the container
CMD ["python3", "-c", "import cv2; print(cv2.getBuildInformation())"]