[Python] cuda commands from python in opencv fail

Discussão em 'Python' iniciado por Stack, Agosto 27, 2025.

  1. Stack

    Stack Membro Participativo

    I've compiled opencv with cuda support and am trying to use some cuda functions - to that end I'm trying the following test:

    import cv2
    if cv2.cuda.getCudaEnabledDeviceCount() > 0:
    print("OpenCV was built with CUDA support.")
    else:
    print("OpenCV was not built with CUDA support.")
    build_info = cv2.getBuildInformation()
    #print(build_info)


    print("\n=== CPU flags ===")
    for line in build_info.splitlines():
    if "math" in line.lower() or "fast" in line.lower():
    print(line)
    print("\n=== CUDA section ===")
    for line in build_info.splitlines():
    if "CUDA" in line or "FAST_MATH" in line:
    print(line)



    # Check if a CUDA-enabled GPU is available
    if not cv2.cuda.getCudaEnabledDeviceCount():
    print("CUDA-enabled GPU not found.")
    # Fallback to a CPU implementation if needed
    exit()

    # Load an image using CPU

    # Load an image
    image = cv2.imread('/home/jeremy/Documents/drone_detect/laser_turret/scout_software/obsidian.jpg')
    if image is None:
    print("Error loading image.")
    exit()

    # Convert to GRAY on CPU (CUDA filter prefers 1 or 4 channels)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    print("gray:", gray.shape, gray.dtype, gray.flags['C_CONTIGUOUS'])
    # Upload to GPU
    gpu_image = cv2.cuda_GpuMat()
    gpu_image.upload(gray)
    assert isinstance(gpu_image, cv2.cuda_GpuMat)
    # Create and apply CUDA Gaussian (type = CV_8UC1)


    # sanity: uploaded image must be 8UC1 and non-empty
    assert isinstance(gpu_image, cv2.cuda_GpuMat) and not gpu_image.empty()
    assert gpu_image.type() == cv2.CV_8UC1, f"type={gpu_image.type()} (need CV_8UC1)"

    gaussian_filter = cv2.cuda.createGaussianFilter(cv2.CV_8UC1, cv2.CV_8UC1, (15, 15), 0)

    # some Python bindings require an explicit dst GpuMat
    dst_gpu = cv2.cuda_GpuMat()
    gaussian_filter.apply(gpu_image, dst_gpu)

    gpu_blurred_image = dst_gpu



    # Display or save the blurred image
    cv2.imshow('Original Image', image)
    cv2.imshow('Blurred Image', gpu_blurred_image.download()) # this is gray

    cv2.waitKey(0)
    cv2.destroyAllWindows()


    This invariably hits problems along the lines of

    OpenCV was built with CUDA support.
    === CPU flags ===
    C++ flags (Release): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG -DNDEBUG
    C++ flags (Debug): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wsuggest-override -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g -O0 -DDEBUG -D_DEBUG
    C flags (Release): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse3 -fvisibility=hidden -O3 -DNDEBUG -DNDEBUG
    C flags (Debug): -fsigned-char -ffast-math -fno-finite-math-only -W -Wall -Wreturn-type -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections -msse3 -fvisibility=hidden -g -O0 -DDEBUG -D_DEBUG
    Unavailable: alphamat cannops cvv fastcv hdf java julia matlab ovis python2 sfm viz
    NVIDIA CUDA: YES (ver 12.9, CUFFT CUBLAS FAST_MATH)

    === CUDA section ===
    NVIDIA CUDA: YES (ver 12.9, CUFFT CUBLAS FAST_MATH)
    gray: (1451, 2194) uint8 True
    Traceback (most recent call last):
    File "<frozen runpy>", line 198, in _run_module_as_main
    File "<frozen runpy>", line 88, in _run_code
    File "/home/jeremy/Documents/drone_detect/laser_turret/shared/verify_opencv_cuda.py", line 53, in <module>
    gaussian_filter.apply(gpu_image, dst_gpu)
    cv2.error: OpenCV(4.13.0-dev) :-1: error: (-5:Bad argument) in function 'apply'
    > Overload resolution failed:
    > - src is not a numpy array, neither a scalar
    > - Expected Ptr<cv::cuda::GpuMat> for argument 'src'
    > - Expected Ptr<cv::UMat> for argument 'src'


    So what is going wrong here - I have an nividia A1000 gpu and compiled opencv using the following cmake command:

    cmake -G "Unix Makefiles"
    -D CMAKE_BUILD_TYPE=Release
    -D CMAKE_INSTALL_PREFIX=/usr/local
    -D OPENCV_EXTRA_MODULES_PATH=$HOME/opencv_contrib/modules
    -D BUILD_opencv_highgui=ON
    -D WITH_GTK=ON
    -D WITH_OPENGL=ON
    -D WITH_CUDA=ON
    -D WITH_CUDNN=ON
    -D WITH_CUBLAS=ON
    -D OPENCV_DNN_CUDA=ON
    -D CUDA_ARCH_BIN=8.6
    -D CUDAToolkit_ROOT=/usr/local/cuda
    -D BUILD_opencv_cudacodec=ON
    -D WITH_NVCUVENC=OFF
    -D BUILD_EXAMPLES=ON
    -D BUILD_PYTHON_SUPPORT=ON
    -D INSTALL_PYTHON_EXAMPLES=ON
    -D BUILD_TESTS=ON
    -D BUILD_PERF_TESTS=OFF
    -D BUILD_opencv_python3=ON
    -D Python3_EXECUTABLE="$PYEXE"
    -D Python3_NumPy_INCLUDE_DIRS="$NUMPY_INC"
    -D OPENCV_PYTHON3_INSTALL_PATH=/home/jeremy/Documents/drone_detect/myenv/lib/python3.12/site-packages
    -D OPENCV_PYTHON3_LIMITED_API=OFF
    -D CUDA_FAST_MATH=ON
    -D ENABLE_FAST_MATH=ON ..

    Continue reading...

Compartilhe esta Página