单次通话后,PyGame中的相机无法使用

时间:2017-12-15 10:23:31

标签: python linux python-3.x pygame raspberry-pi3

我编写了一个简单的python3脚本,通过USB网络摄像头捕获图像。代码运行在Raspberry Pi 3(Raspbian Jessie)上。该代码使用以下库:

  1. pygame的
  2. pprint
  3. 程序的第一次运行正确运行。但是,每次后续运行都会受到影响。请找到以下代码:

    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始终可用。

0 个答案:

没有答案