将OpenCV网络摄像头集成到Kivy用户界面中

时间:2016-06-10 13:20:18

标签: python user-interface opencv kivy

我目前的程序是使用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 - 它使用“屏幕截图”功能将屏幕截图保存到磁盘。然后我可以读取保存的文件并处理它们,但这似乎是一个不必要的步骤。

我还能尝试什么?

3 个答案:

答案 0 :(得分:3)

在此处找到此示例: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')
        texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
        # display image from the texture
        self.img1.texture = texture1

if __name__ == '__main__':
    CamApp().run()
    cv2.destroyAllWindows()

答案 1 :(得分:2)

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

正如您在相机示例中所看到的,这是默认方式,当您在__init__.py查看相机时,您可以看到opencv in providers,因此它可能适用于开箱即用的OpenCV。检查日志是否可以看到OpenCV被检测为提供者。如果检测到CameraOpenCV,则应该看到cap.read(),并且在捕获图像时应该显示自己。

如果你想直接使用OpenCV(即camera_opencv和类似的东西),那么你需要为提供者编写自己的处理程序或向declare @percent float set @percent = 0.0000000000000000015; select distinct t.coin,t.date, (t1.high24hr-t.high24hr)/100.0 as high, (t2.low24hr-t.low24hr)/100.0 as low from YourTable t left join YourTable t1 on t.coin=t1.coin and t.date>t1.date and (t1.high24hr-t.high24hr)/100.0>@percent left join YourTable t2 on t.coin=t2.coin and t.date>t2.date and (t2.low24hr-t.low24hr)/100.0>@percent 文件添加更多选项。

答案 2 :(得分:2)

您可以查看http://www.pyimagesearch.com/2016/02/29/saving-key-event-video-clips-with-opencv/。另请查看Adrian在其网站上发布的所有其他帖子。许多实际例子。