Solutions to abnormal operation of kivy (2.0.0) Camera case
- Time: 2021-9-11
Read the original text
brief introduction
The biggest trouble in life is to build a wheel. In fact, someone has written this wheel, but it's not very well written. It makes me modify a lot of code when it runs. It took me more than a month to run reluctantly. Today I'm really idle (mainly because I haven't got a job). Why don't I make this wheel better.
It's also very simple to write a host computer interface for scientific researchers (the original instability). It is considered that you can write with flash or django, which is more familiar; After thinking about whether to go directly to the gui, such as pyqt and wxpython, I looked at their respective open source protocols. Pyqt is infected by GPL, wxpython is infected by LPGL, and the new version of kivy adopts MIT.
Consider the MIT protocol kivy.
Platform information
- OS: Linux xx 5.13.13-1-MANJARO x86_64 GNU/Linux
- Python: 3.9.6
- kivy: 2.0.0
Specific process
install
according to Official documents You can use relatively simple pip or conda for installation. Try PIP and install it all at once
# Create a virtual environment (this is a good habit) python -m venv env source env/bin/activate # Install the full version of kivy pip install kivy[full]
test
Then run The simplest window case , no problem indicates that the installation is still perfect (at least for now). Practice shows that it is OK if there is no version number.
Click to expand the codeimport kivy from kivy.app import App from kivy.uix.label import Label class MyApp(App): def build(self): return Label(text='Hello world') if __name__ == '__main__': MyApp().run()
Camera case
stay Camera Example The case code is officially given in. After one click cv operation, it is found that it cannot run.
Click to view the code (consistent with the official website)''' Camera Example ============== This example demonstrates a simple use of the camera. It shows a window with a buttoned labelled 'play' to turn the camera on and off. Note that not finding a camera, perhaps because gstreamer is not installed, will throw an exception during the kv language processing. ''' # Uncomment these lines to see all the messages # from kivy.logger import Logger # import logging # Logger.setLevel(logging.TRACE) from kivy.app import App from kivy.lang import Builder from kivy.uix.boxlayout import BoxLayout import time Builder.load_string(''' <CameraClick>: orientation: 'vertical' Camera: id: camera resolution: (640, 480) play: False ToggleButton: text: 'Play' on_press: camera.play = not camera.play size_hint_y: None height: '48dp' Button: text: 'Capture' size_hint_y: None height: '48dp' on_press: root.capture() ''') class CameraClick(BoxLayout): def capture(self): ''' Function to capture the images and give them the names according to their captured time and date. ''' camera = self.ids['camera'] timestr = time.strftime("%Y%m%d_%H%M%S") camera.export_to_png("IMG_{}.png".format(timestr)) print("Captured") class TestCamera(App): def build(self): return CameraClick() TestCamera().run()
error
Click to view the error messageMainly picamera - ModuleNotFoundError: No module named 'picamera'
$ /home/kearney/Documents/code/python/uc2-gui/env/bin/python /home/kearney/Documents/code/python/uc2-gui/cam.py [INFO ] [Logger ] Record log in /home/kearney/.kivy/logs/kivy_21-09-12_5.txt [INFO ] [Kivy ] v2.0.0 [INFO ] [Kivy ] Installed at "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/__init__.py" [INFO ] [Python ] v3.9.6 (default, Jun 30 2021, 10:22:16) [GCC 11.1.0] [INFO ] [Python ] Interpreter at "/home/kearney/Documents/code/python/uc2-gui/env/bin/python" [INFO ] [Factory ] 186 symbols loaded [INFO ] [ImageLoaderFFPy] Using ffpyplayer 4.3.2 [INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_ffpyplayer [INFO ] [Window ] Provider: sdl2 [INFO ] [GL ] Using the "OpenGL" graphics system [INFO ] [GL ] Backend used <sdl2> [INFO ] [GL ] OpenGL version <b'4.6 (Compatibility Profile) Mesa 21.2.1'> [INFO ] [GL ] OpenGL vendor <b'AMD'> [INFO ] [GL ] OpenGL renderer <b'AMD RENOIR (DRM 3.41.0, 5.13.13-1-MANJARO, LLVM 12.0.1)'> [INFO ] [GL ] OpenGL parsed version: 4, 6 [INFO ] [GL ] Shading version <b'4.60'> [INFO ] [GL ] Texture max size <16384> [INFO ] [GL ] Texture max units <32> [INFO ] [Window ] auto add sdl2 input provider [INFO ] [Window ] virtual keyboard not allowed, single mode, not docked [CRITICAL] [Camera ] Unable to find any valuable Camera provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes picamera - ModuleNotFoundError: No module named 'picamera' File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_picamera.py", line 18, in <module> from picamera import PiCamera gi - ModuleNotFoundError: No module named 'gi' File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_gi.py", line 10, in <module> from gi.repository import Gst opencv - ModuleNotFoundError: No module named 'cv2' File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 48, in <module> import cv2 Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/cam.py", line 43, in <module> TestCamera().run() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/app.py", line 949, in run self._run_prepare() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/app.py", line 919, in _run_prepare root = self.build() File "/home/kearney/Documents/code/python/uc2-gui/cam.py", line 40, in build return CameraClick() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/boxlayout.py", line 145, in __init__ super(BoxLayout, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/layout.py", line 76, in __init__ super(Layout, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/widget.py", line 359, in __init__ self.apply_class_lang_rules( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/widget.py", line 463, in apply_class_lang_rules Builder.apply( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 541, in apply self._apply_rule( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 659, in _apply_rule child = cls(__no_builder=True) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/camera.py", line 91, in __init__ on_index() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/camera.py", line 101, in _on_index self._camera = CoreCamera(index=self.index, stopped=True) TypeError: 'NoneType' object is not callable
This is ridiculous. I'm not using raspberry pie now. Why call picamera... Then according to the information given by the website
> Note that not finding a camera, perhaps because gstreamer is not installed
Then check (sudo pacman -Q gstreamer) that gstreamer 1.18.4-1 has been installed in my system
Then look at the comments in the sentence code. Remove the comments in lines 13 ~ 15 of the code, and there should be more detailed record output after running
Click to view more detailed error reporting informationThe information is too long to understand what this tells me..
$ /home/kearney/Documents/code/python/uc2-gui/env/bin/python /home/kearney/Documents/code/python/uc2-gui/cam.py [INFO ] [Logger ] Record log in /home/kearney/.kivy/logs/kivy_21-09-12_7.txt [INFO ] [Kivy ] v2.0.0 [INFO ] [Kivy ] Installed at "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/__init__.py" [INFO ] [Python ] v3.9.6 (default, Jun 30 2021, 10:22:16) [GCC 11.1.0] [INFO ] [Python ] Interpreter at "/home/kearney/Documents/code/python/uc2-gui/env/bin/python" [INFO ] [Factory ] 186 symbols loaded [DEBUG ] [Cache ] register <kv.lang> with limit=None, timeout=None [TRACE ] [Lang ] load file /home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/data/style.kv, using utf8 encoding [TRACE ] [Parser ] parsing 1298 lines [TRACE ] [Parser ] got directive <kivy 1.0> [TRACE ] [Builder ] build rule for <Label> [TRACE ] [Builder ] build rule for <-Button,-ToggleButton> [TRACE ] [Builder ] build rule for <BubbleContent> [TRACE ] [Builder ] build rule for <BubbleButton> [TRACE ] [Builder ] build rule for <Slider> [TRACE ] [Builder ] build rule for <ProgressBar> [TRACE ] [Builder ] build rule for <SplitterStrip> [TRACE ] [Builder ] build rule for <Scatter> [TRACE ] [Builder ] build rule for <RelativeLayout> [TRACE ] [Builder ] build rule for <Image,AsyncImage> [TRACE ] [Builder ] build rule for <EffectWidget> [TRACE ] [Builder ] build rule for <TabbedPanelContent> [TRACE ] [Builder ] build rule for <TabbedPanelStrip> [TRACE ] [Builder ] build rule for <StripLayout> [TRACE ] [Builder ] build rule for <TabbedPanelHeader> [TRACE ] [Builder ] build rule for <Selector> [TRACE ] [Builder ] build rule for <TextInput> [TRACE ] [Builder ] build rule for <TextInputCutCopyPaste> [TRACE ] [Builder ] build rule for <CodeInput> [TRACE ] [Builder ] build rule for <TreeViewNode> [TRACE ] [Builder ] build rule for <TreeViewLabel> [TRACE ] [Builder ] build rule for <StencilView> [TRACE ] [Builder ] build rule for <FileChooserListLayout> [TRACE ] [Builder ] build rule for <FileChooserListView> [TRACE ] [Builder ] build template for [FileListEntry@FloatLayout+TreeViewNode] [TRACE ] [Builder ] build rule for <FileChooserIconLayout> [TRACE ] [Builder ] build rule for <FileChooserIconView> [TRACE ] [Builder ] build template for [FileIconEntry@Widget] [TRACE ] [Builder ] build rule for <FileChooserProgress> [TRACE ] [Builder ] build rule for <Switch> [TRACE ] [Builder ] build rule for <ModalView> [TRACE ] [Builder ] build rule for <Popup> [TRACE ] [Builder ] build rule for <SpinnerOption> [TRACE ] [Builder ] build rule for <Spinner> [TRACE ] [Builder ] build rule for <ActionBar> [TRACE ] [Builder ] build rule for <ActionView> [TRACE ] [Builder ] build rule for <ActionSeparator> [TRACE ] [Builder ] build rule for <ActionButton,ActionToggleButton> [TRACE ] [Builder ] build rule for <ActionLabel> [TRACE ] [Builder ] build rule for <ActionGroup> [TRACE ] [Builder ] build rule for <ActionCheck> [TRACE ] [Builder ] build rule for <ActionPreviousImage@Image> [TRACE ] [Builder ] build rule for <ActionPreviousButton@Button> [TRACE ] [Builder ] build rule for <ActionPrevious> [TRACE ] [Builder ] build rule for <ActionGroup> [TRACE ] [Builder ] build rule for <ActionOverflow> [TRACE ] [Builder ] build rule for <ActionDropDown> [TRACE ] [Builder ] build template for [AccordionItemTitle@Label] [TRACE ] [Builder ] build rule for <AccordionItem> [TRACE ] [Builder ] build rule for <SettingSpacer> [TRACE ] [Builder ] build rule for <SettingItem> [TRACE ] [Builder ] build rule for <SettingBoolean> [TRACE ] [Builder ] build rule for <SettingString> [TRACE ] [Builder ] build rule for <SettingPath> [TRACE ] [Builder ] build rule for <SettingOptions> [TRACE ] [Builder ] build rule for <SettingTitle> [TRACE ] [Builder ] build rule for <SettingSidebarLabel> [TRACE ] [Builder ] build rule for <SettingsPanel> [TRACE ] [Builder ] build rule for <Settings> [TRACE ] [Builder ] build rule for <InterfaceWithSidebar> [TRACE ] [Builder ] build rule for <InterfaceWithSpinner> [TRACE ] [Builder ] build rule for <MenuSpinner> [TRACE ] [Builder ] build rule for <MenuSidebar> [TRACE ] [Builder ] build rule for <ContentPanel> [TRACE ] [Builder ] build rule for <InterfaceWithTabbedPanel> [TRACE ] [Builder ] build rule for <ScrollView> [TRACE ] [Builder ] build rule for <VideoPlayerPreview> [TRACE ] [Builder ] build rule for <VideoPlayerAnnotation> [TRACE ] [Builder ] build rule for <VideoPlayer> [TRACE ] [Builder ] build rule for <CheckBox> [TRACE ] [Builder ] build rule for <ScreenManager> [TRACE ] [Builder ] build rule for <ColorPicker_Input@TextInput> [TRACE ] [Builder ] build rule for <ColorPicker_Label@Label> [TRACE ] [Builder ] build rule for <ColorPicker_Selector@BoxLayout> [TRACE ] [Builder ] build rule for <ColorWheel> [TRACE ] [Builder ] build rule for <ColorPicker> [DEBUG ] [Cache ] register <kv.image> with limit=None, timeout=60 [DEBUG ] [Cache ] register <kv.atlas> with limit=None, timeout=None [INFO ] [ImageLoaderFFPy] Using ffpyplayer 4.3.2 [INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_ffpyplayer [DEBUG ] [Cache ] register <kv.texture> with limit=1000, timeout=60 [DEBUG ] [Cache ] register <kv.shader> with limit=1000, timeout=3600 [TRACE ] [Parser ] parsing 17 lines [TRACE ] [Builder ] build rule for <CameraClick> [DEBUG ] [App ] Loading kv </home/kearney/Documents/code/python/uc2-gui/testcamera.kv> [DEBUG ] [App ] kv </home/kearney/Documents/code/python/uc2-gui/testcamera.kv> not found [TRACE ] [Parser ] parsing 19 lines [TRACE ] [Builder ] build rule for <-CoverBehavior> [INFO ] [Window ] Provider: sdl2 [INFO ] [GL ] Using the "OpenGL" graphics system [INFO ] [GL ] Backend used <sdl2> [INFO ] [GL ] OpenGL version <b'4.6 (Compatibility Profile) Mesa 21.2.1'> [INFO ] [GL ] OpenGL vendor <b'AMD'> [INFO ] [GL ] OpenGL renderer <b'AMD RENOIR (DRM 3.41.0, 5.13.13-1-MANJARO, LLVM 12.0.1)'> [INFO ] [GL ] OpenGL parsed version: 4, 6 [INFO ] [GL ] Shading version <b'4.60'> [INFO ] [GL ] Texture max size <16384> [INFO ] [GL ] Texture max units <32> [TRACE ] [Cache ] Flushed category kv.texture from cache [TRACE ] [Cache ] Flushed category kv.shader from cache [DEBUG ] [Shader ] Fragment compiled successfully [DEBUG ] [Shader ] Vertex compiled successfully [DEBUG ] [ImageSDL2 ] Load </home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/data/glsl/default.png> [TRACE ] [Image ] '/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/data/glsl/default.png', populate to textures (1) [INFO ] [Window ] auto add sdl2 input provider [INFO ] [Window ] virtual keyboard not allowed, single mode, not docked [TRACE ] [Lang ] Found 1 rules for <__main__.CameraClick object at 0x7f40097ee350> [DEBUG ] [Camera ] Ignored <picamera> (import error) [TRACE ] Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_picamera.py", line 18, in <module> from picamera import PiCamera ModuleNotFoundError: No module named 'picamera' [DEBUG ] [Camera ] Ignored <gi> (import error) [TRACE ] Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_gi.py", line 10, in <module> from gi.repository import Gst ModuleNotFoundError: No module named 'gi' [DEBUG ] [Camera ] Ignored <opencv> (import error) [TRACE ] Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 21, in <module> import opencv as cv ModuleNotFoundError: No module named 'opencv' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 48, in <module> import cv2 ModuleNotFoundError: No module named 'cv2' [CRITICAL] [Camera ] Unable to find any valuable Camera provider. Please enable debug logging (e.g. add -d if running from the command line, or change the log level in the config) and re-run your app to identify potential causes picamera - ModuleNotFoundError: No module named 'picamera' File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_picamera.py", line 18, in <module> from picamera import PiCamera gi - ModuleNotFoundError: No module named 'gi' File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_gi.py", line 10, in <module> from gi.repository import Gst opencv - ModuleNotFoundError: No module named 'cv2' File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/__init__.py", line 58, in core_select_lib mod = __import__(name='{2}.{0}.{1}'.format( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 48, in <module> import cv2 Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/cam.py", line 46, in <module> TestCamera().run() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/app.py", line 949, in run self._run_prepare() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/app.py", line 919, in _run_prepare root = self.build() File "/home/kearney/Documents/code/python/uc2-gui/cam.py", line 43, in build return CameraClick() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/boxlayout.py", line 145, in __init__ super(BoxLayout, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/layout.py", line 76, in __init__ super(Layout, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/widget.py", line 359, in __init__ self.apply_class_lang_rules( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/widget.py", line 463, in apply_class_lang_rules Builder.apply( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 541, in apply self._apply_rule( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 659, in _apply_rule child = cls(__no_builder=True) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/camera.py", line 91, in __init__ on_index() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/camera.py", line 101, in _on_index self._camera = CoreCamera(index=self.index, stopped=True) TypeError: 'NoneType' object is not callable
This is outrageous. Check Github repo. There are still many cameras that can't be used (such as ubuntu, win10 and raspi). The problems on stackhoverfold were five years ago. Recently, I want to combine the opencv diagram with kivy. From some of these exchanges, it seems that opencv Python needs to be installed.. Then you can install PIP install opencv python
At this time, comment out lines 13 ~ 15 of the code. Too long information will lead to confusion. At this time, the error output will change, and it is no longer a picamera error
Click to view the new error message$ /home/kearney/Documents/code/python/uc2-gui/env/bin/python /home/kearney/Documents/code/python/uc2-gui/cam.py [INFO ] [Logger ] Record log in /home/kearney/.kivy/logs/kivy_21-09-12_8.txt [INFO ] [Kivy ] v2.0.0 [INFO ] [Kivy ] Installed at "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/__init__.py" [INFO ] [Python ] v3.9.6 (default, Jun 30 2021, 10:22:16) [GCC 11.1.0] [INFO ] [Python ] Interpreter at "/home/kearney/Documents/code/python/uc2-gui/env/bin/python" [INFO ] [Factory ] 186 symbols loaded [INFO ] [ImageLoaderFFPy] Using ffpyplayer 4.3.2 [INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_ffpyplayer [INFO ] [Window ] Provider: sdl2 [INFO ] [GL ] Using the "OpenGL" graphics system [INFO ] [GL ] Backend used <sdl2> [INFO ] [GL ] OpenGL version <b'4.6 (Compatibility Profile) Mesa 21.2.1'> [INFO ] [GL ] OpenGL vendor <b'AMD'> [INFO ] [GL ] OpenGL renderer <b'AMD RENOIR (DRM 3.41.0, 5.13.13-1-MANJARO, LLVM 12.0.1)'> [INFO ] [GL ] OpenGL parsed version: 4, 6 [INFO ] [GL ] Shading version <b'4.60'> [INFO ] [GL ] Texture max size <16384> [INFO ] [GL ] Texture max units <32> [INFO ] [Window ] auto add sdl2 input provider [INFO ] [Window ] virtual keyboard not allowed, single mode, not docked [INFO ] [Camera ] Provider: opencv(['camera_picamera', 'camera_gi'] ignored) [INFO ] [Text ] Provider: sdl2 [ WARN:0] global /tmp/pip-req-build-3umofm98/opencv/modules/videoio/src/cap_v4l.cpp (890) open VIDEOIO(V4L2:/dev/video0): can't open camera by index Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 705, in _apply_rule setattr(widget_set, key, value) File "kivy/weakproxy.pyx", line 35, in kivy.weakproxy.WeakProxy.__setattr__ File "kivy/properties.pyx", line 498, in kivy.properties.Property.__set__ File "kivy/properties.pyx", line 840, in kivy.properties.ListProperty.set File "kivy/properties.pyx", line 545, in kivy.properties.Property.set File "kivy/properties.pyx", line 600, in kivy.properties.Property.dispatch File "kivy/_event.pyx", line 1248, in kivy._event.EventObservers.dispatch File "kivy/_event.pyx", line 1154, in kivy._event.EventObservers._dispatch File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/camera.py", line 103, in _on_index self._camera = CoreCamera(index=self.index, File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 70, in __init__ super(CameraOpenCV, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/__init__.py", line 70, in __init__ self.init_camera() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 120, in init_camera self._resolution = (int(frame.shape[1]), int(frame.shape[0])) AttributeError: 'NoneType' object has no attribute 'shape' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/kearney/Documents/code/python/uc2-gui/cam.py", line 46, in <module> TestCamera().run() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/app.py", line 949, in run self._run_prepare() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/app.py", line 919, in _run_prepare root = self.build() File "/home/kearney/Documents/code/python/uc2-gui/cam.py", line 43, in build return CameraClick() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/boxlayout.py", line 145, in __init__ super(BoxLayout, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/layout.py", line 76, in __init__ super(Layout, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/widget.py", line 359, in __init__ self.apply_class_lang_rules( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/widget.py", line 463, in apply_class_lang_rules Builder.apply( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 541, in apply self._apply_rule( File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 710, in _apply_rule raise BuilderException(rule.ctx, rule.line, kivy.lang.builder.BuilderException: Parser: File "<inline>", line 6: ... 4: Camera: 5: id: camera >> 6: resolution: (640, 480) 7: play: False 8: ToggleButton: ... AttributeError: 'NoneType' object has no attribute 'shape' File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/lang/builder.py", line 705, in _apply_rule setattr(widget_set, key, value) File "kivy/weakproxy.pyx", line 35, in kivy.weakproxy.WeakProxy.__setattr__ File "kivy/properties.pyx", line 498, in kivy.properties.Property.__set__ File "kivy/properties.pyx", line 840, in kivy.properties.ListProperty.set File "kivy/properties.pyx", line 545, in kivy.properties.Property.set File "kivy/properties.pyx", line 600, in kivy.properties.Property.dispatch File "kivy/_event.pyx", line 1248, in kivy._event.EventObservers.dispatch File "kivy/_event.pyx", line 1154, in kivy._event.EventObservers._dispatch File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/uix/camera.py", line 103, in _on_index self._camera = CoreCamera(index=self.index, File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 70, in __init__ super(CameraOpenCV, self).__init__(**kwargs) File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/__init__.py", line 70, in __init__ self.init_camera() File "/home/kearney/Documents/code/python/uc2-gui/env/lib/python3.9/site-packages/kivy/core/camera/camera_opencv.py", line 120, in init_camera self._resolution = (int(frame.shape[1]), int(frame.shape[0]))
The error message pointed out the line of resolution: (640, 480). The experience of debugging uc2 before me told me that it would get a good result if the line was canceled or deleted, and it would run normally after it was deleted.
summary
terms of settlement
- Install openv Python
- Delete the resolution code
Concise code
The case code is nested kv in python. I really don't like it. Change it.
from kivy.app import App from kivy.uix.button import Button from kivy.uix.boxlayout import BoxLayout from kivy.uix.camera import Camera import time class CameraGUI(BoxLayout): def __init__(self, **kwargs): super(CameraGUI, self).__init__(**kwargs) self.camera = Camera(play=False) self.add_widget(self.camera) self.orientation = "vertical" self.btnplay = Button(text="Play") self.btnplay.bind(on_release=self.Play) self.add_widget(self.btnplay) self.btncap = Button(text="Capture") self.btncap.bind(on_release=self.Capture) self.add_widget(self.btncap) def Capture(self, instance): """ Function to capture the images and give them the names according to their captured time and date. """ timestr = time.strftime("%Y%m%d_%H%M%S") self.camera.export_to_png("IMG_{}.png".format(timestr)) print("The button <%s> is being pressed" % instance.text) def Play(self, instance): """ start or stop the camera """ print("The button <%s> is being pressed" % instance.text) self.camera.play = not self.camera.play class Test(App): def build(self): return CameraGUI() if __name__ == "__main__": Test().run()
reference resources
- Python Kivy (App development) calls an example of a camera. Bottom fishing for freshwater fish 2020-08-05 : kivy 1.11 run the same case code, and the error is still reported
- kivy doc examples