-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKivy_app_cv2_camera.py
More file actions
70 lines (61 loc) · 2.39 KB
/
Kivy_app_cv2_camera.py
File metadata and controls
70 lines (61 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import threading
from kivy.app import App
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.uix.image import Image
from kivy.graphics.texture import Texture
from kivy.uix.button import Button
from functools import partial
from kivy.uix.boxlayout import BoxLayout
import cv2
class CameraApp(App):
#def __init__(self, **kwargs):
#super(CameraApp, self).__init__(**kwargs) # Needed.
def build(self):
def exitvid(*args):
self.stop_vid()
exit()
#Vertical Boxlayout for camera and exitButton
self.camerabox = BoxLayout(orientation='vertical')
#Videoscreen
self.vid = Image(allow_stretch=False, keep_ratio=True)
#Exitbutton
self.exitButton = Button(text="EXIT", background_color=(1, 0, 1, 1), size_hint=(None, None), size=(Window.width, 100), font_size="30sp", padding=(20, 20))
#Bind exitbutton to function
self.exitButton.bind(on_press=exitvid)
#Adding widgets to box
self.camerabox.add_widget(self.vid)
self.camerabox.add_widget(self.exitButton)
#variable to cut while-loop in runvideo
self.do_vid = True
#Starting thread for cv2 capture
threading.Thread(target=self.runvideo, daemon=True).start()
#return the camerabox_widget
return self.camerabox
#cv2 Capture
def runvideo(self):
#Capturing from source, ex: 'rtsp://username:password@172.0.0.1/live1.sdp'
cap = cv2.VideoCapture('rtsp://username:password@IP-Address/live1.sdp')
#cv2-loop
while (self.do_vid):
ret, frame = cap.read()
#initiating display_frame once for each loop
Clock.schedule_once(partial(self.display_frame, frame))
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
#stop_while-loop in runvideo()
def stop_vid(self):
self.do_vid = False
#creating texture to send to Image Object.
def display_frame(self, frame, dt):
texture = Texture.create(size=(frame.shape[1],frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(frame.tobytes(order=None), colorfmt='bgr', bufferfmt='ubyte')
texture.flip_vertical()
self.exitButton.size[0] = self.vid.size[0]
self.vid.texture = texture
#Main_Loop
if __name__ == '__main__':
application = CameraApp()
Window.fullscreen = False
application.run()