Jump to content

Is there a way to set an OpenCL device in OpenCV with Python?

yigitayaz262

How can I set a specific device for OpenCL to use in OpenCV in Python 3? When i run this its using Intel UHD graphics.

 

import cv2
import numpy as np
import time

video = cv2.VideoCapture(0)
boyut = 320
ClassDosyasi = 'coco.names'
ClassIsimleri = []
minKararlilik = 0.5
nmsTreshold = 0.3

with open(ClassDosyasi, 'rt') as f:
    ClassIsimleri = f.read().rstrip('\n').rsplit('\n')

modelConf = 'yolov3-320.cfg'
modelWeight = 'yolov3-320.weights'
net = cv2.dnn.readNetFromDarknet(modelConf, modelWeight)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_OPENCL_FP16)


def objeBulucu(ciktilar, frame):
    yukseklik, genislik, channels = frame.shape
    bbox = []
    classIDs = []
    confs = []
    for cikti in ciktilar:
        for detection in cikti:
            scores = detection[5:]
            classID = np.argmax(scores)
            kararlilik = scores[classID]
            if kararlilik > minKararlilik:
                w, h = int(detection[2] * genislik), int(detection[3] * yukseklik)
                x, y = int(detection[0] * genislik) - w / 2, int((detection[1] * yukseklik) - h / 2)
                bbox.append([x, y, w, h])
                classIDs.append(classID)
                confs.append(float(kararlilik))
    indices = cv2.dnn.NMSBoxes(bbox, confs, minKararlilik, nmsTreshold)
    for i in indices:
        i = i[0]
        box = bbox[i]
        x, y, w, h = box[0], box[1], box[2], box[3]
        endX = int(x + w)
        endY = int(y + h)
        cv2.rectangle(frame, (int(x), int(y)), (endX, endY), (0, 255, 0), 2)
        cv2.putText(frame, f'{ClassIsimleri[classIDs[i]].upper()} {int(confs[i] * 100)}%',
                    (int(x), int(y) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)


while True:
    start_time = time.time()
    basari, frame = video.read()

    blob = cv2.dnn.blobFromImage(frame, 1 / 255, (boyut, boyut), [0, 0, 0], 1, crop=False)
    net.setInput(blob)
    layerIsimleri = net.getLayerNames()
    cikisIsimleri = [layerIsimleri[i[0] - 1] for i in net.getUnconnectedOutLayers()]
    ciktilar = net.forward(cikisIsimleri)
    objeBulucu(ciktilar, frame)
    cv2.putText(frame, "FPS: " + str(int(1.0 / (time.time() - start_time))), (5, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8,
                (0, 0, 255),2)
    cv2.imshow('Sonuc', frame)
    cv2.waitKey(1)

 

Link to comment
Share on other sites

Link to post
Share on other sites

@Sauronthanks made it but nothing changed it was 3 fps with Intel UHD and now 3 fps with gtx 1650. How can I speed up this?

Link to comment
Share on other sites

Link to post
Share on other sites

That GPU doesn't support CUDA either, so not a lot of options there for you.

Is your OpenCV version compiled with OpenCL support?

HAL9000: AMD Ryzen 9 3900x | Noctua NH-D15 chromax.black | 32 GB Corsair Vengeance LPX DDR4 3200 MHz | Asus X570 Prime Pro | ASUS TUF 3080 Ti | 1 TB Samsung 970 Evo Plus + 1 TB Crucial MX500 + 6 TB WD RED | Corsair HX1000 | be quiet Pure Base 500DX | LG 34UM95 34" 3440x1440

Hydrogen server: Intel i3-10100 | Cryorig M9i | 64 GB Crucial Ballistix 3200MHz DDR4 | Gigabyte B560M-DS3H | 33 TB of storage | Fractal Design Define R5 | unRAID 6.9.2

Carbon server: Fujitsu PRIMERGY RX100 S7p | Xeon E3-1230 v2 | 16 GB DDR3 ECC | 60 GB Corsair SSD & 250 GB Samsung 850 Pro | Intel i340-T4 | ESXi 6.5.1

Big Mac cluster: 2x Raspberry Pi 2 Model B | 1x Raspberry Pi 3 Model B | 2x Raspberry Pi 3 Model B+

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, jj9987 said:

That GPU doesn't support CUDA either, so not a lot of options there for you.

Is your OpenCV version compiled with OpenCL support?

yes its working with opencl and using gpu. My gpu is gtx 1650 and cuda software installed.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, yigitayaz262 said:

@Sauronthanks made it but nothing changed it was 3 fps with Intel UHD and now 3 fps with gtx 1650. How can I speed up this?

are you certain it's actually using the gpu? kinda seems like it's using the cpu... try using DNN_TARGET_OPENCL instead of DNN_TARGET_OPENCL_FP16, the latter may reference a different env variable.

 

On a side note, why not use CUDA?

2 minutes ago, jj9987 said:

That GPU doesn't support CUDA either

Pretty sure all nvidia GPUs since 2008 support CUDA.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Sauron said:

are you certain it's actually using the gpu? kinda seems like it's using the cpu... try using DNN_TARGET_OPENCL instead of DNN_TARGET_OPENCL_FP16, the latter may reference a different env variable.

 

On a side note, why not use CUDA?

Pretty sure all nvidia GPUs since 2008 support CUDA.

When I use cuda it says

cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\dnn\src\dnn.cpp:1382: error: (-215:Assertion failed) preferableBackend != DNN_BACKEND_OPENCV || preferableTarget == DNN_TARGET_CPU || preferableTarget == DNN_TARGET_OPENCL || preferableTarget == DNN_TARGET_OPENCL_FP16 in function 'cv::dnn::dnn4_v20201117::Net::Impl::setUpNet'

[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn_it83\opencv\modules\videoio\src\cap_msmf.cpp (434) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback


The code again:

modelConf = 'yolov3-tiny.cfg'
modelWeight = 'yolov3-tiny.weights'
net = cv2.dnn.readNetFromDarknet(modelConf, modelWeight)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

 

Link to comment
Share on other sites

Link to post
Share on other sites

I think i need to build it with cuda enabled but idk how to make this to a python library

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, yigitayaz262 said:

When I use cuda it says

you're using a very recent version of opencv, cuda support isn't great for those right now... anyway did using DNN_TARGET_OPENCL make any difference?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

48 minutes ago, Sauron said:

you're using a very recent version of opencv, cuda support isn't great for those right now... anyway did using DNN_TARGET_OPENCL make any difference?

no same fps as cpu

Link to comment
Share on other sites

Link to post
Share on other sites

can i install older version of opencv and use it with cuda. This fps too important for me. Tried pip install opencv-python==4.0 but its not working

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, yigitayaz262 said:

no same fps as cpu

There could be a difference in the way windows handles this compared to Linux... have you tried ubuntu? Anyway here is a full tutorial on using CUDA with opencv in python https://www.pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/

 

Also if speed is really important for some reason you should take a look at MobileNetSSD, in my testing it's significantly faster than yolov3

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

40 minutes ago, Sauron said:

There could be a difference in the way windows handles this compared to Linux... have you tried ubuntu? Anyway here is a full tutorial on using CUDA with opencv in python https://www.pyimagesearch.com/2020/02/03/how-to-use-opencvs-dnn-module-with-nvidia-gpus-cuda-and-cudnn/

 

Also if speed is really important for some reason you should take a look at MobileNetSSD, in my testing it's significantly faster than yolov3

Thinking about windows ubuntu dualboot before this topic. External ssd i ordered will come in 3-4 days. Thanks for help. Bye

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Sauron said:

On a side note, why not use CUDA?

Pretty sure all nvidia GPUs since 2008 support CUDA.

It's not listed in the CUDA GPU list - https://developer.nvidia.com/cuda-gpus

HAL9000: AMD Ryzen 9 3900x | Noctua NH-D15 chromax.black | 32 GB Corsair Vengeance LPX DDR4 3200 MHz | Asus X570 Prime Pro | ASUS TUF 3080 Ti | 1 TB Samsung 970 Evo Plus + 1 TB Crucial MX500 + 6 TB WD RED | Corsair HX1000 | be quiet Pure Base 500DX | LG 34UM95 34" 3440x1440

Hydrogen server: Intel i3-10100 | Cryorig M9i | 64 GB Crucial Ballistix 3200MHz DDR4 | Gigabyte B560M-DS3H | 33 TB of storage | Fractal Design Define R5 | unRAID 6.9.2

Carbon server: Fujitsu PRIMERGY RX100 S7p | Xeon E3-1230 v2 | 16 GB DDR3 ECC | 60 GB Corsair SSD & 250 GB Samsung 850 Pro | Intel i340-T4 | ESXi 6.5.1

Big Mac cluster: 2x Raspberry Pi 2 Model B | 1x Raspberry Pi 3 Model B | 2x Raspberry Pi 3 Model B+

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, jj9987 said:

It's not listed in the CUDA GPU list - https://developer.nvidia.com/cuda-gpus

Then that list is incomplete. Considering it runs a Turing chip (minus raytracing) perhaps it was a marketing decision to push the sale of the more expensive 20xx variants.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×