0

我们正在开发一种多传感器采集工具,但在将图像(来自网络摄像头)保存到硬盘时遇到了问题。

为此,我们使用了三个线程:

  • 一个线程不断地从网络摄像头收集捕获的图像。
  • 一个计时器线程,它收集最后一张图像并将其发送到文件保存方法中。
  • 定时器线程调用第三个线程进行保存,以免干扰主定时器线程的功能。

这适用于低频。当我们将 FPS 提高到 30 左右时,我们开始丢失图像。请注意,我们有多个传感器,而不仅仅是一个网络摄像头。这就是我们使用这种架构而不是直接从网络摄像头线程保存文件的原因(我们需要保持一切同步)

这是 save 方法的当前实现:

private void saveImageFrame(Bitmap b, ulong frameID)
    {
        string fileSavePath = _path+ "//";
        if (b != null)
        {
            Task.Factory.StartNew(() =>
            {
                lock (_lock)
                {
                    Bitmap toSave = new Bitmap(b);
                    string fileName = fileSavePath + frameID.ToString() + ".bmp";
                    toSave.Save(fileName);
                }
            });
        }
    }

我们还尝试不使用任务线程(用于保存)并且不使用锁。这两个导致竞争条件,因为保存比计时器时间间隔花费更多的时间。

我确信在架构和 .NET 功能方面都有更好的方法来做到这一点。任何有助于提高性能的帮助将不胜感激!

4

1 回答 1

4

It's quite possible that your disk just isn't fast enough. A really fast disk subsystem can sustain a write speed of perhaps 100 megabytes per second, and that's if you already have the file open. You're trying to create 30 or more files per second, which in itself is a pretty big load on the system. Add the time required to write the data, and you're pushing the file system's capabilities.

That problem will get worse as the number of files in a folder increases. If you have, say, 1,000 files in the folder, things work pretty quickly. Put 10,000 files in a single folder and you'll find that it takes a long time to create a new file in that folder.

If you are bumping up against hardware performance limitations, you have several courses of action:

  1. Get faster hardware. A second, very fast, dedicated drive, for example.
  2. Reduce the number of files you create. Perhaps you can save multiple images in a compressed format and then write them all as a single file to disk (a zip file, for example). Or reduce the frame rate.
  3. Reduce the amount of data that you write (through compression or by reducing the image size).
于 2013-09-04T14:36:41.087 回答