thecanadianroot / opencv-cuda-docker

GNU General Public License v3.0
24 stars 8 forks source link

ModuleNotFoundError: No module named 'cv2' #7

Open WesleyMNA opened 2 years ago

WesleyMNA commented 2 years ago

I am getting ModuleNotFoundError while trying to import cv2 in Python.

Do I need to add any extra config?

thecanadianroot commented 2 years ago

I think you just need to install the OpenCV Python package, the image doesn't do it by default because it supports multiple programming languages. Did you ran pip install opencv-python before trying to run your Python script?

albertotb commented 2 years ago

It did not work for me. This is what I tried:

I get 0, instead of 1, so does not seem to be support for GPU. To make OpenCV work I think you need to compile it with the Python option and them simlink Python to the appropiate direction. I will try to take a look and see if I can fix it.

manozaig commented 2 years ago

I could make it show 1 when executing cv2.cuda.getCudaEnabledDeviceCount() method. Although when I try to use another method, like GPU frame processing, I get this error:

"OpenCV (-217:Gpu API call) no kernel image is available for execution on the device in function 'call_resize_linear_glob'"

I'm running on an AWS EC2 g4dn instance that runs a Tesla T4 GPU with 7.5 CUDA capability.

@albertotb Did you manage to solve this symlink problem?

albertotb commented 2 years ago

@manozaig Yes, based on this repo, which is great, I created a custom Dockerfile that compiles OpenCV from source with CUDA support and installs Python bindings to system Python. This is what ended up working for me:

ARG CUDA="11.6.0"
ARG UBUNTU="20.04"

FROM nvidia/cuda:${CUDA}-cudnn8-devel-ubuntu${UBUNTU}

ARG PYTHON="3.8"
ARG OPENCV="4.6.0"
ARG CUDA_ARCH_BIN="5.0 5.2 5.3 6.0 6.1 6.2 7.0 7.2 7.5 8.0 8.6"
ARG CUDNN="ON"
ARG DEBIAN_FRONTEND=noninteractive

ENV PIP_ROOT_USER_ACTION=ignore

# In Ubuntu 20.04, this installs Python 3.8
# In Ubuntu 22.04, this installs Python 3.10
# From: https://github.com/Devtography/python-cuda-docker/blob/master/Dockerfile
RUN apt update \
    && apt install -y --no-install-recommends python3 python3-pip python3-numpy python3-dev \
    && ln -sf python3 /usr/bin/python \
    && ln -sf pip3 /usr/bin/pip \
    && pip install --upgrade pip \
    && pip install wheel setuptools

# Install OpenCV dependencies
RUN apt update && apt install -y --no-install-recommends gcc-10 \
    g++-10 \
    build-essential \ 
    ninja-build \
    cmake \ 
    wget \
    unzip \
    pkg-config \
    yasm \ 
    git \
    checkinstall \
    libjpeg-dev \
    libpng-dev \
    libtiff-dev \
    libavcodec-dev \ 
    libavformat-dev \
    libswscale-dev \ 
    libxvidcore-dev x264 \
    libx264-dev \
    libfaac-dev \
    libmp3lame-dev \
    libtheora-dev \ 
    libfaac-dev \
    libmp3lame-dev \
    libvorbis-dev \
    libxine2-dev \
    libv4l-dev \
    v4l-utils

# Download and build OpenCV with CUDA support
# From: https://github.com/thecanadianroot/opencv-cuda-docker/blob/main/Dockerfile
WORKDIR /tmp
RUN wget -q https://github.com/opencv/opencv/archive/refs/tags/${OPENCV}.zip && unzip ${OPENCV}.zip && rm ${OPENCV}.zip
RUN wget -q https://github.com/opencv/opencv_contrib/archive/refs/tags/${OPENCV}.zip && unzip ${OPENCV}.zip && rm ${OPENCV}.zip
RUN mkdir opencv-${OPENCV}/build && \
    cd opencv-${OPENCV}/build && \
    cmake -GNinja -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D WITH_TBB=ON \
    -D ENABLE_FAST_MATH=1 \
    -D CUDA_FAST_MATH=1 \
    -D WITH_CUBLAS=1 \
    -D WITH_CUDA=ON \
    -D BUILD_opencv_cudacodec=OFF \
    -D WITH_V4L=ON \
    -D WITH_QT=OFF \
    -D WITH_OPENGL=ON \
    -D WITH_GSTREAMER=ON \
    -D OPENCV_GENERATE_PKGCONFIG=ON \
    -D OPENCV_PC_FILE_NAME=opencv.pc \
    -D OPENCV_ENABLE_NONFREE=ON \
    -D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib-${OPENCV}/modules \
    -D WITH_CUDNN=${CUDNN} \
    -D OPENCV_DNN_CUDA=${CUDNN} \
    -D BUILD_JAVA=OFF \
    -D BUILD_opencv_python2=OFF \
    -D BUILD_opencv_python3=ON \
    -D HAVE_opencv_python3=ON \
    -D CUDA_ARCH_BIN=${CUDA_ARCH_BIN} \
    -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python \
    -D PYTHON3_EXECUTABLE=/usr/bin/python \
    -D PYTHON3_PACKAGES_PATH=/usr/local/lib/python${PYTHON}/dist-packages/ \
    -D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python${PYTHON}/dist-packages/ \
    -D INSTALL_PYTHON_EXAMPLES=OFF \
    -D INSTALL_C_EXAMPLES=OFF \
    -D BUILD_EXAMPLES=OFF \
    .. && \
    ninja && \
    ninja install && \
    ldconfig

The key compilation flags are, so feel free to modify the others to your liking:

-D BUILD_opencv_python3=ON \
-D HAVE_opencv_python3=ON \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python \
-D PYTHON3_EXECUTABLE=/usr/bin/python \
-D PYTHON3_PACKAGES_PATH=/usr/local/lib/python${PYTHON}/dist-packages/ \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python${PYTHON}/dist-packages/ \
manozaig commented 2 years ago

That's awesome, thanks a lot @albertotb!

thecanadianroot commented 2 years ago

It did not work for me. This is what I tried:

  • Run apt-get install python3-pip
  • pip install opencv-python
  • Run python3 -c "import cv2; print(cv2.cuda.getCudaEnabledDeviceCount())"

I get 0, instead of 1, so does not seem to be support for GPU. To make OpenCV work I think you need to compile it with the Python option and them simlink Python to the appropiate direction. I will try to take a look and see if I can fix it.

Sorry for my late response, I think you should be able to set the default python version with update-alternatives --set python /usr/bin/python3. I'll try to fix this the sooner possible and add more build images.