python 降低cpu_如何在获取帧的同时降低cpu使用率

这篇博客介绍了一个通过FFmpeg在Python中读取RTSP流并降低CPU使用率的方法。作者创建了一个`ffmpeg_videocapture`类,该类利用FFmpeg将视频流转换为管道输出的原始图像,然后用numpy处理成帧。通过在子进程中运行,实现了从多个RTSP流中同时获取帧,并在读取时有效地管理资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我通过直接使用FFmpeg读取帧来提高速度。也许有更好的方法来做到这一点,但这是一种认可。我想你已经在不同的过程中开始了视频捕获,对吗?否则,这也是明智的一步。在import numpy

from subprocess import Popen, PIPE

import cv2

class ffmpeg_videocapture:

def __init__(self, stream, width=640, height=360, scale=1, fps=15):

self.command = 'ffmpeg -rtsp_transport udp -i {i} -pix_fmt bgr24 -s {w}x{h} -vcodec rawvideo ' \

'-an -sn -r {fps} -f image2pipe pipe:1'

self.stream = stream

self.width = width

self.height = height

self.scale = scale

self.fps = fps

self.errors = []

self.start()

def start(self):

width = int(self.width * self.scale)

height = int(self.height * self.scale)

command = self.command.format(i=self.stream, w=width, h=height, fps=self.fps)

self.capture = Popen(command.split(' '), stdout=PIPE, stderr=PIPE, bufsize=10 ** 8)

def read(self):

width = int(self.width * self.scale)

height = int(self.height * self.scale)

raw_image = self.capture.stdout.read(width * height * 3)

frame = numpy.fromstring(raw_image, dtype='uint8')

frame = frame.reshape((height, width, 3))

self.capture.stdout.flush()

return frame is not None, frame

def __exit__(self, exc_type, exc_val, exc_tb):

self.capture.terminate()

def single_camera(rtsp_stream):

cap = ffmpeg_videocapture(rtsp_stream)

while True:

ret, frame = cap.read()

cv2.imshow("frame", frame)

if __name__ == "__main__":

rtsp_stream_list = [

"rtsp_stream_1",

"rtsp_stream_2",

"etc...",

]

for rtsp_stream in rtsp_stream_list:

t = Process(target=single_camera, args=rtsp_stream)

t.start()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值