该方法通过基于 cv2.VideoCapture 能够获取视频流入(rtmp/rtsp等)实时帧,能够解决由于图像处理速度不够等原因造成帧堆积的问题。

class ThreadedCamera(object):

def init(self, source=0):

global cap

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.status,self.frame)

return (None,None)

streamer = ThreadedCamera(args.video)

while True:#cv.waitKey(1) != 'q':

has_frame, show = streamer.grab_frame()

相关推荐
凌叁儿1 小时前
python保留关键字详解
开发语言·python
意.远2 小时前
PyTorch实现二维卷积与边缘检测:从原理到实战
人工智能·pytorch·python·深度学习·神经网络·计算机视觉
一个小猴子`2 小时前
FFMpeg视频编码实战和音频编码实战
ffmpeg·音视频
勤劳的进取家2 小时前
贪心算法之最小生成树问题
数据结构·python·算法·贪心算法·排序算法·动态规划
兮兮能吃能睡2 小时前
Python中的eval()函数详解
开发语言·python
三道杠卷胡3 小时前
【AI News | 20250411】每日AI进展
人工智能·python·计算机视觉·语言模型·aigc
前端开发张小七3 小时前
16.Python递归详解:从原理到实战的完整指南
前端·python
去看日出3 小时前
Photoshop2025最新版v26超详细图文安装教程(附安装包)
图像处理·photoshop
前端开发张小七3 小时前
15.Python正则表达式入门:掌握文本处理的利器
前端·python
odoo中国3 小时前
Python 深度学习实战 第1章 什么是深度学习&代码示例
开发语言·python·深度学习