Skip to content

Commit 1590585

Browse files
committed
kivy_cv1
1 parent b69f818 commit 1590585

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2018/2/8 16:30
3+
# @Author : play4fun
4+
# @File : kivy_cv1.py
5+
# @Software: PyCharm
6+
7+
"""
8+
kivy_cv1.py:
9+
https://gist.github.com/ExpandOcean/de261e66949009f44ad2
10+
11+
pip install kivy
12+
13+
无显示
14+
"""
15+
16+
# coding:utf-8
17+
fromkivy.appimportApp
18+
fromkivy.uix.imageimportImage
19+
fromkivy.clockimportClock
20+
fromkivy.graphics.textureimportTexture
21+
importcv2
22+
23+
24+
classKivyCamera(Image):
25+
def__init__(self, capture, fps, **kwargs):
26+
super(KivyCamera, self).__init__(**kwargs)
27+
self.capture=capture
28+
Clock.schedule_interval(self.update, 1.0/fps)
29+
30+
defupdate(self, dt):
31+
ret, frame=self.capture.read()
32+
ifret:
33+
# convert it to texture
34+
buf1=cv2.flip(frame, 0)
35+
buf=buf1.tostring()
36+
image_texture=Texture.create(
37+
size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
38+
image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
39+
# display image from the texture
40+
self.texture=image_texture
41+
42+
43+
classCamApp(App):
44+
defbuild(self):
45+
self.capture=cv2.VideoCapture(1)
46+
self.my_camera=KivyCamera(capture=self.capture, fps=30)
47+
returnself.my_camera
48+
49+
defon_stop(self):
50+
# without this, app will not exit even if the window is closed
51+
self.capture.release()
52+
53+
54+
if__name__=='__main__':
55+
CamApp().run()

0 commit comments

Comments
(0)