【问题标题】:Integrate OpenCV webcam into a Kivy user interface将 OpenCV 网络摄像头集成到 Kivy 用户界面中
【发布时间】:2016-10-11 11:43:56
【问题描述】:

我当前的程序是 Python 并使用 OpenCV。我依靠网络摄像头捕获,并且正在处理每个捕获的帧:

import cv2

# use the webcam
cap = cv2.VideoCapture(0)
while True:
    # read a frame from the webcam
    ret, img = cap.read()
    # transform image

我想制作一个带有按钮的 Kivy 界面(或其他图形用户界面),同时通过网络摄像头捕获现有功能。

我找到了这个例子: https://kivy.org/docs/examples/gen__camera__main__py.html — 但它没有解释如何获取网络摄像头图像以使用 OpenCV 对其进行处理。

我找到了一个较旧的示例: http://thezestyblogfarmer.blogspot.it/2013/10/kivy-python-script-for-capturing.html — 它使用“屏幕截图”功能将屏幕截图保存到磁盘。然后我可以读取保存的文件并进行处理,但这似乎是不必要的步骤。

我还能尝试什么?

【问题讨论】:

    标签: python user-interface opencv kivy


    【解决方案1】:

    在这里找到这个例子:https://groups.google.com/forum/#!topic/kivy-users/N18DmblNWb0

    它将 opencv 捕获转换为 kivy 纹理,因此您可以在将其显示到您的 kivy 界面之前进行各种 cv 转换。

    __author__ = 'bunkus'
    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.image import Image
    from kivy.clock import Clock
    from kivy.graphics.texture import Texture
    
    import cv2
    
    class CamApp(App):
    
        def build(self):
            self.img1=Image()
            layout = BoxLayout()
            layout.add_widget(self.img1)
            #opencv2 stuffs
            self.capture = cv2.VideoCapture(0)
            cv2.namedWindow("CV2 Image")
            Clock.schedule_interval(self.update, 1.0/33.0)
            return layout
    
        def update(self, dt):
            # display image from cam in opencv window
            ret, frame = self.capture.read()
            cv2.imshow("CV2 Image", frame)
            # convert it to texture
            buf1 = cv2.flip(frame, 0)
            buf = buf1.tostring()
            texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr') 
            #if working on RASPBERRY PI, use colorfmt='rgba' here instead, but stick with "bgr" in blit_buffer. 
            texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
            # display image from the texture
            self.img1.texture = texture1
    
    if __name__ == '__main__':
        CamApp().run()
        cv2.destroyAllWindows()
    

    【讨论】:

    • 使用这种方法并隐藏 cv 窗口使应用程序加载速度比 kivy cam 示例更快。
    【解决方案2】:

    注意:我不知道 OpenCV 是如何工作的,但我找到了camera_opencv.py,所以这意味着有一种简单的方法可以使用它。

    正如您在相机示例中看到的,这是默认方式,当您在 __init__.py 中查看相机时,您可以看到 opencv in providers,因此它可能与 OpenCV 一起使用。如果您可以看到 OpenCV 被检测为提供程序,请检查日志。如果检测到CameraOpenCV,您应该会在某处看到它,并且它应该在捕获图像时显示出来。

    如果您想直接使用 OpenCV(即 cap.read() 和类似的东西),那么您需要为提供程序编写自己的处理程序或将更多选项附加到 camera_opencv 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多