如何使用 Python 将我的 GoPro Hero 4 摄像机实时流连接到 openCV? [英] How connect my GoPro Hero 4 camera live stream to openCV using Python?

查看:117
本文介绍了如何使用 Python 将我的 GoPro Hero 4 摄像机实时流连接到 openCV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从我的新 GoPro Hero 4 相机捕获实时流并使用 openCV 对其进行一些图像处理时遇到问题.

I 'm having troubles trying to capture a live stream from my new GoPro Hero 4 camera and do some image processing on it using openCV.

这是我的试用版(创建的窗口上没有显示任何内容

Here is my trial (nothing shows up on the created window

import cv2
import argparse
import time
import datetime
from goprohero import GoProHero


ap = argparse.ArgumentParser()
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum    area size")
args = vars(ap.parse_args())

camera = cv2.VideoCapture("http://10.5.5.9:8080/gp/gpControl/executep1=gpStream&c1=restart")
time.sleep(5)

cv2.namedWindow("", cv2.CV_WINDOW_AUTOSIZE)

firstFrame = None
noOfCars = 0
speed = 80

while True: 
    (grabbed, frame) = camera.read()
    text = "Smooth"
    print("Capturing ...")

    if not grabbed:
        print("nothing grabbed")
        break

循环中断,因为抓取总是等于假,这意味着 openCV 一无所获.

the loop breaks as grabbed always equals false which means openCV got nothing.

推荐答案

对于那些想知道我是否能够在 OpenCV 上获得良好流的人:

For those wondering I was able to get a good stream on OpenCV:

首先,您需要下载 GoPro Python API(如果您有 pip):

First you'll need to download the GoPro Python API, if you have pip:

pip install goprocam

如果不是

git clone https://github.com/konradit/gopro-py-api
cd gopro-py-api
python setup.py install

然后在 python 终端窗口中运行以下代码:

Then run the following code in a python terminal window:

from goprocam import GoProCamera
from goprocam import constants
gopro = GoProCamera.GoPro()
gopro.stream("udp://127.0.0.1:10000")

这会将UDP流重新流式传输到本地主机,路径上需要FFmpeg!

This will re-stream the UDP stream to localhost, FFmpeg is needed on the path!

然后就可以使用OpenCV打开localhost流了:

Then you can use OpenCV to open the localhost stream:

import cv2
import numpy as np
from goprocam import GoProCamera
from goprocam import constants
cascPath="/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
gpCam = GoProCamera.GoPro()
cap = cv2.VideoCapture("udp://127.0.0.1:10000")
while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    cv2.imshow("GoPro OpenCV", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

查看更多示例此处 - 您可以甚至使用纯 OpenCV 打开流 虽然我不推荐它,因为它非常滞后,但 ffmpeg > localhost > opencv 与仅 opencv 相比非常稳定.

See further examples here - you can even use pure OpenCV to open the stream although I don't recommend it because its very laggy this way, ffmpeg > localhost > opencv is very stable compared to opencv only.

这篇关于如何使用 Python 将我的 GoPro Hero 4 摄像机实时流连接到 openCV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