You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Looking at the above, this copies the image twice, once when converting from np.array to bytes, and another when reading into img_msg.data (which is an array.array).
@Interpause Did you happen to create an MR for this optimization? By the way I found a case where this is not working for me. If the img.data has datatype of np.float32 then this optimization is breaking:
>>> import numpy as np
>>> from array import array
>>> from timeit import timeit
>>> img = np.zeros((4320,7680,3), dtype=np.uint8)>>> timeit(lambda: array('B', []).frombytes(img.tobytes()), number=200)
12.889657152991276
>>> timeit(lambda: array('B', []).frombytes(img.data), number=200)
5.910853378998581
>>> img = np.zeros((4320,7680,3), dtype=np.float32)>>> timeit(lambda: array('B', []).frombytes(img.data), number=200)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.8/timeit.py", line 233, in timeit return Timer(stmt, setup, timer, globals).timeit(number) File "/usr/lib/python3.8/timeit.py", line 177, in timeit timing = self.inner(it, self.timer) File "<timeit-src>", line 6, in inner File "<stdin>", line 1, in <lambda>TypeError: a bytes-like object is required
Would you have any suggestions how to workaround that?
vision_opencv/cv_bridge/python/cv_bridge/core.py
Line 281 in 0b69de3
Looking at the above, this copies the image twice, once when converting from
np.array
tobytes
, and another when reading intoimg_msg.data
(which is anarray.array
).Referring to https://numpy.org/doc/stable/reference/generated/numpy.ndarray.data.html,
np.array.data
exposes thememoryview
of the array. By reading directly from it, it is possible to get a ~2x speed up, especially for large images (below tested using a 8k resolution image).I will write a pull request when I have time.
The text was updated successfully, but these errors were encountered: