12

I am trying to capture a live stream from a GoPro using cv2 in python with the following code:

VIDEO_URL = "http://10.5.5.9:8080/live/amba.m3u8"
cam = cv2.VideoCapture(VIDEO_URL)
cv2.namedWindow("GoPro",cv2.CV_WINDOW_AUTOSIZE)
while True:
    f, im = cam.read()
    cv2.imshow("GoPro",im)
    if cv2.waitKey(5) == 27:
        break
cam.release()
cv2.destroyAllWindows()

but receive the following errors:

WARNING: Couldn't read movie file http://10.5.5.9:8080/live/amba.m3u8
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file     /tmp/opencv-MRl1/opencv-2.4.7.1/modules/highgui/src/window.cpp, line 261
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "gopro_simple.py", line 167, in live_stream
    cv2.imshow("GoPro",im)
cv2.error: /tmp/opencv-MRl1/opencv-2.4.7.1/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow

The stream works in vlc, and the code works with a webcam, so it looks like the problem is that opencv doesn't like the .m3u8 format. Any ideas / suggestions of how to fix this would be greatly appreciated. Thanks.

jeremyeastwood
  • 407
  • 1
  • 3
  • 11
  • [That's a strange coincidence](http://stackoverflow.com/questions/22003438/can-opencv-read-m3u8) – rae1 Feb 25 '14 at 02:32

2 Answers2

14

Found a solution which calls ffmpeg here - works great (with a couple of small alterations to the ffmpeg options):

VIDEO_URL = WEBURL + "live/amba.m3u8"

cv2.namedWindow("GoPro",cv2.CV_WINDOW_AUTOSIZE)

pipe = sp.Popen([ FFMPEG_BIN, "-i", VIDEO_URL,
           "-loglevel", "quiet", # no text output
           "-an",   # disable audio
           "-f", "image2pipe",
           "-pix_fmt", "bgr24",
           "-vcodec", "rawvideo", "-"],
           stdin = sp.PIPE, stdout = sp.PIPE)
while True:
    raw_image = pipe.stdout.read(432*240*3) # read 432*240*3 bytes (= 1 frame)
    image =  numpy.fromstring(raw_image, dtype='uint8').reshape((240,432,3))
    cv2.imshow("GoPro",image)
    if cv2.waitKey(5) == 27:
        break
cv2.destroyAllWindows()

Still tinkering with the code so any suggestions welcome.

jeremyeastwood
  • 407
  • 1
  • 3
  • 11
  • Are you on 64 bit? I had this issue when using 64bit python and opencv. When I switched to 32-bit the issue is gone... – MartinM Apr 14 '14 at 12:33
  • Note that this will not necessarily show all frames in time; to show them in time try adding to to end of the loop `time.sleep( 1.0 / FPS)` – Tomer Oct 29 '17 at 21:12
  • any ideas on how to get the correct height and width so that we dont have manually change the reshape if the URL changes? – Oscar Dolloway Jan 28 '20 at 18:08
  • Add import subprocess as sp on top – Ajai Jul 02 '21 at 16:52
0

Perhaps you use VLC: Open the stream in VLC (http://10.5.5.9:8080/live/amba.m3u8) and then you can restream it or use it as you want. VLC is very powerful. I can strem my gopro content to internet

konraditurbe
  • 165
  • 9
  • Yeah VLC works nicely for recovering the stream, however for my specific application I was keener to run everything through python and command line resources; if I remember rightly the ffmpeg route also introduced less of a delay in the live stream than running through VLC. – jeremyeastwood May 26 '14 at 20:49