Manually trying to take picture doesn't work - python

Basically I have this code:
import time
from kivy.app import App
from kivy.uix.camera import Camera
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class MainWindow(App):
def build(self):
layout = BoxLayout(orientation='vertical')
# Create a camera object
self.cameraObject = Camera(play=False)
self.cameraObject.play = True
self.cameraObject.resolution = (300, 300) # Specify the resolution
# add camera and button to the layout
layout.add_widget(self.cameraObject)
# return the root widget
return layout
# Take the current frame of the video as the photo graph
def onCameraClick(self, *args):
self.cameraObject.export_to_png('hello.png')
print("Success")
if __name__ == '__main__':
MainWindow().run()
while True:
time.sleep(1)
MainWindow().onCameraClick()
What it's supposed to do is:
Run the window
Show the webcam in window
Repeatedly take pictures using the webcam and save it as hello.png
What's it's doing:
Run the window
Show the webcam in window
What's wrong with my code? Is it something to do with trying to access a function inside another class? Or is it an argument error? If it is, I don't see any errors in the log messages.

The problem is that you are trying to save the images after the MainWindow().run(), which does not return until the App is stopped. Try modifying your build() method to use Clock.schedule_interval to save the images:
def build(self):
layout = BoxLayout(orientation='vertical')
# Create a camera object
self.cameraObject = Camera(play=False)
self.cameraObject.play = True
self.cameraObject.resolution = (300, 300) # Specify the resolution
# add camera and button to the layout
layout.add_widget(self.cameraObject)
# save some images
Clock.schedule_interval(self.onCameraClick, 2)
# return the root widget
return layout

try using the same window
if __name__ == '__main__':
win = MainWindow()
win.run()
while True:
time.sleep(1)
win.onCameraClick()

Related

VideoPlayer error loading video only plays sound - Warning: Removing channel layout 0x3, redundant with 2 channels

