【问题标题】:Cannot initialize two VideoCapture instances in Raspberry Pi 4无法在 Raspberry Pi 4 中初始化两个 VideoCapture 实例
【发布时间】:2021-10-26 01:22:00
【问题描述】:

我一直在尝试使用 Raspberry Pi 中的两个网络摄像头为我正在进行的项目录制视频。 我尝试使用 OpenCV VideoCapture 函数来执行此操作,它在我的笔记本电脑上运行良好,但是当我尝试在 Raspberry Pi 中运行我的脚本时,它无法加载并且我收到此错误

[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (1761) ha
ndleMessage Opencv | GStreamer warning: Embedded video playback halted; module v
412srco reported: Cannot identify device '/dev/video-1'.
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (888) ope
n Opencv | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (480) is
ipelinePlaying opencv | GStreamer warning: Gstreamer: pipeline have not been cre
ated
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (935) ope
n Opencv | GStreamer warning: Cannot query video position: status=0, value=-1, d
uration=-1

我只是调用一个函数来保存一定长度的视频,在这里(它也在这个repo):

def save_video_len(video_len):
    video_frames_1 = []
    video_frames_2 = []
    cap_1 = cv2.VideoCapture(0) # Index '0' is the default one
    cap_2 = cv2.VideoCapture(2) # Index '2' was obtained by trial and error
    fps_1 = cap_1.get(cv2.CAP_PROP_FPS) # Getting FPS for Cam 1
    fps_2 = cap_2.get(cv2.CAP_PROP_FPS) # Getting FPS for Cam 2
    # print(f'FPS 1: {fps_1}\nFPS 2: {fps_2}')

    frame_1 = np.array([])
    frame_2 = np.array([])
    t0 = time.time()
    t1 = t0
    date_0 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    out_1 = cv2.VideoWriter(f'cam1_video_date_{date_0}.avi', cv2.VideoWriter_fourcc(*'MJPG'), fps_1, (320,240) )
    out_2 = cv2.VideoWriter(f'cam2_video_date_{date_0}.avi', cv2.VideoWriter_fourcc(*'MJPG'), fps_2, (320,240) )

    while t1 - t0 < video_len:
        date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        t1 = time.time()

        # Capture frame-by-frame
        ret_1, frame_1 = cap_1.read()
        ret_2, frame_2 = cap_2.read()
        # Added date to video frame
        frame_1 = cv2.putText(frame_1, date, (10,20), cv2.FONT_HERSHEY_DUPLEX, fontScale=0.5, color=(0,255,0))
        frame_1 = cv2.resize(frame_1, (320,240), interpolation = cv2.INTER_AREA)
        frame_2 = cv2.putText(frame_2, date, (10,20), cv2.FONT_HERSHEY_DUPLEX, fontScale=0.5, color=(0,255,0))
        frame_2 = cv2.resize(frame_2, (320,240), interpolation = cv2.INTER_AREA)

        out_1.write(frame_1)
        out_2.write(frame_2)

        # Display the resulting frame
        cv2.imshow('Cam 1', frame_1)
        cv2.imshow('Cam 2', frame_2)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        video_frames_1.append(frame_1)
        video_frames_2.append(frame_2)

    # When everything done, release the capture
    cap_1.release()
    cap_2.release()
    cv2.destroyAllWindows()

我也尝试使用 imutils.video.VideoStream 函数,但遇到了同样的问题。此外,当我只留下一个网络摄像头时,它在这两种情况下都可以正常工作。

如果有人知道解决此问题的更好方法,或者我做错了什么,我将不胜感激。

谢谢!

【问题讨论】:

    标签: python opencv raspberry-pi computer-vision webcam-capture


    【解决方案1】:

    您可能缺少一些依赖项,请尝试安装所有 第 2 步下的 apt 包,https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/

    【讨论】:

      猜你喜欢
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2023-03-08
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多