Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的用户指针_Python_Ctypes_V4l2 - Fatal编程技术网

python中的用户指针

python中的用户指针,python,ctypes,v4l2,Python,Ctypes,V4l2,*我正在尝试显示使用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

*我正在尝试显示使用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:没有足够的图像数据


嗯,我要进口

然后

诸如此类的事情,我试图获得记忆

现在b_地址不再是=None b_地址类似于
c_void\u p(145014784)

然后我开始循环,QBUF,DQBUF,等等

问题是,当我调用pygame.image.frombuffer时


我得到了<强>类型错误:预期字符缓冲对象

> P>我对C类型了解不多,但我正在做类似的事情(包裹C++网络摄像头捕获,用DirectPython显示)。 在我的例子中,我只是用python制作了一个缓冲区,如下所示:

bufferSize = imageWidth * imageHeight
buf = "\0" * bufferSize
将buf传递给图像捕获功能以进行填充


可能会发布一个更完整的代码示例…

您是否尝试将
缓冲区作为第一个参数直接传递?如果这不起作用,你想用
ctypes
创建一个可写的字符缓冲区,这是我知道的唯一方法(我不知道你从哪里得到
b_address.value

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

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

首先,我当然有以下几点:

现在预览:

我按照@sipickles所说的做了,使用“\0”来查看整个过程是否有效 确实如此:)

问题是如何正确地对userptr进行pas,其中的数据实际上是需要传递给预览的数据吗

我在这里真的迷路了。有人知道v4l2?

看起来就是你想要的。这将为您提供一个python字符串缓冲区,其中内存内容位于指针地址。这应该适合传递到
Image.fromstring
pygame.Image.fromsbuffer
确定。所以现在我通过自己设置sizeimage解决了sizeimage问题:

现在frombuffer显示的不是来自缓冲区的帧


调用posix_memalign时,分配b_地址。我不确定addresof(b_地址)是传递该参数的正确方式,因此也不确定这是否是实际问题。不是frombuffer方法,我只是尝试使用python和python绑定,使用v4l从照相机捕获和显示帧。谢谢。非常感谢你。这真的很有帮助。我现在得到了ValueError,但是:ValueError:没有足够的图像数据,我将不得不对此进行进一步调查
ioctl(device, VIDIOC_S_FMT, addr(format))
ioctl(device, VIDIOC_G_FMT, addr(format))
posix_memalign(addressof(b_address), getpagesize(), format.fmt.pix.sizeimage)
pg_img = pygame.image.frombuffer(
         buffer.m.userptr,
         (format.fmt.pix.width, format.fmt.pix.height),
         preview_settings['pixformat']
         )
bufferSize = imageWidth * imageHeight
buf = "\0" * bufferSize
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()
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()