How to save captured picture in kivy without timestamp as name - python

This is camera code
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()
I want to save captured image in my current directory with a name like img.png instead of saving image name with a timestamp.
This code saves image with name like IMG_20200216_162830 and I want it like IMG.png
Can anyone help me?
Thank you

Remove the timestamp, that's all:
camera.export_to_png("IMG.png")

Related

Kivy (Python) Problem with AttributeError after add_widget with kv-file

I'am new in Kivy and have follow problem (Environment is Python 3.7 with Kivy-1.11.1.):
I need a navigation area and a view area (=ViewScreen). With the navigation area i change the view area (change of kv-files - look later at 'def next_screen'). My problem is, that i can't interact with widgets (e.g. 'lblTest') in the view area.
I use follow files:
testGUI.py (= GUI Application)
testGUIRoot.kv (= RootWidget as kv-file)
testGUIBtn1.kv (= view area 1 as kv-file)
testGUIBtn2.kv (= view area 2 as kv-file)
The GUI Application is simple and starts the GUI and change the view area.
testGUI.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
class RootWidget(BoxLayout):
# runs select application
def startApplication (self, instance):
print(self.lblTest)
class mainGUI(App):
def build(self):
# loading the content of root.kv
self.root = Builder.load_file('testGUIRoot.kv')
def next_screen(self, screen):
#Clear container and load the given screen object from file in kv folder.
filename = screen + '.kv'
# unload the content of the .kv file
# reason: it could have data from previous calls
Builder.unload_file(filename)
# clear the container
self.root.container.clear_widgets()
# load the content of the .kv file
screen = Builder.load_file(filename)
# add the content of the .kv file to the container
self.root.container.add_widget(screen)
if __name__ == '__main__':
'''Start the application'''
mainGUI().run()
I use follow kv-files:
testGUIRoot.kv:
#:kivy 1.11.1
RootWidget:
container: container
orientation: 'horizontal'
# Navigation
BoxLayout:
orientation: 'vertical'
size_hint: (0.35, 1)
Button:
text: 'testButton1'
on_release: root.startApplication(self,)
on_press: app.next_screen('testGUIBtn1')
Button:
text: 'testButton2'
on_release: root.startApplication(self,)
on_press: app.next_screen('testGUIBtn2')
# ViewScreen
BoxLayout:
size_hint: (0.65, 1)
id: container
orientation: 'vertical'
padding: 0
spacing: 3
Label:
text: 'no data'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
testGUIBtn1.kv:
#:kivy 1.11.1
Label:
id: lblTest
text: 'Button 1'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
testGUIBtn2.kv:
#:kivy 1.11.1
Label:
id: lblTest
text: 'Button 2'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
Follow error appears:
AttributeError: 'RootWidget' object has no attribute 'lblTest'
Have you a solution to interact with the Object 'lblTest'? For example like self.lblTest.text = 'Test-Text'.
Thank you in advance
I have been working with Kivy just this week, also as a new user.
The thing I have learned is to define properties on your RootWidget, just as the labels you already defined in the .kv files. This 'links' the layout and the Python code to eachother.
First of all you need to import the Kivy ObjectProperty by adding:
from kivy.properties import ObjectProperty
Next up is to declare the property on your RootWidget class.
You can add lblTest = ObjectProperty(None) right after the class-declaration.
The top of your file testGUI.py should look like this then:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class RootWidget(BoxLayout):
# Link the layout objects
lblTest = ObjectProperty(None)
# runs select application
def startApplication (self, instance):
print(self.lblTest)
The page that really helped me with this is https://techwithtim.net/tutorials/kivy-tutorial/object-properties/
A little sidenote is it would be best to keep your id attributes fully unique.
I found the solution
Content of 'def next_screen' of mainGUI(App) to 'def startApplication'. Now i can change widgets in object oScreen or using the object in other python libs.
def startApplication (self, instance, sScreen):
filename = sScreen + '.kv'
# unload the content of the .kv file
# reason: it could have data from previous calls
Builder.unload_file(filename)
# clear the container
self.container.clear_widgets()
# load the content of the .kv file
oScreen = Builder.load_file(filename)
# add the content of the .kv file to the container
self.container.add_widget(oScreen)
print(oScreen.ids.lblTest.text)
The follow should be added in kv-files testGUIBtn1.kv, testGUIBtn2.kv:
RootWidget:
In kv-file testGUIRoot.kv you have change on_release to
on_release: root.startApplication(self,'testGUIBtn1')

Why doesn't the next screen show up in kivy app?

I am expecting the following kivy app to switch screens when I press and then release the button, but nothing happens and there's no error on the terminal. When I run the app, the GirisEkrani screen show up, and then when I press and release the button in GirisEkrani, the next screen (GirisEkrani2) should show up. Do you how to make that work?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<ekranApp>
GirisEkrani:
id: ge
Button:
text: "İleri"
on_release: ge.manager.current = ge.manager.next()
GirisEkrani2:
Button:
id: ge2
text: "Ileri 2"
on_release: ge2.manager.current = ge2.manager.next()
KontrolEkrani:
id: ke
Button:
text: "Geri"
on_release: ke.manager.current = ke.manager.previous()
""")
class GirisEkrani(Screen):
pass
class GirisEkrani2(Screen):
pass
class KontrolEkrani(Screen):
pass
class ekranApp(App, ScreenManager):
def build(self):
#root = ScreenManager()
#root.add_widget(GirisEkrani(name = "giris_ekrani"))
#root.add_widget(GirisEkrani2(name = "giris_ekrani2"))
#root.add_widget(KontrolEkrani(name = "kontrol_ekrani"))
return self
if __name__ == "__main__":
ekranApp().run()
While people seem to advocate the use of .kv files as opposed to using pure Python, I find it very frustrating to have no errors showing up when something doesn't work.
The build() method of an App is expected to return a Widget which will be the root of the Widget tree for your App, but yours returns the App itself. I suspect that will cause problems. However, your code is not working because you are not setting the names of the Screens. Here is a corrected version of your kv:
Builder.load_string("""
<ekranApp>:
GirisEkrani:
id: ge
name: "giris_ekrani"
Button:
text: "İleri"
on_release: ge.manager.current = ge.manager.next()
GirisEkrani2:
id: ge2
name: "giris_ekrani2"
Button:
text: "Ileri 2"
on_release: ge2.manager.current = ge2.manager.next()
KontrolEkrani:
id: ke
name: "kontrol_ekrani"
Button:
text: "Geri"
on_release: ke.manager.current = ke.manager.previous()
""")
Also, your id for GirisEkrani2 is actually set on the Button.

Python & Kivy: Camera and Coming Texts in Different Screens

I want to build an app with Kivy in Python but I got some errors that I tried to solve many times but I can't.
I'm opening a camera screen firstly. In screen, we see our webcam screen and there is 2 buttons at the bottom (Play and Capture). While I pressing Play, webcam is on and if I press Capture button, I'm taking snapshot. After taking snapshot, we are going to the screen which is located on the left. Special thanks to Erik , he built this working code below.
But when I capture the photo and we are in the left screen, I want to show some text (coming from ANOTHER py file for example basic hello1, hello2... whatever) in left screen.
I know that how can I print basically a sentence in just Console with only print function but I want to write many print function's results in my application on the left screen synchronously from ANOTHER py file.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
import time
class CheckScreen(Screen):
pass
class CameraClickScreen(Screen):
def capture(self):
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
print("Captured")
GUI = Builder.load_string("""
GridLayout:
cols: 1
ScreenManager:
id: screen_manager
CameraClickScreen:
name: "camera_click_screen"
id: camera_click_screen
CheckScreen:
name: "check_screen"
id: check_screen
<CameraClickScreen>:
orientation: 'vertical'
GridLayout:
cols: 1
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()
# root refers to <CameraClickScreen>
# app refers to TestCamera, app.root refers to the GridLayout: at the top
app.root.ids['screen_manager'].transition.direction = 'left'
app.root.ids['screen_manager'].current = 'check_screen'
<CheckScreen>:
Button:
text: "Next Screen"
font_size: 50
""")
class TestCamera(App):
def build(self):
return GUI
TestCamera().run()
Normally with this code, on the left screen, we see "Next Screen" sentence with a button. I want to see variable texts coming from py file. For example Hello 1, and clear the screen, Hello 2, and clear the screen, ..., Hello 5 and stop.
How can I add this basic print functions and integrate with app to show us on the left screen?

Kivy's ScreenManager and Popups don't want to work together

as stated in the title - I'm stuck. I've been playing with the code around and everything works as long as I keep ScreenManager and Popup separate. Once combined - they refuse to cooperate. Anyway, here is the simple app that shows the problem I'm having.
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import Screen, ScreenManager
class First(GridLayout,Screen):
def show_popup(self):
Popp().open()
pass
class Second(Screen):
pass
class Popp(Popup):
pass
class ScreenManagement(ScreenManager):
pass
app = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return app
if __name__ == "__main__":
MainApp().run()
And main.kv file
ScreenManagement:
First:
Second:
<First>:
name:"First"
rows: 2
Button:
text: "FirstButton"
on_release: app.root.current = "Second"
Button:
text: "Show popup"
on_release: root.show_popup()
<Second>:
name:"Second"
Button:
text: "BUTTON"
on_release: app.root.current = "First"
<Popp>:
title: "testing"
text: "Hello world"
size_hint: None,None
size: 400,400
auto_dismiss: False
Button:
text: "Okay"
on_press: root.dismiss()
App starts, first and second screen are working but when trying to get popup up I end up with:
kivy.uix.popup.PopupException: Popup can have only one widget as content
Somehow Screen is seen as a widget inside of Popp? Or am I terribly misinterpreting kivy docs?
It's a bug with loading kv file, it should throw an exception in this case.
What you are doing in the code is loading the kv file twice, what causes some weird behavior. Just delete the Builder.load_file(..) and it will work. The file is going to be loaded automatically.
Also, never do double subclassing of widgets like class First(GridLayout, Screen) as it might lead to some problems. Instead, create a grid layout inside the screen.
Put the elements in the Popup inside a layout, for example: Boxlayout.
Here's what I mean:
<Popp>:
title: "testing"
BoxLayout:
orientation: 'vertical'
size_hint: None,None
size: 400,400
Label:
text: "Hello world"
Button:
text: "Okay"
on_press: root.dismiss()
I have same problem with using kivy Builder.load_file and Popup, they dont work together.
the solution is simple, build popup in python code side. this is a loading popup example:
python:
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
import time, threading
buildKV = Builder.load_file("example.kv")
class ExampleApp(App):
def show_popup(self):
content = BoxLayout(orientation= "vertical")
image=Image(source= 'files/loading.gif', anim_delay= 0)
label=Label(text= 'Model is Running.\nBe Patient Please.')
content.add_widget(image)
content.add_widget(label)
self.popup = Popup(title='Model is Running.',
size_hint=(.250, .785),
content=content, auto_dismiss=False)
self.popup.open()
def process_button_click(self):
# Open the pop up
self.show_popup()
# Call some method that may take a while to run.
# I'm using a thread to simulate this
mythread = threading.Thread(target=self.something_that_takes_5_seconds_to_run)
mythread.start()
def something_that_takes_5_seconds_to_run(self):
thistime = time.time()
while thistime + 10 > time.time(): # 5 seconds
time.sleep(1)
# Once the long running task is done, close the pop up.
self.pop_up.dismiss()
if __name__ == "__main__":
ExampleApp().run()
kivy:
BoxLayout:
Button:
height: 40
width: 100
size_hint: (None, None)
text: 'Click Me'
on_press: app.process_button_click()

How I can use Kivy (Python) camera

I try to use uix.camera widget and show some wideo from my web-camera.
I looked into the documentation and try to use this simply code. But it's just show me a white creen withoud any video (I enabled playing).
What I'm doing wrong?
Maybe some useful docs\tutorial exist (because from official documentation I understanding a little from many). Thanks for any help.
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.camera import Camera
class MainApp(App):
def build(self):
return Camera(play=True)
if __name__== "__main__":
MainApp().run()
You need to specify resolution. In my case, I also needed to specify index=1, that is the second camera plugged in my computer.
Example:
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.camera import Camera
class MainApp(App):
def build(self):
return Camera(play=True, index=1, resolution=(640,480))
if __name__== "__main__":
MainApp().run()
It seems like you need to set resolution=[x, y] property besides play=True, because the default one doesn't work.
The following are the Kivy examples to use camera.
from kivy.app import App
from kivy.lang import Builder
kv = '''
BoxLayout:
orientation: 'vertical'
Camera:
id: camera
resolution: 399, 299
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: '48dp'
Button:
text: 'Start'
on_release: camera.play = True
Button:
text: 'Stop'
on_release: camera.play = False
'''
class CameraApp(App):
def build(self):
return Builder.load_string(kv)
if __name__ == '__main__':
CameraApp().run()
Example 2:-
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_" + timestr)
print("Captured")
class TestCamera(App):
def build(self):
return CameraClick()
TestCamera().run()
I just faced the same problem and found out that Kivy is very slow when creating widget for usb webcam device.
If you have set the index and other parameters properly, maybe just waiting a little longer for Kivy to create the video widget then you could see the webcam view shows in the window, but I am still trying to find out the reason why Kivy takes so long time (about a minute) to create usb webcam widget, hope someone could give some advices about this problem.
I tried using the code as suggested by #Thiago. It didnt work, I suspect its not detecting my USB camera that I have plugged into my Raspberry Pi4.
Following is the code I am using and I also ran a query in the terminal to obtain the camera type that is plugged in (only one plugged in).
The Code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.camera import Camera
class MainApp(App):
def build(self):
return Camera(play=True, index=0, resolution=(640,480))
if __name__== "__main__":
MainApp().run()
Terminal Output:
pi#raspberrypi:~ $ v4l2-ctl --list-formats
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
[1]: 'YUYV' (YUYV 4:2:2)

Categories