将锉刀 CCTV 视频流式传输到 TKInter 中的标签中

问题描述

我有一个 RaspBerry Pi 显示屏项目正在运行,通过 rtsp 将 2 个 CCTV 馈送显示到几个 Tkinter 标签中,问题是它太慢并且在实时馈送后面长达 10 秒。经过研究,我现在知道我必须将实际捕获的帧与主线程分开以加快速度,但即使阅读了几个教程后我也无法让它工作。

这有效但很慢,显然它的阻塞是引入延迟的地方

def garden1():
  cap2 = cv2.VideoCapture("LOCAL URL")
  cap2.set(cv2.CAP_PROP_BUFFERSIZE,3)
  while True:
    try:
      if cap2.isOpened():
         status,frame = cap2.read()
         if status:
           frame = cv2.resize(frame,(350,200)) 
           cv2image = cv2.cvtColor(frame,cv2.COLOR_BGR2RGBA)
           img = Image.fromarray(cv2image)
           imgtk = ImageTk.PhotoImage(image=img)
           gardenlbl.imgtk = imgtk
           gardenlbl.configure(image=imgtk)
         else:
           t_stamp = error_timestamp()
           logging.info(t_stamp +"  Garden Videostream restarted")
           cap2 = cv2.VideoCapture("LOCAL URL")
           continue
    except cv2.error as e:
       print ("Something went wrong with garden video")
       pass

我知道我必须沿着这条路走下去,所以设置一个我可以用于每个提要的类,然后一个单独的线程来读取帧并将它们放入标签 - 下面的代码片段:

def garden():
  while True:
    try:
      frame = video_stream2.read()
      cv2image = cv2.cvtColor(frame,cv2.COLOR_BGR2RGBA)
      img = Image.fromarray(cv2image)
      imgtk = ImageTk.PhotoImage(image=img)
      gardenlbl.imgtk = imgtk
      gardenlbl.configure(image=imgtk)
      time.sleep(1)
    except cv2.error as e:
      print (str(e)  + "Something went wrong with garden video")
      pass

class streamvideo(object):
  def __init__(self,src=0):
        self.capture = cv2.VideoCapture(src)
        self.thread = Thread(target=self.update,args=())
        self.thread.daemon = True
        self.thread.start()
        print ("thread started")

  def update(self):
        print ("Update started")
        while True:
          if self.capture.isOpened():
            (self.status,self.frame) = self.capture.read()

  def read(self):
        if self.status:
          self.frame = self.resize_frame(self.frame,width=350)
          return self.frame

  def resize_frame(self,image,width=None,height=None,inter=cv2.INTER_AREA):
        dim = None
        (h,w) = image.shape[:2]
        if width is None and height is None:
            return image
        if width is None:
            r = height / float(h)
            dim = (int(w * r),height)
        else:
            r = width / float(w)
            dim = (width,int(h * r))
        return cv2.resize(image,dim,interpolation=inter)

stream_garden = local url
video_stream2 = streamvideo(stream_garden)
t_e = threading.Thread(target=garden,name="Garden",daemon=True)
t_e.start()

我目前遇到的错误是:

Update started
thread started
Exception in thread Garden:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py",line 917,in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py",line 865,in run
    self._target(*self._args,**self._kwargs)
  File "monitor2.py",line 422,in garden
    frame = video_stream2.read()
  File "monitor2.py",line 448,in read
    if self.status:
AttributeError: 'streamvideo' object has no attribute 'status'

我总是避免上课,因为我总是发现自己绊倒了 - 谁能提出任何改进建议?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)