I am trying to use VideoPlayer to load a local video. The program runs fine when it is standalone (in its own file). But, when I bring it into my main program, it loads the video but only plays the sound. I get an error (warning, to be more precise) message:
[WARNING] [ffpyplayer ] [ffpyplayer_abuffersink # 000001e84fb55580]
Removing channel layout 0x3, redundant with 2 channels
Here's the standalone version:
import cv2
from kivy.uix.videoplayer import VideoPlayer
from kivymd.app import MDApp
class PlayVid(MDApp):
def build(self):
player = VideoPlayer(source="Roadhouse.mp4")
player.state = "play"
player.options = {"eos": "stop"}
player.allow_stretch = True
return player
if __name__ == '__main__':
PlayVid().run()
And here is the same thing split in functions and class in my main program:
class PlayVid(MDApp):
def playnow(self):
# player = VideoPlayer(source='Roadhouse.mp4')
# player.state = "play"
video = VideoPlayer(source='Roadhouse.mp4')
video.state = "play"
# player.options = {"eos": "stop"}
# player.allow_stretch = True
return video
class SecondWindow(Screen):
def build (self):
sm = ScreenManager()
self.sec_screen = SecondWindow()
sm.add_widget(self.sec_screen)
return sm
def start_play(self):
PlayVid.playnow(self)
A kv button in SecondWindow triggers start_play and then PlayVid.playnow(self). That's all. It runs, loads the file, and then just plays the sound. No video.
I can't understand what I'm doing wrong. Help?
Thanks!
I created a standalone program for it, and it works. I just can't understand why it drops the video and plays the sound when brought into the main program.
Your playnow() method returns the VideoPlayer widget, but that return is ignored. You must add that widget to your GUI in order to see it. Try using this version of start_play():
def start_play(self):
v = MDApp.get_running_app().playnow()
self.add_widget(v)

os.system() don't let updating image source in kivy python

I am trying to update the image source on a changeImageSource function it changes the source instantly but when I use time.sleep() method in that function, function executes but doesn't update the source of the image. updates after time.sleep() call completed.
from kivy.app import App
from kivy.uix.image import AsyncImage
from kivy.uix.button import Button
from kivy.uix.widget import Widget
import time
# creating the App class
class MyApp(App):
def build(self):
parent = Widget()
#creating and adding image to widget
self.img = AsyncImage(
source='http://kivy.org/logos/kivy-logo-black-64.png')
self.img.pos = (400,400)
#creating btn and adding press handler
self.change_img_btn = Button(text="Change Image ")
self.change_img_btn.bind(on_press = self.changeImageSource)
#adding widget to Widget instance
parent.add_widget(self.img)
parent.add_widget(self.change_img_btn)
return parent;
def changeImageSource(self,*args):
self.img.source = "https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a"
time.sleep(4)
# run the App
MyApp().run()
One: The "return parent;" should be "return parent"
Two: Why do you need time.sleep()?
You can also try flipping line 29 and 30.

kivy widgets doesn't appear just show black screen

I am a new learner of the Kivy module.I am practicing making a selfie app by following a video, by clicking on the button, the camera takes a frame and export it as a PNG image. in code I am adding widget (camera, button), and what show is a black screen. so what should I do?
import cv2
from kivy.app import App
from kivy.uix.camera import Camera
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class selfie_app(App):
def make(self):
self.obj_camera = Camera()
self.obj_camera.resolution = (810, 810)
obj_button = Button(text="Click !!")
obj_button.size_hint = (.5,.2)
obj_button.pos_hint = {'x':.15, 'y':.15}
obj_button.bind(on_press = self.selfie_take())
obj_layout = BoxLayout()
obj_layout.add_widget(obj_button)
obj_layout.add_widget(self.obj_camera)
return obj_layout
def selfie_take(self, *args):
print("selfie taken successfully !")
self.obj_camera.export_to_png('./demo_file.png')
if __name__ == '__main__':
selfie_app().run()

Kivy image not showing

Im working on a basic app using python and kivy and im trying to import an image which I'm basically going to use as a splash screen of sorts but for some reason I couldn't get it to work so I created a .py file and wrote some very simple code to try to get it to work:
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
label = Label(source="image.jpg")
return label
if __name__ == "__main__":
ImageTestApp().run()
The image is in the same directory of the .py file and the name of the image is exactly the same, I even made the window size the same as the image size and tried .PNG but nothing worked 😖
Label does not have an attribute called source. You should change Label to Image, and it will works. Please refer to example below for details.
Example
main.py
from kivy.app import App
from kivy.uix.image import Image
from kivy.core.window import Window
Window.size = (360, 640)
class ImageTestApp(App):
def build(self):
return Image(source="image.jpg")
if __name__ == "__main__":
ImageTestApp().run()
Output

Display an image when pressing a button in Qt GUI (Python)

I drew a GUI in Qt Creator, with a button, a slider and some labels.
What I am trying: when the button is pressed, print on terminal and in a label the modified value of the slider and display an image. As many webpages suggested, I am trying to display an image into a label by using the pixmap method. This is my whole code (the structure of the GUI is in the imported mainwindow.ui file)
import sys
from PyQt4 import QtCore, QtGui, uic
qtCreatorFile = "mainwindow.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class myownGUI(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
#button
self.Do_button.clicked.connect(self.action)
#slider
self.SLIDER.valueChanged[int].connect(self.SLIDER_update)
#"global" variable init. by callback
self.SLIDER_update()
#The button callback
def action(self):
print "DOING ACTION!"
print self.Slider
#trying to display the image in the Image_label
image = QtGui.QImage(QtGui.QImageReader(":/images/test.png").read())
self.Image_label.setPixmap(QtGui.QPixmap(image))
#self.Image_label.show() #unuseful command?
#Slider update callback
def SLIDER_update(self):
self.Slider= self.SLIDER.value()
if (self.Slider % 2 == 0): #even
self.Slider = self.Slider + 1
self.Slider_label.setText(str(self.Slider))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = myownGUI()
window.show()
sys.exit(app.exec_())
The code runs, it shows no error but the image is not displayed.
I tried both JPG and PNG images. I tried also the simple image name, when the image is in the same folder.
What is wrong in my code?
There is another way to display images in QT inside the GUI (with python) ?
Thank you in advance
Working with: Ubuntu 14.04 / Qt version 4.8.6
I try to read all similar questions in stack overflow. It seems that my question is duplicated, but none of the answers seems to resolve my problem.
EDIT: Using PRMoureu's syntax it works also when the image is the same folder, like
image = QtGui.QImage(QtGui.QImageReader("./test.png").read())
Now the image is displayed and have only to be rescaled.
You should call the image with another path syntax :
image = QtGui.QImage(QtGui.QImageReader("./images/test.png").read())
or
image = QtGui.QImage(QtGui.QImageReader("images/test.png").read())

Categories