AI, Deep Learning Basics/Computer Vision

Build OpenCV with CUDA in virtual environment

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.

 

How to build OpenCV with Cuda and cuDNN support in Windows - 2023 - Machine Learning Projects

Hey guys, in this blog we will see how we can build OpenCV with Cuda and cuDNN support in Windows. This process can be very tricky and tedious if doing it for

machinelearningprojects.net

 

How to install OpenCV 4.2.0 with CUDA 10.1 on Ubuntu 20.04 LTS (Focal Fossa)

Recently we were doing a project on computer vision where we needed to use OpenCV with CUDA. Now in order to do so we needed to download…

medium.com

 

OpenCV + CUDA 직접 빌드하기 (Windows/Linux 종합)

최근에 opencv에 있는 dnn을 한번 써보려고 직접 소스를 받아서 빌드(build)해 보았다. 역시나 엄청난 삽질의 연속이고 할 때마다 이것 저것 해결책을 검색하느라 많은 시간을 소모한다 (삽질은 누구

darkpgmr.tistory.com