Python 3.x 将rasp CCTV视频流到TKInter的标签中
我有一个Raspberry Pi显示屏项目正在工作,通过rtsp将两个CCTV提要显示到两个Tkinter标签中,问题是它太慢了,在直播提要后长达10秒。在做了研究之后,我现在知道我必须将实际捕获的帧从主线程中分离出来以加快速度,但即使在阅读了几篇教程之后,我也无法让它工作 这是可行的,但速度很慢,很明显,它的阻塞就是引入延迟的地方Python 3.x 将rasp CCTV视频流到TKInter的标签中,python-3.x,tkinter,video-streaming,rtsp,Python 3.x,Tkinter,Video Streaming,Rtsp,我有一个Raspberry Pi显示屏项目正在工作,通过rtsp将两个CCTV提要显示到两个Tkinter标签中,问题是它太慢了,在直播提要后长达10秒。在做了研究之后,我现在知道我必须将实际捕获的帧从主线程中分离出来以加快速度,但即使在阅读了几篇教程之后,我也无法让它工作 这是可行的,但速度很慢,很明显,它的阻塞就是引入延迟的地方 def garden1(): cap2 = cv2.VideoCapture("LOCAL URL") cap2.set(cv2.CAP
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'
我总是避免上课,因为我总是发现自己绊倒了-有人能提出任何改进吗?你确定这一行
(self.status,self.frame)=self.capture.read()
能按预期工作吗?不-我一点也不确定-我知道能工作的函数是garden1()函数,工作正常,但流不是实时的,等等。另外,因为我得到了AttributeError:“streamvideo”对象没有属性“status”错误,我认为没有!在这种情况下,我要做的是在if self.capture.isOpened()行下写一行print('here')
:
如果here是在错误发生之前打印的,您知道错误必须在if
语句之后。但是我假设if语句阻止了你的代码工作,这里根本不会被打印。这里
在出错之前被打印,然后一直打印直到我杀死它?如果这是一个语句而不是一个问题。打印出该函数返回的内容self.capture.read()?