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

2024/10/11 14:18:20

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 GoProHeroap = 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 = 80while 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.

Answer

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.

https://en.xdnf.cn/q/69768.html

Related Q&A

I thought Python passed everything by reference?

Take the following code#module functions.py def foo(input, new_val):input = new_val#module main.py input = 5 functions.foo(input, 10)print inputI thought input would now be 10. Why is this not the cas…

Python: -mno -cygwin

im trying to learn a lot of python on windows and that includes installing several packages, however everytime i invoke python setup.py install i have a problem with -mno -cygwin for gcc. ive have rea…

Django Sites Framework initial setup

Im comfortable with fairly one-dimensional Django implementations, but now trying to understand the multi-sites-with-shared-stuff process. Ive read through the Django Sites Framework and many posts o…

Data corruption: Wheres the bug‽

Last edit: Ive figured out what the problem was (see my own answer below) but I cannot mark the question as answered, it would seem. If someone can answer the questions I have in my answer below, name…

Python NetworkX — set node color automatically based on a list of values

I generated a graph with networkx import networkx as nx s = 5 G = nx.grid_graph(dim=[s,s]) nodes = list(G.nodes) edges = list(G.edges) p = [] for i in range(0, s):for j in range(0, s):p.append([i,j])…

control wspace for matplotlib subplots

I was wondering: I have a 1 row, 4 column plot. However, the first three subplots share the same yaxes extent (i.e. they have the same range and represent the same thing). The forth does not. What I w…

Getting indices of both zero and nonzero elements in array

I need to find the indicies of both the zero and nonzero elements of an array.Put another way, I want to find the complementary indices from numpy.nonzero().The way that I know to do this is as follows…

tweepy how to get a username from id

how do I derrive a plaintext username from a user Id number with tweepy? Here is the CORRECTED code that I am using:ids = [] userid = "someOne" for page in tweepy.Cursor(api.followers_ids, s…

How to select many to one to many without hundreds of queries using Django ORM?

My database has the following schema:class Product(models.Model):passclass Tag(models.Model):product = models.ForeignKey(Product)attr1 = models.CharField()attr2 = models.CharField()attr3 = models.CharF…

Quickly dumping a database in memory to file

I want to take advantage of the speed benefits of holding an SQLite database (via SQLAlchemy) in memory while I go through a one-time process of inserting content, and then dump it to file, stored to b…