Have you compiled OpenCV with CUDA?
I just tried
cv2.cuda.setDevice(0)
with no errors and when I tried device 1 which doesn’t exist I get the below error informing me of that
OpenCV(4.5.0) Error: Gpu API call (invalid device ordinal) in cv::cuda::setDevice, file modules\core\src\cuda_info.cpp, line 73
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.5.0) \modules\core\src\cuda_info.cpp:73: error: (-217:Gpu API call) invalid device ordinal in function 'cv::cuda::setDevice'