How connect my GoPro Hero 4 camera live stream to openCV using Python?
Asked Answered
G

2

7

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

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

Glabrescent answered 20/3, 2016 at 9:55 Comment(0)
A
3

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

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

pip install goprocam

if not

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

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")

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

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()

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.

Alroy answered 20/5, 2017 at 6:18 Comment(2)
Thanks for the reply. The code used to restream UDP to localhost repeatedly throws this error for me, [mpegts @ 0x7f981b801400] Packet corrupt (stream = 0, dts = 1432431), with increasing dts values. Do you have any insight to the cause?Plotinus
How can we do this for Go Pro 9? It doesn't work for go pro 9.Chromatin
H
0

This is because you are not choosing a literal stream to capture video from. Your ip from the videocapture object must contain an extension on the end, whether it's .jpg or .mpeg (I'd prefer using this), or a few others listed in the documentation. If you use a literal video stream (such as the .mpeg instead of a picture), you should do cv.grab then cv.retrieve. this goes for all ip cameras. Hope this helped :)

Hormonal answered 11/12, 2016 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.