from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen from…
- User
from kivy.app import App from kivy.uix.screenmanager import ScreenManager, Screen from kivy.uix.button import Button from kivy.uix.image import Image from kivy.uix.boxlayout import BoxLayout from kivy.uix.behaviors import ButtonBehavior from kivy.graphics.texture import Texture from kivy.clock import Clock from kivy.metrics import dp from kivy.properties import StringProperty import JsonUtl as jsonutil import cv2 import multiprocessing
class ButtonImage(ButtonBehavior, Image): pass
class MainScreen(Screen): ServerListVar = jsonutil.OpenJsontoArray() BtnId = 0 DelBtnId = 0
codedef __init__(self, **kw): super().__init__(**kw) for i in range(0, len(self.ServerListVar)): self.CreateButtons(i) def Clicked(self, widget): Adress = self.ids.StreamAdress if not Adress.text == "": self.ServerListVar.append(Adress.text.strip()) jsonutil.SaveArraytoJson(self.ServerListVar) self.CreateButtons(len(self.ServerListVar) - 1) Adress.text = "" def CamSelectButtonPressed(self, x): app = App.get_running_app() app.data = self.ServerListVar[x] app.root.current = "Stream" def CamDeleteButtonPressed(self, x): del self.ServerListVar[x] self.ids.CamSelectStackLayout.clear_widgets() jsonutil.SaveArraytoJson(self.ServerListVar) self.ServerListVar = jsonutil.OpenJsontoArray() self.BtnId = 0 self.DelBtnId = 0 for i in range(0, len(self.ServerListVar)): self.CreateButtons(i) def CreateButtons(self, i): box = BoxLayout(size_hint=(None, None), width=dp(200), height=dp(200), orientation="vertical") bimage = ButtonImage(source="ConnectionError.png") b2 = ButtonImage(size_hint=(None, None), width=dp(200), height=dp(50)) b2.source = "DeleteButton.png" bimage.bind(on_press=lambda instance, button_id=self.BtnId: self.CamSelectButtonPressed(button_id)) self.BtnId = self.BtnId + 1 b2.bind(on_press=lambda instance, button_id=self.DelBtnId: self.CamDeleteButtonPressed(button_id)) self.DelBtnId = self.DelBtnId + 1 # call capture in a separate process process = multiprocessing.Process(target=self.capture_video, args=(self.ServerListVar[i], bimage)) process.start() self.processes.append(process) box.add_widget(bimage) box.add_widget(b2) self.ids.CamSelectStackLayout.add_widget(box) def capture_video(self, url, widget): capture = cv2.VideoCapture(url, cv2.CAP_FFMPEG, [cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY]) while True: ret, frame = capture.read() if ret: # update the widget texture on the main process Clock.schedule_once(lambda dt: self.update_texture(frame, widget)) break else: capture.release() break def update_texture(self, frame, widget): buf = cv2.flip(frame, 0).tobytes() texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt="bgr") texture.blit_buffer(buf, colorfmt="bgr", bufferfmt="ubyte") widget.texture = texture - Assistant
There are several issues with this code:
- In
if __name__ == "main", "main" should be "main" (with double underscores). - The import statement
import JsonUtl as jsonutilis causing errors because no module namedJsonUtlis found. This module needs to be created or the import statement needs to be modified. - The
processesattribute is not defined in theMainScreenclass before being used in theon_stopmethod. - The
gotomainscreenmethod is defined insideon_enterofStreamViewbut never called. - In
on_enterofStreamView, the widget hierarchy is not properly updated. TheBoxLayoutis not added to the screen, causing the and widgets to not be visible.
- In