To make OpenCV work with CUDA support in a specific conda environment, you need to build OpenCV from source within that environment. Here's a step-by-step guide:
1. Setup Your Conda Environment:
If you haven't created the environment yet:
```bash
conda create --name [YOUR ENV NAME] python=3.8
```
Activate your environment:
```bash
conda activate [YOUR ENV NAME]
```
2. Install Dependencies:
Within the activated environment, install required packages:
```bash
conda install numpy cmake gcc g++ git
```
3. Download OpenCV and OpenCV_contrib:
You'll need both the main OpenCV source and the additional modules from OpenCV_contrib:
```bash
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
```
4. Build OpenCV with CUDA Support:
Navigate to the OpenCV directory and create a build folder:
```bash
cd opencv
mkdir build && cd build
```
Run the CMake command. Make sure you point to your specific Python interpreter and libraries within the conda environment:
```bash
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") \
-D PYTHON_EXECUTABLE=$(which python) \
-D PYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D WITH_CUDA=ON \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
..
```
Now, compile and install:
```bash
make -j$(nproc)
make install
```
5. Verify Installation:
Start a Python interpreter within the `[YOUR ENV NAME]` environment:
```bash
python
```
Check if OpenCV is available with CUDA support:
```python
import cv2
print(cv2.__version__)
print(cv2.cuda.getCudaEnabledDeviceCount())
```
If everything worked correctly, you should see the version of OpenCV you installed and a number greater than zero (indicating the number of CUDA-enabled devices).
Note:
Building OpenCV with CUDA can sometimes be tricky due to various system configurations, CUDA versions, GPU types, etc. Always keep an eye on the CMake configuration output to make sure CUDA-related configurations are correctly detected. If you run into issues, refer to OpenCV's official documentation or online forums for help specific to your problem.
- https://gist.github.com/raulqf/f42c718a658cddc16f9df07ecc627be7
- https://darkpgmr.tistory.com/184
- https://medium.com/@sb.jaduniv/how-to-install-opencv-4-2-0-with-cuda-10-1-on-ubuntu-20-04-lts-focal-fossa-bdc034109df3
- https://machinelearningprojects.net/build-opencv-with-cuda-and-cudnn/