[英]How to I transfer an image(opencv Matrix/numpy array) from c++ publisher to python sender via ZeroMQ?
[英]Transfer OpenGL image on GPU from C++ to Python for deep learning
我在 C++ 中使用 pybind11 接口构建了一个模拟器,以使用 PyTorch 在 Python 中运行深度学习。 在每个时间步,我使用 SFML 库(openGL 的包装器)从模拟器的场景中绘制某些东西。 我在纹理上绘制它,然后从该纹理中获取像素,如下所示:
glBindTexture(GL_TEXTURE_2D, imageTexture.getTexture().getNativeHandle());
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data());
然后我使用 pybind11 接口将像素向量img
从 C++ 移动到 Python 。 问题是这种 GPU 到 CPU 的操作非常慢。 由于 Python 中的向量随后被转移回 GPU 以进行快速深度学习 (CNN) 操作,因此我想知道如何避免该步骤。
My best guess so far is that I should at each step bind the texture in C++ (as in the code above), then right after that in the Python get the bound texture using CUDA, while keeping it on the GPU. 但是我不知道该怎么做,我不太了解 GPU 以及 CUDA/OpenGL 的工作原理。 指向正确方向的指针将不胜感激!
您应该为此操作使用 PBO(像素缓冲区对象)。
使用 PBO 的数据传输操作非常快
https://www.khronos.org/opengl/wiki/Pixel_Buffer_Object
GLuint w_pbo[2];
// Create pbo objects and than
// Do your drawings.
int w_readIndex = 0;
int w_writeIndex = 1;
glReadBuffer(GL_COLOR_ATTACHMENT0);
w_writeIndex = (w_writeIndex + 1) % 2;
w_readIndex = (w_readIndex + 1) % 2;
glBindBuffer(GL_PIXEL_PACK_BUFFER, w_pbo[w_writeIndex]);
// copy from framebuffer to PBO asynchronously. it will be ready in the NEXT frame
glReadPixels(0, 0, SCR_WIDTH, SCR_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
// now read other PBO which should be already in CPU memory
glBindBuffer(GL_PIXEL_PACK_BUFFER, w_pbo[w_readIndex]);
unsigned char* downsampleData = (unsigned char*)glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY);
现在您可以使用 unsigned char* downsampleData 构建纹理 memory
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.