Segmentation fault OpenCV cap.read udp stream Python
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")
while(cap.isOpened()):
print "loop"
ret, frame = cap.read()
print "ret, frame"
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: As suggested by Samer Tufail, I tried adding:
if cap.set(3, 1280)==True:
print "width set"
else:
print "error width"
sys.exit()
if cap.set(4, 720)==True:
print "height set"
else:
print "error height"
sys.exit()
between "cap = cv2.VideoCapture()...while(cap.isOpened())". However, it gives a different error now (I tried with and without the ==True).
GStreamer Plugin: Embedded video playback halted; module udpsrc0 reported: Internal data flow error. OpenCV Error: Unspecified error (GStreamer: unable to start pipeline ) in icvStartPipeline, file /home/lab/Sam/OpenCV/opencv/modules/videoio/src/cap_gstreamer.cpp, line 407 Traceback (most recent call last): File "OpenCV_Gst.py", line 9, in if cap.set(3, 1280)==True: cv2.error: /home/lab/Sam/OpenCV/opencv/modules/videoio/src/cap_gstreamer.cpp:407: error: (-2) GStreamer: unable to start pipeline in function icvStartPipeline
I solve the same problem,when I do this:
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 !
videoconvert ! decodebin ! appsink")
It works.
链接地址: http://www.djcxy.com/p/25432.html