UDP 直播 (GoPro Hero4) 在 python 中使用 OpenCV 打开,但不在 C++ 中打开

UDP livestream (GoPro Hero4) opens using OpenCV in python but not in C++

我想在 GoPro Hero4 Black 直播中使用 C++ 中的 OpenCV 进行图像处理。固件版本为5.0.

使用 python 它成功运行。当我在 C++ 状态 31 和 32 中以相同的方式实现它时,打开 VideoCapture 时切换到 1,因此直播已启动并且客户端已连接。但是,下面的 cap.isOpen() returns false。我需要补充一点,cap.open(...) 命令让程序停止几秒钟,这是不合理的。

代码在python

import cv2
import numpy as np
from time import time
import socket
from goprocam import GoProCamera
from goprocam import constants
import requests

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
t=time()

url     = "http://10.5.5.9/gp/gpControl/execute?p1=gpStream&a1=proto_v2&c1=restart"
payload = {  }
headers = {}
res = requests.post(url, data=payload, headers=headers)

cap = cv2.VideoCapture("udp://10.5.5.9:8554")

while (cap.isOpened()):
    nmat, frame = cap.read()
    if nmat == True:
        cv2.imshow("GoPro OpenCV", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    if time() - t >= 2.5:
        sock.sendto("_GPHD_:0:0:2:0.000000\n".encode(), ("10.5.5.9", 8554))

        t=time()
# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()

C++中的代码

#define CURL_STATICLIB
#include <curl\curl.h>
#include <boost/exception/exception.hpp>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <boost/asio.hpp>

#include <iostream>

#include "opencv2/opencv.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;

int main()
{
    CURL* curl;
    curl = curl_easy_init();
    if (curl) {

        curl_easy_setopt(curl, CURLOPT_URL, "http://10.5.5.9/gp/gpControl/execute?p1=gpStream&c1=start");
        curl_easy_perform(curl);
        curl_easy_setopt(curl, CURLOPT_URL, "http://10.5.5.9/gp/gpControl/execute?p1=gpStream&c1=restart");
        curl_easy_perform(curl);
    }

    boost::asio::io_service ioService;
    boost::asio::ip::udp::resolver resolver(ioService);
    boost::asio::ip::udp::endpoint dest(boost::asio::ip::address::from_string("10.5.5.9"), 8554);
    boost::asio::ip::udp::socket sock(ioService, boost::asio::ip::udp::v4());
    boost::this_thread::sleep_for(boost::chrono::milliseconds(2000));
    sock.send_to(boost::asio::buffer("_GPHD_:0:0:2:0.000000\n", 22), dest);
    
    VideoCapture cap;

    cap.open("udp://10.5.5.9:8554");

    while (cap.isOpened() )
    {
        std::cout << "is open" << std::endl;

    }

    return 0;
}

有人知道如何解决这个问题吗?

我还尝试了不同的 API VideoCapture 参考资料

cap.open("udp://10.5.5.9:8554", CAP_ANY);
cap.open("udp://10.5.5.9:8554", CAP_FFMPEG);

我也试过使用@前缀

cap.open("udp://@10.5.5.9:8554", CAP_ANY);

更新: 我无法测试 VideoCapture 后端的正确执行,因为笔记本电脑网络摄像头似乎无法用 python 中的 CAP_FFMPEG 打开。 但是,当我通过声明索引 0 并使用 CAP_FFMPEG 后端让笔记本电脑网络摄像头 运行 时,我得到的行为与我声明 gopro udp livestream 时的行为相同。

cap.open(0, CAP_FFMPEG);

解决方案是停用专用网络的 windows 防火墙。不能再琐碎了,但是万一有人在那个时候挣扎……这里是提示:)