My configuration:
ubuntu 16.04
opencv 3.3.1
gcc version 5.4.0 20160609
ffmpeg version 3.4.2-1~16.04.york0
and I built opencv with:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON_EXECUTABLE=$(which python) -D OPENCV_EXTRA_MODULES_PATH=/home/xxx/opencv_contrib/modules -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_IPP=ON -D WITH_OPENNI2=ON -D WITH_V4L=ON -D WITH_FFMPEG=ON -D WITH_GSTREAMER=OFF -D WITH_OPENMP=ON -D WITH_VTK=ON -D BUILD_opencv_java=OFF -D BUILD_opencv_python3=OFF -D WITH_CUDA=ON -D ENABLE_FAST_MATH=1 -D WITH_NVCUVID=ON -D CUDA_FAST_MATH=ON -D BUILD_opencv_cnn_3dobj=OFF -D FORCE_VTK=ON -D WITH_CUBLAS=ON -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" -D WITH_GDAL=ON -D WITH_XINE=ON -D BUILD_EXAMPLES=OFF -D BUILD_DOCS=ON -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -D BUILD_opencv_dnn=OFF -D BUILD_PROTOBUF=OFF -D opencv_dnn_BUILD_TORCH_IMPORTER=OFF -D opencv_dnn_PERF_CAFFE=OFF -D opencv_dnn_PERF_CLCAFFE=OFF -DBUILD_opencv_dnn_modern=OFF -D CUDA_ARCH_BIN=6.1 ..
and use these python code to read and show:
import cv2
from com.xxx.cv.core.Image import Image
capture=cv2.VideoCapture("rtsp://192.168.10.184:554/mpeg4?username=xxx&password=yyy")
while True:
grabbed,content=capture.read()
if grabbed:
Image(content).show()
doSomething()
else:
print "nothing grabbed.."
Everytime, after reading about 50 frames,it will give an error like:
[h264 @ 0x8f915e0] error while decoding MB 53 20, bytestream -7
then nothing can be grabbed further,and the strange thing is:
1,comment doSomething() or
2,keep doSomething() and recording the stream from same IPCamera,then run
code against recorded video
both cases,code works fine,can anyone tell how to solve this problem?Thank in advance!
I was facing this problem and I have just solved it now.
(Note: I am using python3)
I have suspected that it has something to do with the timing, because that error arises when doing expensive operations between consecutive capture.read()
. Your question made me sure about this.
Also, the problem arises when I stream from my network camera (which uses H264 encoding) and there was no problem when using my laptop camera.
So the solution that worked for me is using multi-threading, with the python "threading" module. One thread streams and the other processes, while managing thread locks correctly so no read/write conflicts occur.