I've had a Python (3.7) app using OpenCV (3.4) for a year or so, working fine. I recently reinstalled Debian Buster, and with it upgraded to OpenCV 4.1. Somewhere in that process I've found that videos which previously worked ok now can't be read by OpenCV.
A sample video with the problem is here: https://www.dropbox.com/s/tu4ddegh6yn05nu/ErrorReadingHeader.mp4?dl=0
Basically, this plays fine in most media apps, but my install of OpenCV won't open it - isOpened() returns false. However, the odd thing is that it's not a constant problem - most of the videos from the same IP camera work fine, but around a quarter can't be read. I assume there's some very minor oddity with the files, but I can't see what it is, and it's hard to get the manufacturer to do much about it because they play fine in most media apps.
When I run `cv2.getBuildInformation()` I get the following:
Video I/O:
DC1394: NO
FFMPEG: YES
avcodec: YES (58.35.100)
avformat: YES (58.20.100)
avutil: YES (56.22.100)
swscale: YES (5.3.100)
avresample: NO
GStreamer: YES (1.14.4)
v4l/v4l2: YES (linux/videodev2.h)
After turning on debugging mode (with `export OPENCV_LOG_LEVEL=debug` and `export OPENCV_VIDEOIO_DEBUG=1`) I get the following when I try to do `vc = cv2.VideoCapture('/home/Dave/ErrorReadingHeader.mp4')`:
[ INFO:0] VIDEOIO: Enabled backends(5, sorted by priority): FFMPEG(1000); GSTREAMER(990); V4L2(980); CV_IMAGES(970); CV_MJPEG(960)
[ WARN:0] VIDEOIO(FFMPEG): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2338590] error reading header
[ WARN:0] VIDEOIO(FFMPEG): can't create capture
[ WARN:0] VIDEOIO(GSTREAMER): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[ WARN:0] VIDEOIO(GSTREAMER): can't create capture
[ WARN:0] VIDEOIO(V4L2): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
VIDIOC_REQBUFS: Inappropriate ioctl for device
[ WARN:0] VIDEOIO(V4L2): can't create capture
[ WARN:0] VIDEOIO(CV_IMAGES): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[ WARN:0] VIDEOIO(CV_IMAGES): created, isOpened=0
[ WARN:0] VIDEOIO(CV_MJPEG): trying capture filename='/home/Dave/ErrorReadingHeader.mp4' ...
[ WARN:0] VIDEOIO(CV_MJPEG): can't create capture
Any thoughts on how to resolve this issue? Keen to understand if this particular video can be opened in other people's installations of OpenCV 4.1, and if so what your VideoIO configuration is.
Edit: for info, the following are the steps I used to install OpenCV 4.1 on Raspberry Pi 4 / Debian Buster:
Temporarily increase swap file size and gpu memory:
sudo sed -i 's/CONF_SWAPSIZE=100/CONF_SWAPSIZE=2048/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
sudo sed -i '$a gpu_mem=128' /boot/config.txt
Install dependencies:
sudo apt-get -y install build-essential cmake pkg-config git
sudo apt-get -y install libjpeg-dev libtiff-dev libjasper-dev libpng12-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
sudo apt-get -y install libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran
sudo apt-get install libcanberra-gtk* which is an ARM-specific version of GTK
sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get -y install python3-dev python3-numpy python3-pip
Download and prepare OpenCV:
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.1.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.1.0.zip
unzip opencv.zip
unzip opencv_contrib.zip
mv opencv-4.1.0/ opencv
mv opencv_contrib-4.1.0/ opencv_contrib
Build OpenCV:
cd ~/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3 \
-D WITH_GSTREAMER=ON \
-D WITH_GSTREAMER_0_10=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..
Make and install OpenCV:
make -j4 noting this could take an hour or more
sudo make install
sudo ldconfig
sudo apt-get update
Copy the package for Python:
sudo cp ~/opencv/build/lib/python3/cv2.cpython-37m-arm-linux-gnueabihf.so /usr/local/lib/python3.7/dist-packages/cv2.so
Tidy up:
cd ~
sudo sed -i 's/CONF_SWAPSIZE=2048/CONF_SWAPSIZE=100/g' /etc/dphys-swapfile
sudo /etc/init.d/dphys-swapfile stop
sudo /etc/init.d/dphys-swapfile start
sudo sed -i 's/gpu_mem=128/gpu_mem=16/g' /boot/config.txt
rm opencv.zip
rm opencv_contrib.zip
sudo rm -r opencv
sudo rm -r opencv_contrib
Test that it installed successfully:
python 3, then within python import cv2, then print(cv2.__version__)
Thanks a lot!
↧