# How to force cv2 to use second GPU in Python for dnn

**URL:** <https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409>\
**Category:** Python\
**Tags:** cuda, dnn\
**Created:** [February 8, 2021, 2:12am UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409 "2021-02-08T02:12:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![overc710](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/overc710/32/941_2.png) [@overc710](https://forum.opencv.org/u/overc710)\
**Post date:** [February 8, 2021, 2:12am UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/1 "2021-02-08T02:12:50Z")

</div>

I’m trying to get mobilenet detectors working on multiple cameras on my local network, with inference running on two GPUs. I searched around a bit and found out you can select a specific GPU using cv2.cuda.SetDevice(gpuid), but that didn’t work for me in OpenCV 4.5. Is this command deprecated/obsolete?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 8, 2021, 2:44am UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/2 "2021-02-08T02:44:04Z")

</div>

welcome.

please explain “didn’t work”.

---

<div class="post-metadata">

**Author:** ![overc710](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/overc710/32/941_2.png) [@overc710](https://forum.opencv.org/u/overc710)\
**Post date:** [February 8, 2021, 3:16am UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/3 "2021-02-08T03:16:09Z")

</div>

It gave me an error saying the command didn’t exist, that SetDevice wasn’t a method for the cv2.cuda class.

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [February 8, 2021, 9:51am UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/4 "2021-02-08T09:51:26Z")

</div>

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

```auto
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'

```

---

<div class="post-metadata">

**Author:** ![overc710](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/overc710/32/941_2.png) [@overc710](https://forum.opencv.org/u/overc710)\
**Post date:** [February 8, 2021, 1:00pm UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/5 "2021-02-08T13:00:36Z")

</div>

I just reran the program with `cv2.cuda.setDevice(1)` instead of `cv2.cuda.SetDevice(1)`, and it ran. I think the guide that I read had the capitalization wrong. Sorry for the dumb mistake. My best guess as to why device 1 didn’t work for you but worked for me is because you might not have a second GPU.

---

<div class="post-metadata">

**Author:** ![overc710](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/overc710/32/941_2.png) [@overc710](https://forum.opencv.org/u/overc710)\
**Post date:** [February 8, 2021, 1:01pm UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/6 "2021-02-08T13:01:10Z")

</div>

Also, I’m new, how do you insert blocks of code like you did?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 8, 2021, 4:29pm UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/7 "2021-02-08T16:29:57Z")

</div>

three backticks for a block:

````
```
generic code here
```

```python
python code here. try other languages too
```

````

single backticks for `inline` code

```
single backticks for `inline` code

```

or indent by 4 spaces for monospaced text without highlighting. toolbar above text entry box has a button for that.

---

<div class="post-metadata">

**Author:** ![overc710](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/overc710/32/941_2.png) [@overc710](https://forum.opencv.org/u/overc710)\
**Post date:** [February 8, 2021, 6:17pm UTC](https://forum.opencv.org/t/how-to-force-cv2-to-use-second-gpu-in-python-for-dnn/1409/8 "2021-02-08T18:17:01Z")

</div>

Thanks!

```python
cv2.cuda.setDevice(1)

```
