本文介绍了python中的用户指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
*我正在尝试显示使用v4l捕获的网络摄像头的预览.
*I am trying to display preview from webcam captured using 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:预期为字符缓冲区对象
推荐答案
类似 ctypes.string_at(地址,大小)
是您想要的.这将为您提供一个python字符串缓冲区,其内存内容位于指针的地址处.这应该适合传递给 Image.fromstring
或 pygame.image.frombuffer
.
这篇关于python中的用户指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!