-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf_set_webcam.py
More file actions
58 lines (50 loc) · 1.83 KB
/
f_set_webcam.py
File metadata and controls
58 lines (50 loc) · 1.83 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
import cv2
from kivy.clock import Clock
from kivymd.app import MDApp
from kivy.graphics.texture import Texture
from kivymd.uix.screen import MDScreen
KV = """
<SetWebcam>:
MDBoxLayout:
orientation: 'vertical'
Image:
id: image
MDPersianLabel:
id: notification
size_hint: 1, .15
MDRectangleFlatIconButton:
button_text: app.language_dialogs["try_again"]
icon: "reload"
size_hint: 1, .15
on_release: root.retry()
MDRectangleFlatIconButton:
button_text: app.language_dialogs["exit"]
icon: "location-exit"
size_hint: 1, .15
on_release: root.manager.current = "home"
"""
class SetWebcam(MDScreen):
def __init__(self, **kwargs):
super(SetWebcam, self).__init__(**kwargs)
self.capture = cv2.VideoCapture("")
Clock.schedule_interval(self.update, 1 / 10)
def on_enter(self, *args):
self.capture = cv2.VideoCapture(0)
return super().on_enter(*args)
def on_leave(self, *args):
self.capture = cv2.VideoCapture("")
return super().on_leave(*args)
def retry(self):
self.capture = cv2.VideoCapture(0)
def update(self, dt):
if self.capture.isOpened():
self.ids.notification.label_text = MDApp.get_running_app().language_dialogs["webcam_is_connected"]
else:
self.ids.notification.label_text = MDApp.get_running_app().language_dialogs["webcam_error2"]
ret, frame = self.capture.read()
if ret:
buf = cv2.flip(frame, 0).tostring()
texture1 = Texture.create(
size=(frame.shape[1], frame.shape[0]), colorfmt="bgr")
texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.ids.image.texture = texture1