How to catch opencl error in python?

asked 2019-03-02 11:20:21 -0600

LBerger gravatar image

I try to catch opencl error in python (windows 10 vs 2017):

import numpy as np
import cv2 as cv

img = cv.imread("g:/lib/opencv/samples/data/lena.jpg")
if img is None:
    print('problem')
    exit()
while True:
    cv.imshow("test",img)
    try:
        cv.cvtColor([],cv.COLOR_BGR2GRAY)
    except cv.error:
        print ("cvtColor erreur")
    except TypeError:
        print ("cvtColor type erreur")
    code = cv.waitKey(500)
    if code == 27:
        break
print("OK")
print("Try OpenCL error CL_MEM_OBJECT_ALLOCATION_FAILURE ") 
while True:
    cv.imshow("test",img)
    try:
        umat = cv.UMat(255 * np.ones((30000,30000,3),np.uint8))
        um = cv.cvtColor(umat,cv.COLOR_BGR2GRAY)
    except cv.error:
        print ("cvtColor erreur")
    except TypeError:
        print ("cvtColor type erreur")
    except :
         print ("*****autre  erreur******")
    code = cv.waitKey(500)
    if code == 27:
        break
print("OK")

Result is :

cvtColor type erreur
cvtColor type erreur
OK
Try OpenCL error CL_MEM_OBJECT_ALLOCATION_FAILURE
OpenCL error CL_MEM_OBJECT_ALLOCATION_FAILURE (-4) during call: clEnqueueNDRangeKernel('RGB2Gray', dims=2, globalsize=30208x30000x1, localsize=NULL) sync=false
OpenCV: terminate handler is called! The last OpenCV error is:
OpenCV(4.0.1-dev) Error: Assertion failed (u->refcount == 0 && "UMat deallocation error: some derived Mat is still alive") in cv::ocl::OpenCLAllocator::deallocate, file G:\Lib\opencv\modules\core\src\ocl.cpp, line 4797
 press a key to continue

It means that I can catch cv.error but I am not able to catch opencl error. Python script stops and return to system. Any idea to solve this?

edit retag flag offensive close merge delete