# cv2.error: OpenCV(4.7.0) /modules/dnn/src/dnn.cpp: 错误: (-2:未指定错误) DNN 模块未使用 CUDA 后端构建；已切换到 CPU

- **ID:** `opencv/dnn-cuda-backend-missing`
- **领域:** opencv
- **类别:** config_error
- **错误码:** `-2`
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

OpenCV 安装未包含 DNN 的 CUDA 支持，因此 GPU 加速不可用，回退到 CPU。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 4.5.0 | active | — | — |
| 4.6.0 | active | — | — |
| 4.7.0 | active | — | — |
| 4.8.0 | active | — | — |
| 4.9.0 | active | — | — |

## 解决方案

1. ```
   Rebuild OpenCV from source with CUDA enabled: set -DWITH_CUDA=ON and -DWITH_CUDNN=ON in CMake, and ensure CUDA toolkit and cuDNN are installed.
   ```
2. ```
   Use a pre-built OpenCV package that includes CUDA support, such as from conda-forge (opencv with cuda variant) or NVIDIA's official Docker images.
   ```

## 无效尝试

- **Set environment variable OPENCV_DNN_CUDA=1** — This environment variable does not exist; CUDA support must be compiled into OpenCV. (95% 失败率)
- **Install CUDA toolkit after OpenCV installation** — CUDA toolkit must be present at build time; installing it after does not add CUDA support to an existing OpenCV build. (85% 失败率)
- **Use cv2.dnn.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA) to force CUDA** — If the DNN module was not built with CUDA, this call will throw the same error; it does not enable CUDA at runtime. (90% 失败率)
