在 JavaScript 中从多个 USB 摄像头创建视频流时难以实现并行化

问题描述 投票:0回答:1

我正在尝试使用 JavaScript(chrome、windows)从六个 USB 摄像头捕获图像。我实现了三个功能:一个用于检索已连接摄像机的列表,另一个用于初始化视频流,第三个用于从这些流中捕获图像。尽管使用了 async/await 和 map 函数,我仍然面临着流创建并行化的问题。

问题在于,与顺序方法相比,创建流所需的时间没有显示出任何改进。流创建似乎没有按预期并行发生。

这是我的代码的简化版本:

    // Function to get the list of connected cameras
    const getCameraList = async () => {
      const devices = await navigator.mediaDevices.enumerateDevices();
      return devices.filter(device => device.kind === 'videoinput');
    }

    // Function to initialize video streams
    const initializeStreams = async (camera) => {
      const constraints = {
        audio: false,
        video: { deviceId: camera.deviceId },
      };
      return await navigator.mediaDevices.getUserMedia(constraints)
    }

    // Function to capture images from streams
    const captureImages = async (streams) => {
      const result = await Promise.all(streams.map(async (stream) => {
        const videoTrack = stream.getVideoTracks()[0];
        if (!videoTrack) return;

        const imageCapture = new ImageCapture(videoTrack);
        const blob = await imageCapture.takePhoto({});
        return blob;
      }))
    }

    // Main function to orchestrate the process
    const main = async () => {
      try {
        const cameras = await getCameraList();

        // The issue is here: parallelization is not effective
        const streams = await Promise.all(cameras.map(initializeStreams));

        // same issue here: parallelization is not effective
        await captureImages(streams);
      } catch (error) {
        console.error("An error occurred:", error);
      }
    }

    main();

这是初始化流的日志结果:

13:40:05.921  Cam3: 6524.796142578125 ms
13:40:11.737  Cam6: 12340.535888671875 ms
13:40:13.050  Cam5: 13652.81201171875 ms
13:40:18.869  Cam1: 19471.5029296875 ms
13:40:24.712  Cam2: 25313.91796875 ms
13:40:30.521  Cam4: 31123.049072265625 ms


13:40:30.521  load streams: 31124.97021484375 ms

对于如何在创建视频流期间实现有效并行化的任何建议,我将不胜感激。谢谢!

javascript webrtc usb-camera
1个回答
0
投票

需要一些工作人员或任何视频 SDK 才能实现并行化,其中一个网站是 videosdk.live

© www.soinside.com 2019 - 2024. All rights reserved.