python中的用户指针
*我正在尝试显示使用 v4l 捕获的网络摄像头的预览。
代码如下:
from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import PhotoImage
from ctypes.util import find_library
libc = CDLL(find_library('c'))
posix_memalign = libc.posix_memalign
getpagesize = libc.getpagesize
device_name = '/dev/video0'
ps = preview_settings = {
'width': 320,
'height': 240,
'pixformat': 'RGB',
}
PIX_FMT = V4L2_PIX_FMT_RGB555
preview = Tk()
image = PhotoImage(ps['pixformat'], (ps['width'], ps['height']))
label = Label(preview, text='Preview', image=image, width=ps['width'], height=ps['height'])
label.pack()
capability = v4l2_capability()
size = v4l2_frmsizeenum()
format = v4l2_format()
request = v4l2_requestbuffers()
buffer = v4l2_buffer()
b_address = c_void_p()
frame_name_count = '0'
type = V4L2_BUF_TYPE_VIDEO_CAPTURE
device = open(device_name, 'rw')
ioctl(device, VIDIOC_QUERYCAP, addr(capability))
size.pixel_format = PIX_FMT
size.index = 0
format.type = type
format.fmt.pix.pixelformat = PIX_FMT
format.fmt.pix.width = size.discrete.width
format.fmt.pix.height = size.discrete.height
format.fmt.pix.field = V4L2_FIELD_NONE
format.fmt.pix.bytesperline = 0
format.fmt.pix.sizeimage = 0
request.type = type
request.memory = V4L2_MEMORY_USERPTR
request.count = 1
ioctl(device, VIDIOC_S_FMT, addr(format))
ioctl(device, VIDIOC_G_FMT, addr(format))
ioctl(device, VIDIOC_REQBUFS, addr(request))
posix_memalign(addressof(b_address), getpagesize(), format.fmt.pix.sizeimage)
buffer.type = request.type
buffer.memory = request.memory
buffer.index = 0
buffer.m.userptr = b_address.value
buffer.length = format.fmt.pix.sizeimage
while True:
ioctl(device, VIDIOC_QBUF, addr(buffer))
ioctl(device, VIDIOC_STREAMON, cast(type, c_void_p))
ioctl(device, VIDIOC_DQBUF, addr(buffer))
preview_data = string_at(buffer.m.userptr, buffer.length)
im = fromstring(ps['pixformat'], (ps['width'], ps['height']), preview_data)
image.paste(im)
preview.update()
我得到 ValueError:没有足够的图像数据
好吧,我导入
c_lib = CDLL(find_library('c'))
posix_memalign = c_lib.posix_memalign
getpagesize = c_lib.getpagesize
然后之后
ioctl(device, VIDIOC_S_FMT, addr(format))
ioctl(device, VIDIOC_G_FMT, addr(format))
诸如此类的事情,我尝试获取内存
posix_memalign(addressof(b_address), getpagesize(), format.fmt.pix.sizeimage)
现在 b_address 不再 = None b_address 类似于 c_void_p(145014784)
然后我开始循环、QBUF、DQBUF 等。
事情是这样的当我调用 pygame.image.frombuffer
pg_img = pygame.image.frombuffer(
buffer.m.userptr,
(format.fmt.pix.width, format.fmt.pix.height),
preview_settings['pixformat']
)
我得到 TypeError:需要一个字符缓冲区对象
*I am trying to display preview from webcam captured using v4l.
Here is an idea of how the code looks like:
from ctypes import *
from v4l2 import *
from Image import fromstring
from Tkinter import Tk, Label
from ImageTk import PhotoImage
from ctypes.util import find_library
libc = CDLL(find_library('c'))
posix_memalign = libc.posix_memalign
getpagesize = libc.getpagesize
device_name = '/dev/video0'
ps = preview_settings = {
'width': 320,
'height': 240,
'pixformat': 'RGB',
}
PIX_FMT = V4L2_PIX_FMT_RGB555
preview = Tk()
image = PhotoImage(ps['pixformat'], (ps['width'], ps['height']))
label = Label(preview, text='Preview', image=image, width=ps['width'], height=ps['height'])
label.pack()
capability = v4l2_capability()
size = v4l2_frmsizeenum()
format = v4l2_format()
request = v4l2_requestbuffers()
buffer = v4l2_buffer()
b_address = c_void_p()
frame_name_count = '0'
type = V4L2_BUF_TYPE_VIDEO_CAPTURE
device = open(device_name, 'rw')
ioctl(device, VIDIOC_QUERYCAP, addr(capability))
size.pixel_format = PIX_FMT
size.index = 0
format.type = type
format.fmt.pix.pixelformat = PIX_FMT
format.fmt.pix.width = size.discrete.width
format.fmt.pix.height = size.discrete.height
format.fmt.pix.field = V4L2_FIELD_NONE
format.fmt.pix.bytesperline = 0
format.fmt.pix.sizeimage = 0
request.type = type
request.memory = V4L2_MEMORY_USERPTR
request.count = 1
ioctl(device, VIDIOC_S_FMT, addr(format))
ioctl(device, VIDIOC_G_FMT, addr(format))
ioctl(device, VIDIOC_REQBUFS, addr(request))
posix_memalign(addressof(b_address), getpagesize(), format.fmt.pix.sizeimage)
buffer.type = request.type
buffer.memory = request.memory
buffer.index = 0
buffer.m.userptr = b_address.value
buffer.length = format.fmt.pix.sizeimage
while True:
ioctl(device, VIDIOC_QBUF, addr(buffer))
ioctl(device, VIDIOC_STREAMON, cast(type, c_void_p))
ioctl(device, VIDIOC_DQBUF, addr(buffer))
preview_data = string_at(buffer.m.userptr, buffer.length)
im = fromstring(ps['pixformat'], (ps['width'], ps['height']), preview_data)
image.paste(im)
preview.update()
and I get ValueError: not enough image data
well, I import
c_lib = CDLL(find_library('c'))
posix_memalign = c_lib.posix_memalign
getpagesize = c_lib.getpagesize
and then after
ioctl(device, VIDIOC_S_FMT, addr(format))
ioctl(device, VIDIOC_G_FMT, addr(format))
and stuff like that, I try to acquire memory
posix_memalign(addressof(b_address), getpagesize(), format.fmt.pix.sizeimage)
now b_address is no longer = None
b_address is something like c_void_p(145014784)
then I start the loop, QBUF, DQBUF, etc..
the thing is, that when I call pygame.image.frombuffer
pg_img = pygame.image.frombuffer(
buffer.m.userptr,
(format.fmt.pix.width, format.fmt.pix.height),
preview_settings['pixformat']
)
I get TypeError: expected a character buffer object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
看起来像
ctypes.string_at(地址,大小)
< /a> 就是你想要的。这将为您提供一个 python 字符串缓冲区,其中包含指针地址处的内存内容。这应该适合传递给Image.fromstring
或pygame.image.frombuffer
。Looks like
ctypes.string_at(address, size)
is what you want. That'll give you a python string buffer with the contents of the memory at your pointer's address. This should be suitable to pass toImage.fromstring
orpygame.image.frombuffer
.我对 ctypes 不太了解,但我正在做类似的事情(包装的 c++ 网络摄像头捕获,用 DirectPython 显示)。
就我而言,我只是在 python 中创建了一个缓冲区,如下所示:
将 buf 传递给您的图像捕获函数进行填充?
也许发布更完整的代码示例......
I don't know much about ctypes, but I am doing a similar thing (wrapped c++ webcam capture, displayed with DirectPython).
In my case I just made a buffer in python like this:
Pass buf to your image capture function for filling?
Maybe post a more complete code sample...
您是否尝试过直接传递
buffer
作为第一个参数?如果这不起作用,并且您想使用ctypes
创建可写字符缓冲区,create_string_buffer 是我知道的唯一方法(我不明白你从哪里获取b_address.value
em>)。Have you tried passing
buffer
directly as the first arg? If that doesn't work and you want to make a writeable character buffer withctypes
, create_string_buffer is the only way I know (I don't understand where you're getting theb_address.value
from).好吧,我所做的就是将 pygame 留给 Tkinter 和 PIL
现在,在相同的分配之后,我将其传递
buffer.m.userptr
* 到 fromstring 方法从图像首先我当然有以下内容:
现在预览:
我做了@sipickles所说的,用'\0'来看看整个事情会成功的
它确实做到了:)
问题是如何正确地传递 userptr 以及其中的数据实际上是需要传递给预览的内容
我真的迷失在这里。有人知道 v4l2 吗?
OK, what I did was to leave pygame for Tkinter and PIL
now after the same allocation I pass
buffer.m.userptr
*to fromstring method from Imagefirst I have, of course, the following:
and now preview:
I did what @sipickles said, with '\0' to see if the whole thing will work
And it did :)
the thing is how to pas that userptr correctly and is the data in it actually what it needs to be passed to the preview
I am really lost here. Someone knows v4l2?
好的。所以现在我通过自己设置 sizeimage 解决了 sizeimage 问题:
现在 frombuffer 显示的内容不是缓冲区中的帧。
Ok. so for now I fixed the sizeimage problem by setting the sizeimage myself:
Now frombuffer displays something that is not the frame from the buffer.