提问人:DK kim 提问时间:11/6/2023 最后编辑:DK kim 更新时间:11/6/2023 访问量:54
如何减少 emgu.cv RTSP 播放器中的高 CPU 使用率?
how to reduce high Cpu Usage in emgu.cv RTSP Player?
问:
我是使用 C# 的新手开发人员。我制作了用户控件来播放 emgu.cv 的 rtsp。我将此用户控件用于播放 9 rtsp 的主程序。但是当我使用我的用户控件 9 次时,它使我的 CPU 溢出,超过 70% 的 CPU 使用率,还有 1.3GB 的内存使用率。我不知道我的代码出了什么问题。请帮助我任何在 C# 中使用过 emgu.cv 的人。只播放一个 RTSP 并不重要。 还要告诉我,如果我的代码看起来很糟糕,如何修复它! 我不知道原因。请尽快帮助我! 这是我的代码
capture = new VideoCapture(rtspUrl, VideoCapture.API.Ffmpeg);
capture.SetCaptureProperty(CapProp.Fps, frame);
capture.Start();
Task.Run(async () =>
{
while (isPlaying)
{
UpdateFrame();
await Task.Delay(1000 / frame);
Thread.Sleep(1);
}
});
}
using (Mat frame = new Mat())
{
capture.Retrieve(frame, 0);
if (!frame.IsEmpty)
{
using (Image<Bgr, byte> image =
frame.ToImage<Bgr, byte>())
{
lock (captureLock)
{
try
{
if (!isRecording && videoWriter !=null)
videoWriter.Dispose();
else if (isRecording && videoWriter != null)
{
if(frame!=null)
videoWriter.Write(frame);
}
}
catch (System.AccessViolationException) { }
}
pictureBoxRTSP1.Invoke((MethodInvoker)(() =>pictureBoxRTSP1.Image = image.ToBitmap()));
}
}
else
{
}
}
答: 暂无答案
评论
isPlaying
if(frame!=null)
对我来说没有意义。如果是 ,则调用 应导致 NullReferenceException。另外: - 我希望这只是为了问题的简洁。你永远不应该有一个空的(除了极少数例外,你应该在里面留下一个评论,说明为什么在这种情况下故意是空的)。null
frame.ToImage
catch (System.AccessViolationException) { }
catch
M-x untabify