2

*我正在尝试显示使用 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: not enough image data


好吧,我进口

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: expected a character buffer object

4

5 回答 5

1

看起来ctypes.string_at(address, size)就是你想要的。这将为您提供一个 python 字符串缓冲区,其中包含指针地址处的内存内容。这应该适合传递给Image.fromstringor pygame.image.frombuffer

于 2010-01-18T22:49:46.653 回答
0

您是否尝试过buffer直接作为第一个参数传递?如果这不起作用并且您想使用 制作可写字符缓冲区ctypes,那么create_string_buffer是我知道的唯一方法(我不明白您从哪里得到b_address.value from)。

于 2010-01-15T16:36:42.540 回答
0

好的,我所做的就是将 pygame 留给 Tkinter 和 PIL

现在在相同的分配之后,我将 buffer.m.userptr* 传递给 Image 中的 fromstring 方法

首先,我当然有以下内容:

import Image
import Tkinter

tk = Tkinter.Tk()
preview = ImageTk.PhotoImage(ps['pixformat'], (ps['width'], ps['height']))
label = Tkinter.Label(tk, text='Preview', image=preview, width=ps['width'], height=ps['height'])
label.pack()

现在预览:

im = Image.fromstring(ps['pixformat'], (format.fmt.pix.width, format.fmt.pix.height), '\0'*buffer.m.userptr)
preview.paste(im)
tk.update()

我做了@sipickles 所说的,用 '\0' 看看整个事情是否会奏效 ,它确实做到了:)

问题是如何正确传递该 userptr 并且其中的数据实际上是需要传递给预览的

我真的迷路了。有人知道v4l2吗?

于 2010-01-18T11:49:39.897 回答
0

好的。所以现在我通过自己设置 sizeimage 来解决 sizeimage 问题:

现在 frombuffer 显示的内容不是缓冲区中的帧。

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()
type = V4L2_BUF_TYPE_VIDEO_CAPTURE

device = open(device_name, 'rw')

ioctl(device, VIDIOC_QUERYCAP, 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

request.type = type
request.memory = V4L2_MEMORY_USERPTR
request.count = 1

format.fmt.pix.sizeimage = format.fmt.pix.width * format.fmt.pix.height * 4
buffer.length = format.fmt.pix.sizeimage

ioctl(device, VIDIOC_S_FMT, format)

posix_memalign(byref(b_address), getpagesize(), format.fmt.pix.sizeimage)

buffer.m.userptr = b_address.value

buffer.type = request.type
buffer.memory = request.memory

ioctl(device, VIDIOC_REQBUFS, request)

while True:

    ioctl(device, VIDIOC_QBUF, buffer)

    ioctl(device, VIDIOC_STREAMON, cast(type, c_void_p))

    ioctl(device, VIDIOC_DQBUF, buffer)

    **# What happens here? preview_data is wrong?**
    preview_data = string_at(buffer.m.userptr, buffer.length)

    im = frombuffer(ps['pixformat'], (ps['width'], ps['height']), preview_data)
    image.paste(im)
    preview.update()
于 2010-01-26T12:53:35.857 回答
0

我对 ctypes 了解不多,但我正在做类似的事情(包装的 c++ 网络摄像头捕获,使用 DirectPython 显示)。

就我而言,我只是在 python 中创建了一个缓冲区,如下所示:

bufferSize = imageWidth * imageHeight
buf = "\0" * bufferSize

将 buf 传递给您的图像捕获功能进行填充?

也许发布更完整的代码示例......

于 2010-01-15T13:24:45.107 回答