【问题标题】:Manually trying to take picture doesn't work手动尝试拍照不起作用
【发布时间】:2021-04-30 02:12:39
【问题描述】:

基本上我有这个代码:


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()

它应该做的是:

  1. 运行窗口
  2. 在窗口中显示网络摄像头
  3. 使用网络摄像头反复拍照并保存为hello.png

它在做什么:

  1. 运行窗口
  2. 在窗口中显示网络摄像头

我的代码有什么问题?是否与尝试访问另一个类中的函数有关?或者它是一个参数错误?如果是,我在日志消息中看不到任何错误。

【问题讨论】:

    标签: python class kivy


    【解决方案1】:

    问题是您试图在MainWindow().run() 之后保存图像,直到App 停止后才会返回。尝试修改您的 build() 方法以使用 Clock.schedule_interval 保存图像:

    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
    

    【讨论】:

      【解决方案2】:

      尝试使用同一个窗口

      if __name__ == '__main__':
          win = MainWindow()
          win.run()
          while True:
              time.sleep(1)
              win.onCameraClick()
      

      【讨论】:

      • 嗯,这不起作用,但我发现了一些东西,在我尝试关闭我的标签后,它开始说“成功”,这意味着它开始执行 while 循环。这让我觉得它试图在窗口关闭后启动 while 循环,这就是为什么它在窗口处于活动状态时没有激活。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多