Ask Your Question
0

Segmentation fault OpenCV cap.read() udp stream Python

asked 2016-09-15 10:29:14 -0600

Sam94 gravatar image

updated 2016-09-16 17:11:04 -0600

I'm new to using OpenCV, and I'm trying to write a program to access a video stream on a UDP port. However, the code keeps giving a segmentation fault when I run it. The program is just intended to display each frame as it is read in by OpenCV, and it works on files on my computer. If you could point out what I'm doing wrong, I would appreciate it.

import cv2
import numpy as np

cap = cv2.VideoCapture("udpsrc port=5600 caps=\"application/x-rtp, format=(string)I420, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)progressive, colorimetry=(string)bt709, framerate=(fraction)25/1\" ! rtph264depay !  decodebin ! appsink", cv2.CAP_GSTREAMER)

while(cap.isOpened()):
    print "loop"

    ret, frame = cap.read()
    print "ret, frame"

    if frame == None:
        break
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    print "gray"

    cv2.imshow('frame', gray)
    print "imshow"
    if cv2.waitKey(40) & 0xFF == ord('q'):
        print "breaking"
        break

cap.release()
cv2.destroyAllWindows()

The output is:

loop ret, frame gray imshow loop Segmentation fault (core dumped)

Running:

gst-launch-1.0 -e udpsrc port=5600 caps="application/x-rtp, format=(string)I420, width=(int)1280, height=(int)720, pixel-aspect-ratio=(fraction)1/1, interlace-mode=(string)progressive, colorimetry=(string)bt709, framerate=(fraction)25/1" ! rtph264depay !  decodebin ! avimux ! filesink location=/home/lab/Desktop/test.avi

in the terminal works just fine, so I'm not sure what to look at next.

Thanks for your help.

edit retag flag offensive close merge delete

Comments

try to add a check, before the color conversion: if frame is None: break (your connection might have boee interrupted.)

berak gravatar imageberak ( 2016-09-15 10:39:25 -0600 )edit

Thanks but that didn't fix it, still the same error. I updated the code to reflect the changes.

Sam94 gravatar imageSam94 ( 2016-09-15 10:47:01 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-09-16 03:10:12 -0600

pklab gravatar image

updated 2016-09-16 03:12:05 -0600

I think your URL is wrong or, considering you are using a GStreamer interface your should use the apiPreference param like below:

cap = cv2.VideoCapture(YOUR_URL, cv2.CAP_GSTREAMER)

In addiction your OpenCV binaries must have enabled the GStreamer API, usually isn't, therefore you have to rebuild OpenCV from source and enable the API using CMake.

edit flag offensive delete link more

Comments

My OpenCV is built with GStreamer enabled. I added the cv2.CAP_GSTREAMER as suggested, but it's still throwing the same error. What do you think might be wrong with the URL? It works in gstreamer, so I would think that it would work here too. I did test "udpsrc uri=udp://0.0.0.0:5600" but it still has the same error.

Sam94 gravatar imageSam94 ( 2016-09-16 17:09:12 -0600 )edit

I can''t really help because I don't have a GStreamer system ready and I never tried it, but

  • Check if this helps
  • try again using the simplest URL similar to: video/x-raw, format=RGB ! videoconvert ! appsink ! udpsrc host=localhost port=5000
  • Go in depth with debug
pklab gravatar imagepklab ( 2016-09-17 03:24:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-15 10:29:14 -0600

Seen: 3,265 times

Last updated: Sep 16 '16