1

I am trying to make my code faster by using Numba and vectorizing. My code is to get input from two 360 deg cameras and crop them. The main idea here is to learn how to use Numba and vectorizing. Below is my code:

import cv2
import time
from numba import vectorize
import numba 

dim = 1440

@vectorize(['int[:, :, 3](int[:, :, 3])'], target='cuda')
def cropFaster(img):
        croppedImG = img[:,10:1100]
        return croppedImG

cap1 = cv2.VideoCapture(0)  # Set input device number
cap1.set(3, dim)  # Set Horizontal resolution
cap1.set(4, dim)  # Set Vertical resolution

cap2 = cv2.VideoCapture(1)  # Set input device number
cap2.set(3, dim)  # Set Horizontal resolution
cap2.set(4, dim)  # Set Vertical resolution

while (cap1.isOpened()):
        t1 = time.clock()
        ret1, img1 = cap1.read()
        ret2, img2 = cap2.read()
        croppedImgL =  cropFaster(img1) 
        croppedImgR =  cropFaster(img2)
        t2 = time.clock() - t1
        print (t2)
        cv2.imshow("Left", croppedImgL)
        cv2.imshow("Right", croppedImgR)
        if cv2.waitKey(1) & 0xFF == ord('q'):
                break 
cap1.release()

I get the following error:

Traceback (most recent call last):
  File "transparentAPI2.py", line 9, in <module>
    @vectorize(['int[:, :, 3](int[:, :, 3])'], target='cuda')
  File "/usr/local/lib/python3.6/dist-packages/numba/npyufunc/decorators.py", line 120, in wrap
    vec.add(sig)
  File "/usr/local/lib/python3.6/dist-packages/numba/npyufunc/deviceufunc.py", line 391, in add
    args, return_type = sigutils.normalize_signature(sig)
  File "/usr/local/lib/python3.6/dist-packages/numba/sigutils.py", line 26, in normalize_signature
    parsed = _parse_signature_string(sig)
  File "/usr/local/lib/python3.6/dist-packages/numba/sigutils.py", line 16, in _parse_signature_string
    return eval(signature_str, {}, types.__dict__)
  File "<string>", line 1, in <module>
TypeError: 'type' object is not subscriptable

Any idea how to fix this? Thanks

1 Answer 1

1

If you haven't found it by now, the exception comes from the signature. You need to explicitate not only the type but also the byte-size of each argument. So int is not acceptable, but int8 or int16 int32 or int64. However, this is not the only thing that is wrong with this function:

  • If you read the documentation, vectorize is only supposed to be used when the arguments are numbers, not arrays. What you are looking for in these cases is guvectorize, whereby the inputs and outputs can be arrays of various dimensions.
  • You can also not provide a number as array dimension in the signature, you must use :.
  • It is unlikely you will see any significant speed up using numba in this case, as you are only using numpy indexing inside the function. Numpy is already compiled C in the background.

I can't give you a working example because with guvectorize you also need to provide a numpy-style signature, and this doesn't work when the shape of the output does not relate to the shape of the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.