我编写了一个简单的python3脚本,通过USB网络摄像头捕获图像。代码运行在Raspberry Pi 3(Raspbian Jessie)上。该代码使用以下库:
程序的第一次运行正确运行。但是,每次后续运行都会受到影响。请找到以下代码:
pygame.init()
print("Initialized pygame. Listing cameras")
pygame.camera.init()
pprint(pygame.camera.list_cameras())
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()
print("Camera started")
image = cam.get_image()
print("Photo captured!")
pygame.image.save(image,'selfie.jpg')
cam.stop()
print("Unlocking resource...")
pygame.quit()
print("Done!")
五次运行的输出日志如下: 运行1:
初始化的pygame。列出相机 ['的/ dev / video0的'] 相机开始了 照片拍摄! 解锁资源...... 完成!
运行2:
Initialized pygame. Listing cameras
[]
Traceback (most recent call last):
File "camtest.py", line 56, in <module>
cam.start()
SystemError: Cannot identify '/dev/video0': 2, No such file or directory
运行3:
Initialized pygame. Listing cameras
[]
Traceback (most recent call last):
File "camtest.py", line 56, in <module>
cam.start()
SystemError: Cannot identify '/dev/video0': 2, No such file or directory
运行4:
Initialized pygame. Listing cameras
[]
Traceback (most recent call last):
File "camtest.py", line 56, in <module>
cam.start()
SystemError: Cannot open '/dev/video0': 13, Permission denied
运行5:
Initialized pygame. Listing cameras
['/dev/video0']
Camera started
Photo captured!
Unlocking resource...
Done!
如您所见,生成的错误表明/ dev / video0处的设备不可用。我已检查/ dev和video0始终可用。