class StreamView(Screen):
output = StringProperty()
capture = None
code
def on_enter(self):
app = App.get_running_app()
self.output = app.data
self.capture = cv2.VideoCapture(self.output) # RTSP URL
if not self.capture.isOpened():
b = Button(text="Unable to connect to stream. Go back to Main Screen.")
b.bind(on_press=self.gotomainscreen)
self.add_widget(b)
else:
self.image = Image()
self.add_widget(self.image)
Clock.schedule_interval(self.update, 1.0 / 30.0) # 30 fps
def update(self, dt):
ret, frame = self.capture.read()
if ret:
buf = cv2.flip(frame, 0).tostring() # flip & convert to bytes
texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.image.texture = texture
def on_leave(self):
self.capture.release()
def gotomainscreen(self, *args):
app = App.get_running_app()
app.root.current = "Main"
User
self.capture = cv2.VideoCapture(self.output) # RTSP URL this line freezes entire program if url is incorrect
Assistant
Yes, that's because cv2.VideoCapture() waits for data to be received from the RTSP server before it determines if the URL is valid or not. This can take some time and appears to freeze the program.
One solution is to run the cv2.VideoCapture() in a separate thread or use asyncio to avoid blocking the main thread. Here's an example using threads:
code
User
AttributeError: 'StreamView' object has no attribute 'image'
Assistant
This error occurs because the "image" property is not created before the update() method tries to access it.
To fix this, you can add an if condition at the beginning of the update() method to check if the "image" property exists before updating the texture. Here is the updated code:
code
User
TypeError: Cannot create graphics instruction outside the main Kivy thread
Assistant
This error occurs because the update() method is accessing the UI (specifically, the Image widget) from a different thread than the main Kivy thread.
To fix this, you can use Kivy's Clock module to schedule the update() method to be called from the main thread. Here's the updated code:
code
import threading
class StreamView(Screen):
output = StringProperty()
capture = None
thread = None
def on_enter(self):
app = App.get_running_app()
self.output = app.data
self.thread = threading.Thread(target=self.open_video, args=())
self.thread.start()
def open_video(self):
self.capture = cv2.VideoCapture(self.output) # RTSP URL
if not self.capture.isOpened():
self.show_error()
else:
Clock.schedule_interval(self.update, 1.0 / 30.0) # 30 fps
self.show_video()
def show_video(self):
self.image = Image()
self.add_widget(self.image)
def show_error(self):
b = Button(text="Unable to connect to stream. Go back to Main Screen.")
b.bind(on_press=self.gotomainscreen)
self.add_widget(b)
def update(self, dt):
ret, frame = self.capture.read()
if ret:
buf = cv2.flip(frame, 0).tostring() # flip & convert to bytes
texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.image.texture = texture
def on_leave(self):
self.capture.release()
def gotomainscreen(self, *args):
app = App.get_running_app()
app.root.current = "Main"
With this implementation, open_video() is run in a separate thread so that the UI remains responsive while waiting for cv2.VideoCapture() to determine if the URL is valid. If the URL is invalid, show_error() is called instead of creating the video widget.
Alternatively, you could use asyncio with cv2.VideoCapture() if you prefer.
class StreamView(Screen):
output = StringProperty()
capture = None
image = None
def on_enter(self):
app = App.get_running_app()
self.output = app.data
self.capture = cv2.VideoCapture(self.output) # RTSP URL
if not self.capture.isOpened():
b = Button(text="Unable to connect to stream. Go back to Main Screen.")
b.bind(on_press=self.gotomainscreen)
self.add_widget(b)
else:
self.image = Image()
self.add_widget(self.image)
Clock.schedule_interval(self.update, 1.0 / 30.0) # 30 fps
def update(self, dt):
if self.image:
ret, frame = self.capture.read()
if ret:
buf = cv2.flip(frame, 0).tostring() # flip & convert to bytes
texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.image.texture = texture
def on_leave(self):
self.capture.release()
def gotomainscreen(self, *args):
app = App.get_running_app()
app.root.current = "Main"
With this change, the update() method will only attempt to update the texture if the "image" property has been created.
from kivy.clock import Clock
class StreamView(Screen):
output = StringProperty()
capture = None
image = None
def on_enter(self):
app = App.get_running_app()
self.output = app.data
self.capture = cv2.VideoCapture(self.output) # RTSP URL
if not self.capture.isOpened():
b = Button(text="Unable to connect to stream. Go back to Main Screen.")
b.bind(on_press=self.gotomainscreen)
self.add_widget(b)
else:
self.image = Image()
self.add_widget(self.image)
Clock.schedule_interval(self.update, 1.0 / 30.0) # 30 fps
def update(self, dt):
ret, frame = self.capture.read()
if ret:
buf = cv2.flip(frame, 0).tostring() # flip & convert to bytes
texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
# use Clock.schedule_once to call the setter method from the main thread
Clock.schedule_once(lambda dt: setattr(self.image, 'texture', texture), 0)
def on_leave(self):
self.capture.release()
def gotomainscreen(self, *args):
app = App.get_running_app()
app.root.current = "Main"
Instead of updating the Image widget directly from the update() method, this code schedules the texture update to be called from the main thread using Clock.schedule_once(). This ensures that all UI updates are done from the main thread and avoids errors related to accessing UI elements from another thread.