fix: use gpu

This commit is contained in:
ziesorx 2025-09-25 22:59:55 +07:00
parent 5f29392c2f
commit 6bb679f4d8
5 changed files with 533 additions and 84 deletions

View file

@ -1,54 +1,166 @@
# Base image with all ML dependencies and NVIDIA Video Codec SDK
# Base image with complete ML and hardware acceleration stack
FROM pytorch/pytorch:2.8.0-cuda12.6-cudnn9-runtime
# Install system dependencies including GStreamer with NVDEC support
RUN apt update && apt install -y \
# Install build dependencies and system libraries
RUN apt-get update && apt-get install -y \
# Build tools
build-essential \
cmake \
git \
pkg-config \
wget \
unzip \
yasm \
nasm \
# System libraries
libgl1 \
libglib2.0-0 \
libgtk-3-0 \
libgomp1 \
# GStreamer base
libgstreamer1.0-0 \
libgstreamer-plugins-base1.0-0 \
libgstreamer-plugins-bad1.0-0 \
# Media libraries for FFmpeg build
libjpeg-dev \
libpng-dev \
libtiff-dev \
libx264-dev \
libx265-dev \
libvpx-dev \
libfdk-aac-dev \
libmp3lame-dev \
libopus-dev \
libv4l-dev \
libxvidcore-dev \
libdc1394-22-dev \
# TurboJPEG for fast JPEG encoding
libturbojpeg0-dev \
# GStreamer complete stack
libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev \
libgstreamer-plugins-bad1.0-dev \
gstreamer1.0-tools \
gstreamer1.0-plugins-base \
gstreamer1.0-plugins-good \
gstreamer1.0-plugins-bad \
gstreamer1.0-plugins-ugly \
gstreamer1.0-libav \
# GStreamer Python bindings
python3-gst-1.0 \
# NVIDIA specific GStreamer plugins for hardware acceleration
gstreamer1.0-vaapi \
# FFmpeg with hardware acceleration support
ffmpeg \
libavcodec-extra \
libavformat58 \
libswscale5 \
# TurboJPEG for fast JPEG encoding
libturbojpeg0-dev \
python3-gst-1.0 \
# Python development
python3-dev \
python3-numpy \
# NVIDIA driver components
libnvidia-encode-535 \
libnvidia-decode-535 \
&& rm -rf /var/lib/apt/lists/*
# Install NVIDIA DeepStream (includes hardware accelerated GStreamer plugins)
# This provides nvv4l2decoder, nvvideoconvert, etc.
RUN apt update && apt install -y \
wget \
software-properties-common \
&& wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.0-1_all.deb \
&& dpkg -i cuda-keyring_1.0-1_all.deb \
&& apt update \
&& apt install -y libnvidia-decode-535 \
&& rm -rf /var/lib/apt/lists/* cuda-keyring_1.0-1_all.deb
# Install NVIDIA Video Codec SDK headers
RUN cd /tmp && \
wget https://github.com/FFmpeg/nv-codec-headers/archive/refs/tags/n12.1.14.0.zip && \
unzip n12.1.14.0.zip && \
cd nv-codec-headers-n12.1.14.0 && \
make install && \
rm -rf /tmp/*
# Set environment variables for hardware acceleration
ENV OPENCV_FFMPEG_CAPTURE_OPTIONS="video_codec;h264_cuvid"
# Build FFmpeg from source with full NVIDIA hardware acceleration
ENV FFMPEG_VERSION=6.0
RUN cd /tmp && \
wget https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.xz && \
tar xf ffmpeg-${FFMPEG_VERSION}.tar.xz && \
cd ffmpeg-${FFMPEG_VERSION} && \
./configure \
--enable-gpl \
--enable-nonfree \
--enable-libx264 \
--enable-libx265 \
--enable-libvpx \
--enable-libfdk-aac \
--enable-libmp3lame \
--enable-libopus \
--enable-cuda-nvcc \
--enable-cuvid \
--enable-nvenc \
--enable-nvdec \
--enable-cuda-llvm \
--enable-libnpp \
--extra-cflags=-I/usr/local/cuda/include \
--extra-ldflags=-L/usr/local/cuda/lib64 \
--nvccflags="-gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_89,code=sm_89 -gencode arch=compute_90,code=sm_90" && \
make -j$(nproc) && \
make install && \
ldconfig && \
cd / && rm -rf /tmp/*
# Build OpenCV from source with custom FFmpeg and full CUDA support
ENV OPENCV_VERSION=4.8.1
RUN cd /tmp && \
wget -O opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \
unzip opencv.zip && \
unzip opencv_contrib.zip && \
cd opencv-${OPENCV_VERSION} && \
mkdir build && cd build && \
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_CUDA=ON \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_CUBLAS=ON \
-D WITH_NVCUVID=ON \
-D WITH_CUVID=ON \
-D BUILD_opencv_cudacodec=ON \
-D WITH_FFMPEG=ON \
-D WITH_GSTREAMER=ON \
-D WITH_LIBV4L=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib-${OPENCV_VERSION}/modules \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D PYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON_LIBRARY=$(python3 -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
.. && \
make -j$(nproc) && \
make install && \
ldconfig && \
cd / && rm -rf /tmp/*
# Set environment variables for maximum hardware acceleration
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/lib:${LD_LIBRARY_PATH}"
ENV PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}"
ENV PYTHONPATH="/usr/local/lib/python3.10/dist-packages:${PYTHONPATH}"
ENV GST_PLUGIN_PATH="/usr/lib/x86_64-linux-gnu/gstreamer-1.0"
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:${LD_LIBRARY_PATH}"
# Copy and install base requirements (ML dependencies that rarely change)
# Optimized environment variables for hardware acceleration
ENV OPENCV_FFMPEG_CAPTURE_OPTIONS="rtsp_transport;tcp|hwaccel;cuda|hwaccel_device;0|video_codec;h264_cuvid|hwaccel_output_format;cuda"
ENV OPENCV_FFMPEG_WRITER_OPTIONS="video_codec;h264_nvenc|preset;fast|tune;zerolatency|gpu;0"
ENV CUDA_VISIBLE_DEVICES=0
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=compute,video,utility
# Copy and install base requirements (exclude opencv-python since we built from source)
COPY requirements.base.txt .
RUN pip install --no-cache-dir -r requirements.base.txt
RUN grep -v opencv-python requirements.base.txt > requirements.tmp && \
mv requirements.tmp requirements.base.txt && \
pip install --no-cache-dir -r requirements.base.txt
# Verify complete hardware acceleration setup
RUN echo "=== Hardware Acceleration Verification ===" && \
echo "FFmpeg Hardware Accelerators:" && \
ffmpeg -hide_banner -hwaccels 2>/dev/null | head -10 && \
echo "FFmpeg NVIDIA Decoders:" && \
ffmpeg -hide_banner -decoders 2>/dev/null | grep -E "(cuvid|nvdec)" | head -5 && \
echo "FFmpeg NVIDIA Encoders:" && \
ffmpeg -hide_banner -encoders 2>/dev/null | grep nvenc | head -5 && \
echo "OpenCV Configuration:" && \
python3 -c "import cv2; print('OpenCV version:', cv2.__version__); print('CUDA devices:', cv2.cuda.getCudaEnabledDeviceCount()); build_info = cv2.getBuildInformation(); print('CUDA support:', 'CUDA' in build_info); print('CUVID support:', 'CUVID' in build_info); print('FFmpeg support:', 'FFMPEG' in build_info); print('GStreamer support:', 'GStreamer' in build_info)" && \
echo "GStreamer NVIDIA Plugins:" && \
gst-inspect-1.0 2>/dev/null | grep -E "(nvv4l2|nvvideo)" | head -5 || echo "GStreamer NVIDIA plugins not detected" && \
echo "=== Verification Complete ==="
# Set working directory
WORKDIR /app