如何检测Kivy是否安装了摄像头?

时间:2014-12-13 09:48:01

标签: python camera kivy

如果没有安装相机子系统,那么在解析examples/camera.py demo的相机小部件时,Kivy会崩溃。而不是崩溃,是否有一个电话,看看是否会找到相机提供商?

这是跟踪:

[DEBUG             ] [Camera      ] Ignored <avfoundation> (import error)
[DEBUG             ] [Camera      ] Ignored <opencv> (import error)
[CRITICAL          ] [Camera      ] Unable to find any valuable Camera provider at all!
[INFO              ] [Text        ] Provider: pygame
 Traceback (most recent call last):
   File "camera.py", line 33, in <module>
     CameraApp().run()
   File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/app.py", line 766, in run
     root = self.build()
   File "camera.py", line 29, in build
     return Builder.load_string(kv)
   File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 1522, in load_string
     self._apply_rule(widget, parser.root, parser.root)
   File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 1704, in _apply_rule
     e))
 kivy.lang.BuilderException: Parser: File "<inline>", line 7:
 ...
       5:    Camera:
       6:        id: camera
 >>    7:        resolution: 399, 299
       8:
       9:    BoxLayout:
 ...
 TypeError: 'NoneType' object is not callable

1 个答案:

答案 0 :(得分:2)

您可以将您的通话初始化为try:..except块:

camera_available = False
try:
    cam = kivy.camera()
except TypeError:
    cam = None
if not cam is None and not cam.__camera is None: # Experiment has shown both checks are needed
    camera_available = True
# Anything that tries to use cam now needs to check camera_available