v4l2文档的示例代码是否原样(打开为'rw')?

时间:2017-02-14 11:47:31

标签: python python-3.x ubuntu ubuntu-16.04 v4l2

我刚开始在v4l2上使用Python 3Ubuntu 16.04.}

我正在尝试从v4l2 Python文档中运行该示例,即:

$ cat demo4SO.py
#!/usr/bin/env python3

import v4l2
import fcntl

vd = open('/dev/video0', 'rw')
#vd = open('/dev/video0', 'r')
cp = v4l2.v4l2_capability()
fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp)

print("driver:",cp.driver)
print("card:",cp.card)

但是我得到了:

$ python3 demo4SO.py
Traceback (most recent call last):
  File "demo4SO.py", line 6, in <module>
    vd = open('/dev/video0', 'rw')
ValueError: must have exactly one of create/read/write/append mode

即使使用sudo.

运行,我也会收到同样的错误

尝试用'w +'(或'r +')打开,我得到:

$ python3 demo4SO.py
Traceback (most recent call last):
  File "demo4SO.py", line 8, in <module>
    vd = open('/dev/video0', "w+")
io.UnsupportedOperation: File or stream is not seekable.

如果我将'rw'(或'w +'或'r +')更改为'r',代码将按预期工作。

$ cat demo4SO.py
#!/usr/bin/env python3

import v4l2
import fcntl

#vd = open('/dev/video0', 'rw')
vd = open('/dev/video0', 'r')
cp = v4l2.v4l2_capability()
fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp)

print("driver:",cp.driver)
print("card:",cp.card)


$ python3 demo4SO.py
driver: b'uvcvideo'
card: b'Intel(R) RealSense(TM) 410'

我的用户名属于视频设备的组,因此问题似乎与权限无关:

$ groups
openstack root sudo video staff
$ ls -ls /dev/video0
0 crw-rw----+ 1 root video 81, 0 פבר 14 11:31 /dev/video0

知道为什么我不能按原样运行文档的示例代码(使用'rw')?

1 个答案:

答案 0 :(得分:1)

更改

vd = open('/dev/video0', 'rw')

vd = open('/dev/video0', 'rb+', buffering=0)

允许使用O_RDWR权限打开文件。