该方法通过基于 cv2.VideoCapture 能够获取实时帧,能够解决由于图像处理速度不够等原因造成帧堆积的问题。
import cv2 from threading import Thread class ThreadedCamera(object): def __init__(self, source=0): self.capture = cv2.VideoCapture(source) self.thread = Thread(target=self.update, args=()) self.thread.daemon = True self.thread.start() self.status = False self.frame = None def update(self): while True: if self.capture.isOpened(): (self.status, self.frame) = self.capture.read() def grab_frame(self): if self.status: return self.frame return None if __name__ == '__main__': stream_link = "http://livebroadcast.ccwb.cn/live/w1627026956756903.m3u8" streamer = ThreadedCamera(stream_link) while True: frame = streamer.grab_frame() if frame is not None: cv2.imshow("Context", frame) cv2.waitKey(1)